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

src/Plugin/DevelWizard/Spell/EntityBundleClassSpell.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\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Template\TwigEnvironment;
use Drupal\devel_wizard\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\String\UnicodeString;

/**
 * @todo Implement.
 */
class EntityBundleClassSpell extends SpellBase implements
  PluginFormInterface,
  ContainerFactoryPluginInterface {

  protected EntityTypeManagerInterface $entityTypeManager;

  protected ModuleExtensionList $moduleList;

  protected TwigEnvironment $twig;

  protected Filesystem $fs;

  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('extension.list.module'),
      $container->get('twig'),
      new Filesystem(),
    );
  }

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    MessengerInterface $messenger,
    LoggerInterface $logger,
    TranslationInterface $stringTranslation,
    Utils $utils,
    ConfigFactoryInterface $configFactory,
    EntityTypeManagerInterface $entityTypeManager,
    ModuleExtensionList $moduleList,
    TwigEnvironment $twig,
    Filesystem $fs,
  ) {
    $this->entityTypeManager = $entityTypeManager;
    $this->moduleList = $moduleList;
    $this->twig = $twig;
    $this->fs = $fs;

    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $messenger,
      $logger,
      $stringTranslation,
      $utils,
      $configFactory,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'module' => '',
      'entityTypeId' => '',
      'bundleId' => '',
    ];
  }

  protected function populateCalculatedConfigurationValues(): static {
    parent::populateCalculatedConfigurationValues();
    $conf =& $this->configuration;

    if (!$conf['module']) {
      throw new \InvalidArgumentException('module is required');
    }

    if (!$conf['entityTypeId']) {
      throw new \InvalidArgumentException('entityTypeId is required');
    }

    if (!$conf['bundleId']) {
      throw new \InvalidArgumentException('bundleId is required');
    }

    $conf['moduleDir'] = $this->moduleList->getPath($conf['module']);

    $conf['moduleFilePath'] = Path::join(
      $conf['moduleDir'],
      "{$conf['module']}.module",
    );

    $conf['entityTypeIdUpperCamel'] = (new UnicodeString("a_{$conf['entityTypeId']}"))
      ->camel()
      ->trimPrefix('a')
      ->toString();

    $conf['bundleIdUpperCamel'] = (new UnicodeString("a_{$conf['bundleId']}"))
      ->camel()
      ->trimPrefix('a')
      ->toString();

    $conf['class'] = [
      'namespace' => "Drupal\\{$conf['module']}\\ContentEntityBundle",
      'class' => "{$conf['entityTypeIdUpperCamel']}{$conf['bundleIdUpperCamel']}",
    ];
    $conf['class']['classFqn'] = "{$conf['class']['namespace']}\\{$conf['class']['class']}";
    $conf['class']['filePath'] = Path::join(
      $conf['moduleDir'],
      'src',
      'ContentEntityBundle',
      "{$conf['class']['class']}.php",
    );

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    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 {
    return $this;
  }

}

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

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