devel_wizard-2.x-dev/src/Plugin/DevelWizard/Spell/TaxonomyVocabularySpell.php

src/Plugin/DevelWizard/Spell/TaxonomyVocabularySpell.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_taxonomy_vocabulary',
  category: new TranslatableMarkup('Taxonomy vocabulary'),
  label: new TranslatableMarkup('Taxonomy vocabulary - All in one'),
  description: new TranslatableMarkup('Creates a Taxonomy vocabulary with all the bells and whistles.'),
  tags: [
    'config_entity' => new TranslatableMarkup('Config entity'),
    'bundle' => new TranslatableMarkup('Bundle'),
    'taxonomy_vocabulary' => new TranslatableMarkup('Taxonomy vocabulary'),
    'site_build' => new TranslatableMarkup('Site build'),
  ],
)]
class TaxonomyVocabularySpell extends ConfigEntitySpellBase {

  protected string $provider = 'taxonomy';

  protected string $configEntityTypeId = 'taxonomy_vocabulary';

  protected string $contentEntityTypeId = 'taxonomy_term';

  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' => [
          'type' => '',
        ],
      ],
      'user_role_enabled' => TRUE,
      'user_role' => [],
      'migration_enabled' => TRUE,
      'migration' => [],
      'behat_enabled' => TRUE,
      'behat' => [],
    ];
  }

  public function populateCalculatedConfigurationValues(): static {
    parent::populateCalculatedConfigurationValues();

    $bundleId = $this->configuration['create']['values']['vid'] ?? '';
    if (!$bundleId) {
      throw new \InvalidArgumentException('machine_name 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['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\TaxonomyVocabularySpell $createSpell */
    /* @noinspection PhpUnhandledExceptionInspection */
    $createSpell = $this
      ->spellManager
      ->createInstance(
        'devel_wizard_taxonomy_vocabulary_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\NodeTypeUserRoleSpell $userRoleSpell */
    /* @noinspection PhpUnhandledExceptionInspection */
    $userRoleSpell = $this
      ->spellManager
      ->createInstance(
        'devel_wizard_node_type_user_role',
        $configuration['user_role'],
      );
    $form['user_role_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Create Taxonomy vocabulary 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\TaxonomyVocabularyMigrationSpell $migrationSpell */
    /* @noinspection PhpUnhandledExceptionInspection */
    $migrationSpell = $this
      ->spellManager
      ->createInstance(
        'devel_wizard_taxonomy_vocabulary_migration',
        $configuration['behat'],
      );
    $form['migration_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Create Taxonomy vocabulary 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\TaxonomyVocabularyBehatSpell $behatSpell */
    /* @noinspection PhpUnhandledExceptionInspection */
    $behatSpell = $this
      ->spellManager
      ->createInstance(
        'devel_wizard_taxonomy_vocabulary_behat',
        $configuration['behat'],
      );
    $form['behat_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Create Taxonomy vocabulary 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,
        ],
        'behat' => [
          'finished' => 0,
        ],
      ];

      $this->batchContext['sandbox']['finished'] = 1.0;
    }

    // @todo Translatable next_message.
    $steps = [
      'create' => [
        'spell_id' => 'devel_wizard_taxonomy_vocabulary_create',
        'next_step' => 'user_role',
        'next_message' => 'User role',
      ],
      'user_role' => [
        'spell_id' => 'devel_wizard_taxonomy_vocabulary_user_role',
        'next_step' => 'migration',
        'next_message' => 'Migration',
      ],
      'migration' => [
        'spell_id' => 'devel_wizard_taxonomy_vocabulary_migration',
        'next_step' => 'behat',
        'next_message' => 'Behat',
      ],
      'behat' => [
        'spell_id' => 'devel_wizard_taxonomy_vocabulary_behat',
        'next_step' => '_finished',
        'next_message' => 'Finished',
      ],
    ];

    $stepId = $this->batchContext['sandbox']['current_step'];
    switch ($stepId) {
      case 'create':
      case 'user_role':
      case 'migration':
      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':
        $this->batchContext['sandbox']['finished'] = 1.0;
        // @todo This will be overwritten in ::doIt().
        $this->batchContext['finished'] = 1.0;
        break;

    }

    if ($this->batchContext['finished'] === 1.0) {
      $this->postProcessBatchContext();
    }

    return $this;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc