devel_wizard-2.x-dev/src/Spell/SpellAbracadabraForm.php
src/Spell/SpellAbracadabraForm.php
<?php
declare(strict_types=1);
namespace Drupal\devel_wizard\Spell;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* @todo \Drupal\Core\Form\BaseFormIdInterface.
*/
class SpellAbracadabraForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'devel_wizard_spell_abracadabra_form';
}
public function title(SpellInterface $spell) {
return $this->t(
'Cast a spell on this site – @spell.label',
[
'@spell.label' => $spell->getPluginDefinition()->getLabel(),
],
);
}
public function buildForm(array $form, FormStateInterface $form_state, ?SpellInterface $spell = NULL) {
if ($spell instanceof ConfigurableInterface) {
// @todo Get default values from queryString.
$configuration = [];
$spell->setConfiguration($configuration);
}
if ($spell instanceof PluginFormInterface) {
$form['spell'] = $spell->buildConfigurationForm(
[
'#type' => 'container',
'#tree' => TRUE,
'#parents' => [
'spell',
],
'#arra_parents' => [
'spell',
],
],
$form_state,
);
}
else {
$form['message'] = [
'#markup' => '<p>' . $this->t('This spell has no any configuration.') . '</p>',
];
}
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this->t('Abracadabra'),
'#name' => 'abracadabra',
'#button_type' => 'primary',
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
/** @var \Drupal\devel_wizard\Spell\SpellInterface $spell */
$spell = $form_state->getBuildInfo()['args'][0];
if ($spell instanceof PluginFormInterface) {
$spell->validateConfigurationForm($form['spell'], $form_state);
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\devel_wizard\Spell\SpellInterface $spell */
$spell = $form_state->getBuildInfo()['args'][0];
if ($spell instanceof PluginFormInterface) {
$spell->submitConfigurationForm($form['spell'], $form_state);
}
$batchBuilder = SpellAbracadabraBatchHandler::build($spell);
batch_set($batchBuilder->toArray());
}
}
