devel_wizard-2.x-dev/src/Plugin/DevelWizard/Spell/BlockContentTypeSpell.php
src/Plugin/DevelWizard/Spell/BlockContentTypeSpell.php
<?php
declare(strict_types=1);
namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\devel_wizard\Attribute\DevelWizardSpell;
use Drupal\devel_wizard\Spell\SpellManagerInterface;
use Drupal\devel_wizard\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
#[DevelWizardSpell(
id: 'devel_wizard_block_content_type',
category: new TranslatableMarkup('Block type'),
label: new TranslatableMarkup('Block type - All in one'),
description: new TranslatableMarkup('Creates a Block type with all the bells and whistles.'),
tags: [
'bundle' => new TranslatableMarkup('Bundle'),
'config_entity' => new TranslatableMarkup('Config entity'),
'block_content_type' => new TranslatableMarkup('Block type'),
'site_build' => new TranslatableMarkup('Site build'),
],
)]
class BlockContentTypeSpell extends ConfigEntitySpellBase {
protected string $provider = 'block_content';
protected string $configEntityTypeId = 'block_content_type';
protected string $contentEntityTypeId = 'block_content';
protected SpellManagerInterface $spellManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('messenger'),
$container->get('logger.channel.devel_wizard_spell'),
$container->get('string_translation'),
$container->get('devel_wizard.utils'),
$container->get('config.factory'),
$container->get('plugin.manager.devel_wizard.spell'),
);
}
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
MessengerInterface $messenger,
LoggerInterface $logger,
TranslationInterface $stringTranslation,
Utils $utils,
ConfigFactoryInterface $configFactory,
SpellManagerInterface $spellManager,
) {
$this->spellManager = $spellManager;
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$messenger,
$logger,
$stringTranslation,
$utils,
$configFactory,
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'create_enabled' => TRUE,
'create' => [
'values' => [
'id' => '',
],
],
'user_role_enabled' => TRUE,
'user_role' => [],
'migration_enabled' => TRUE,
'migration' => [],
'admin_view_enabled' => TRUE,
'admin_view' => [],
'behat_enabled' => TRUE,
'behat' => [],
];
}
public function populateCalculatedConfigurationValues(): static {
parent::populateCalculatedConfigurationValues();
$bundleId = $this->configuration['create']['values']['id'] ?? '';
if (!$bundleId) {
throw new \InvalidArgumentException('Block type ID is required');
}
if (empty($this->configuration['user_role']['machine_name'])) {
$this->configuration['user_role']['machine_name'] = $bundleId;
}
if (empty($this->configuration['migration']['machine_name'])) {
$this->configuration['migration']['machine_name'] = $bundleId;
}
if (empty($this->configuration['admin_view']['machine_name'])) {
$this->configuration['admin_view']['machine_name'] = $bundleId;
}
if (empty($this->configuration['behat']['machine_name'])) {
$this->configuration['behat']['machine_name'] = $bundleId;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$configuration = $this->getConfiguration();
/** @var \Drupal\devel_wizard\Plugin\DevelWizard\Spell\BlockContentTypeCreateSpell $createSpell */
/* @noinspection PhpUnhandledExceptionInspection */
$createSpell = $this
->spellManager
->createInstance(
"devel_wizard_{$this->configEntityTypeId}_create",
$configuration['create'],
);
$form['create'] = $createSpell->buildConfigurationForm(
[
'#parents' => array_merge($form['#parents'], ['create']),
'#tree' => TRUE,
'#type' => 'details',
'#title' => $this->t('Create'),
'#open' => TRUE,
],
$form_state,
);
/** @var \Drupal\devel_wizard\Plugin\DevelWizard\Spell\BlockContentTypeUserRoleSpell $userRoleSpell */
/* @noinspection PhpUnhandledExceptionInspection */
$userRoleSpell = $this
->spellManager
->createInstance(
"devel_wizard_{$this->configEntityTypeId}_user_role",
$configuration['user_role'],
);
$form['user_role_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Create Block type specific administrator user role'),
'#required' => FALSE,
'#default_value' => $configuration['user_role_enabled'],
];
$userRoleInputName = $this->utils->inputName($form['#parents'], 'user_role_enabled');
$form['user_role'] = $userRoleSpell->buildConfigurationForm(
[
'#parents' => array_merge($form['#parents'], ['user_role']),
'#tree' => TRUE,
'#type' => 'details',
'#title' => $this->t('User role'),
'#open' => TRUE,
'#states' => [
'visible' => [
":input[name=\"{$userRoleInputName}\"]" => ['checked' => TRUE],
],
],
],
$form_state,
);
$form['user_role']['machine_name']['#required'] = FALSE;
$form['user_role']['machine_name']['#access'] = FALSE;
/** @var \Drupal\devel_wizard\Plugin\DevelWizard\Spell\BlockContentTypeMigrationSpell $migrationSpell */
/* @noinspection PhpUnhandledExceptionInspection */
$migrationSpell = $this
->spellManager
->createInstance(
"devel_wizard_{$this->configEntityTypeId}_migration",
$configuration['behat'],
);
$form['migration_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Create Block type specific migration definitions'),
'#required' => FALSE,
'#default_value' => $configuration['behat_enabled'],
];
$migrationInputName = $this->utils->inputName($form['#parents'], 'migration_enabled');
$form['migration'] = $migrationSpell->buildConfigurationForm(
[
'#parents' => array_merge($form['#parents'], ['migration']),
'#tree' => TRUE,
'#type' => 'details',
'#title' => $this->t('Migration'),
'#open' => TRUE,
'#states' => [
'visible' => [
":input[name=\"{$migrationInputName}\"]" => ['checked' => TRUE],
],
],
],
$form_state,
);
$form['migration']['machine_name']['#required'] = FALSE;
$form['migration']['machine_name']['#access'] = FALSE;
/** @var \Drupal\devel_wizard\Plugin\DevelWizard\Spell\BlockContentTypeAdminViewSpell $adminViewSpell */
/* @noinspection PhpUnhandledExceptionInspection */
$adminViewSpell = $this
->spellManager
->createInstance(
"devel_wizard_{$this->configEntityTypeId}_admin_view",
$configuration['admin_view'],
);
$form['admin_view_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Create Block type specific admin overview list'),
'#required' => FALSE,
'#default_value' => $configuration['admin_view_enabled'],
];
$adminViewInputName = $this->utils->inputName($form['#parents'], 'admin_view_enabled');
$form['admin_view'] = $adminViewSpell->buildConfigurationForm(
[
'#parents' => array_merge($form['#parents'], ['admin_view']),
'#tree' => TRUE,
'#type' => 'details',
'#title' => $this->t('Admin view'),
'#open' => TRUE,
'#states' => [
'visible' => [
":input[name=\"{$adminViewInputName}\"]" => ['checked' => TRUE],
],
],
],
$form_state,
);
$form['admin_view']['machine_name']['#required'] = FALSE;
$form['admin_view']['machine_name']['#access'] = FALSE;
/** @var \Drupal\devel_wizard\Plugin\DevelWizard\Spell\BlockContentTypeBehatSpell $behatSpell */
/* @noinspection PhpUnhandledExceptionInspection */
$behatSpell = $this
->spellManager
->createInstance(
"devel_wizard_{$this->configEntityTypeId}_behat",
$configuration['behat'],
);
$form['behat_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Create Block type specific Behat tests'),
'#required' => FALSE,
'#default_value' => $configuration['behat_enabled'],
];
$behatInputName = $this->utils->inputName($form['#parents'], 'behat_enabled');
$form['behat'] = $behatSpell->buildConfigurationForm(
[
'#parents' => array_merge($form['#parents'], ['behat']),
'#tree' => TRUE,
'#type' => 'details',
'#title' => $this->t('Behat'),
'#open' => TRUE,
'#states' => [
'visible' => [
":input[name=\"{$behatInputName}\"]" => ['checked' => TRUE],
],
],
],
$form_state,
);
$form['behat']['machine_name']['#required'] = FALSE;
$form['behat']['machine_name']['#access'] = FALSE;
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// @todo Implement validateConfigurationForm() method.
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue($form['#parents'], []);
$this->setConfiguration($values);
}
protected function doIt(): static {
$configuration = $this->getConfiguration();
if (!isset($this->batchContext['sandbox']['current_step'])) {
$this->batchContext['sandbox']['current_step'] = 'create';
$this->batchContext['sandbox']['sub_spells'] = [
'create' => [
'finished' => 0,
],
'user_role' => [
'finished' => 0,
],
'migration' => [
'finished' => 0,
],
'admin_view' => [
'finished' => 0,
],
'behat' => [
'finished' => 0,
],
];
$this->batchContext['sandbox']['finished'] = 1.0;
}
// @todo Translatable next_message.
$steps = [
'create' => [
'spell_id' => "devel_wizard_{$this->configEntityTypeId}_create",
'next_step' => 'user_role',
'next_message' => 'User role',
],
'user_role' => [
'spell_id' => "devel_wizard_{$this->configEntityTypeId}_user_role",
'next_step' => 'migration',
'next_message' => 'Migration',
],
'migration' => [
'spell_id' => "devel_wizard_{$this->configEntityTypeId}_migration",
'next_step' => 'admin_view',
'next_message' => 'Admin overview',
],
'admin_view' => [
'spell_id' => "devel_wizard_{$this->configEntityTypeId}_admin_view",
'next_step' => 'behat',
'next_message' => 'Behat',
],
'behat' => [
'spell_id' => "devel_wizard_{$this->configEntityTypeId}_behat",
'next_step' => '_finished',
'next_message' => 'Finished',
],
];
$stepId = $this->batchContext['sandbox']['current_step'];
switch ($stepId) {
case 'create':
case 'user_role':
case 'migration':
case 'admin_view':
case 'behat':
$step = $steps[$stepId];
if ($configuration["{$stepId}_enabled"]) {
/* @noinspection PhpUnhandledExceptionInspection */
$spell = $this
->spellManager
->createInstance(
$step['spell_id'],
$configuration[$stepId],
);
$spell->prepare();
$spell->abracadabra($this->batchContext['sandbox']['sub_spells'][$stepId]);
$this->batchContext['message'] = $this->batchContext['sandbox']['sub_spells'][$stepId]['message']
?? $spell->getPluginDefinition()->getLabel();
}
else {
$this->batchContext['sandbox']['sub_spells'][$stepId]['finished'] = 1.0;
}
if ($this->batchContext['sandbox']['sub_spells'][$stepId]['finished'] === 1.0) {
$this->batchContext['sandbox']['current_step'] = $step['next_step'];
$this->batchContext['message'] = $step['next_message'];
}
break;
case '_finished':
// @todo This will be overwritten in ::doIt().
$this->batchContext['finished'] = 1.0;
break;
}
if ($this->batchContext['finished'] === 1.0) {
$this->postProcessBatchContext();
}
return $this;
}
}
