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

src/Plugin/DevelWizard/Spell/TaxonomyVocabularyCreateSpell.php
<?php

declare(strict_types=1);

namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ModuleInstallerInterface;
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\ShellProcessFactoryInterface;
use Drupal\devel_wizard\Spell\SpellTraitPackageManager;
use Drupal\devel_wizard\Utils;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\VocabularyInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

#[DevelWizardSpell(
  id: 'devel_wizard_taxonomy_vocabulary_create',
  category: new TranslatableMarkup('Taxonomy'),
  label: new TranslatableMarkup('Taxonomy vocabulary - Create'),
  description: new TranslatableMarkup('Creates a new Taxonomy vocabulary without any extra.'),
  tags: [
    'bundle' => new TranslatableMarkup('Bundle'),
    'config_entity' => new TranslatableMarkup('Config entity'),
    'taxonomy_vocabulary' => new TranslatableMarkup('Taxonomy vocabulary'),
    'site_build' => new TranslatableMarkup('Site build'),
  ],
)]
class TaxonomyVocabularyCreateSpell extends ConfigEntitySpellBase {

  use SpellTraitPackageManager;

  protected string $provider = 'taxonomy';

  protected string $configEntityTypeId = 'taxonomy_vocabulary';

  protected string $contentEntityTypeId = 'taxonomy_term';

  /**
   * {@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('entity_type.manager'),
      $container->get('devel_wizard.shell_process_factory'),
      $container->get('module_installer'),
      $container->get('extension.list.module'),
    );
  }

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    MessengerInterface $messenger,
    LoggerInterface $logger,
    TranslationInterface $stringTranslation,
    Utils $utils,
    ConfigFactoryInterface $configFactory,
    EntityTypeManagerInterface $entityTypeManager,
    ShellProcessFactoryInterface $shellProcessFactory,
    ModuleInstallerInterface $moduleInstaller,
    ModuleExtensionList $moduleList,
  ) {
    $this->entityTypeManager = $entityTypeManager;
    $this->shellProcessFactory = $shellProcessFactory;
    $this->moduleInstaller = $moduleInstaller;
    $this->moduleList = $moduleList;
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $messenger,
      $logger,
      $stringTranslation,
      $utils,
      $configFactory,
    );
  }

  public function defaultConfiguration() {
    return [
      'values' => [
        'vid' => '',
        'name' => '',
        'description' => 'Useless description',
        'weight' => 1,
      ],
    ];
  }

  public function populateCalculatedConfigurationValues(): static {
    parent::populateCalculatedConfigurationValues();
    if (empty($this->configuration['values']['vid'])) {
      throw new \InvalidArgumentException('vid is required');
    }

    if (empty($this->configuration['values']['name'])) {
      $this->configuration['values']['name'] = $this->configuration['values']['vid'];
    }

    return $this;
  }

  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $configuration = $this->getConfiguration();

    $form['values'] = [
      '#tree' => TRUE,
      'vid' => [
        '#type' => 'machine_name',
        '#title' => $this->t('Machine-name of the Taxonomy Vocabulary'),
        '#default_value' => $configuration['values']['vid'],
        '#maxlength' => 64,
        '#description' => $this->t('It must only contain lowercase letters, numbers, and underscores.'),
        '#machine_name' => [
          'exists' => [static::class, 'taxonomyVocabularyLoad'],
          'standalone' => TRUE,
        ],
        '#autocomplete_route_name' => 'devel_wizard.autocomplete.config_entity_instance',
        '#autocomplete_route_parameters' => [
          'entityTypeId' => $this->configEntityTypeId,
        ],
      ],
    ];

    return $form;
  }

  public static function taxonomyVocabularyLoad(string $vid): ?VocabularyInterface {
    return class_exists(Vocabulary::class) ?
      Vocabulary::load($vid)
      : NULL;
  }

  /**
   * {@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);
  }

  public function getRequiredModules(): array {
    $modules = parent::getRequiredModules();
    $modules['taxonomy'] = TRUE;

    return $modules;
  }

  /**
   * @throws \Drupal\Core\Entity\EntityStorageException
   * @throws \Drupal\Core\Entity\EntityMalformedException
   */
  protected function doIt(): static {
    if (!isset($this->batchContext['sandbox']['current_step'])) {
      $this->batchContext['sandbox']['current_step'] = 'required_modules_install';
    }

    $configuration = $this->getConfiguration();
    switch ($this->batchContext['sandbox']['current_step']) {
      case 'required_modules_install':
        $requiredModules = array_keys($this->getRequiredModules(), TRUE);
        if ($requiredModules) {
          foreach ($requiredModules as $moduleName) {
            $this->ensureModuleInstalled($moduleName);
          }
          // @todo Clear the cache only when it is necessary.
          drupal_flush_all_caches();
        }

        $this->batchContext['message'] = $this->t(
          'Create Taxonomy vocabulary: @entity.label',
          [
            '@entity.label' => $configuration['values']['vid'],
          ],
        );
        $this->batchContext['sandbox']['current_step'] = 'taxonomy_vocabulary_create';
        $this->batchContext['sandbox']['finished'] = 0.5;
        break;

      case 'taxonomy_vocabulary_create':
        $this->createConfigInstance($this->configuration['values']);
        $this->batchContext['sandbox']['current_step'] = '_finished';
        $this->batchContext['sandbox']['finished'] = 1.0;
        break;

      case '_finished':
        $this->batchContext['sandbox']['finished'] = 1.0;
        break;

    }

    return $this;
  }

}

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

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