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

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

declare(strict_types=1);

namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;

use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Template\TwigEnvironment;
use Drupal\devel_wizard\Attribute\DevelWizardSpell;
use Drupal\devel_wizard\Spell\SpellTraitPackageManager;
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\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\String\UnicodeString;

#[DevelWizardSpell(
  id: 'devel_wizard_route_admin_menu_block_page',
  category: new TranslatableMarkup('Code'),
  label: new TranslatableMarkup('Page - Admin menu block'),
  description: new TranslatableMarkup('Generates a new systemAdminMenuBlockPage.'),
  tags: [
    'code' => new TranslatableMarkup('Code'),
    'route' => new TranslatableMarkup('Route'),
    'menu_link' => new TranslatableMarkup('Menu link'),
    'page' => new TranslatableMarkup('Page'),
    'admin' => new TranslatableMarkup('Admin'),
  ],
)]
class AdminMenuBlockPageSpell extends SpellBase implements
  PluginFormInterface,
  ConfigurableInterface,
  ContainerFactoryPluginInterface {

  use SpellTraitPackageManager;

  protected ModuleExtensionList $moduleList;

  protected TwigEnvironment $twig;

  protected UrlMatcherInterface $router;

  protected MenuLinkManagerInterface $menuLinkManager;

  protected Filesystem $fs;

  /**
   * {@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('extension.list.module'),
      $container->get('twig'),
      $container->get('router.no_access_checks'),
      $container->get('plugin.manager.menu.link'),
      new Filesystem(),
    );
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'module' => '',
      'parentPath' => '/admin/config',
      'group' => '',
    ];
  }

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

    $this->configuration['moduleDir'] = $this->moduleList->getPath($this->configuration['module']);
    $this->configuration['moduleDash'] = str_replace('_', '-', $this->configuration['module']);
    $this->configuration['groupDash'] = str_replace('_', '-', $this->configuration['group']);

    $match = $this->router->match($this->configuration['parentPath']);
    $links = $this->menuLinkManager->loadLinksByRoute($match['_route']);
    /** @var \Drupal\Core\Menu\MenuLinkDefault $parentMenuLink */
    $parentMenuLink = reset($links) ?: NULL;

    $this->configuration['routing'] = [
      'filePath' => Path::join(
        $this->configuration['moduleDir'],
        "{$this->configuration['module']}.routing.yml",
      ),
      'id' => sprintf(
        '%s.%s_%s',
        $this->configuration['module'],
        (new UnicodeString($this->configuration['parentPath']))
          ->trimPrefix('/')
          ->replace('/', '_')
          ->toString(),
        $this->configuration['group'],
      ),
      'definition' => [
        'path' => sprintf(
          '%s/%s-%s',
          $this->configuration['parentPath'],
          $this->configuration['module'],
          $this->configuration['group'],
        ),
        'defaults' => [
          '_title' => $this->configuration['group'],
          '_controller' => '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage',
        ],
        'requirements' => [
          '_permission' => 'access administration pages',
        ],
      ],
    ];

    $this->configuration['linksMenu'] = [
      'filePath' => Path::join(
        $this->configuration['moduleDir'],
        "{$this->configuration['module']}.links.menu.yml",
      ),
      'id' => $this->configuration['routing']['id'],
      'definition' => [
        'route_name' => $this->configuration['routing']['id'],
        'parent' => $parentMenuLink->getPluginId(),
        'title' => $this->configuration['group'],
        'description' => 'My description',
        'weight' => 10,
      ],
    ];

    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $conf = $this->getConfiguration();

    $form['module'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Module'),
      '#default_value' => $conf['module'],
      '#autocomplete_route_name' => 'devel_wizard.autocomplete.module',
    ];

    $form['parentPath'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Parent path'),
      '#default_value' => $conf['parentPath'],
      '#description' => $this->t('For example: <code>/admin/config</code>, <code>/admin/structure</code>, <code>/admin/reports</code>'),
    ];

    $form['group'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Group'),
      '#default_value' => $conf['group'],
      '#description' => $this->t('My custom machine-name'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValue($form['#parents'], []);
    $parentPathInputName = implode(
      '][',
      array_merge($form['#parents'], ['parentPath']),
    );
    try {
      $match = $this->router->match($values['parentPath']);
      $links = $this->menuLinkManager->loadLinksByRoute($match['_route']);
      if (!$links) {
        $form_state->setErrorByName($parentPathInputName, $this->t('There is no menu link which points to the given parent path'));
      }
    }
    catch (ResourceNotFoundException) {
      $form_state->setErrorByName($parentPathInputName, $this->t('The given parent path is not exists'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValue($form['#parents'], []);
    $this->setConfiguration($values);
  }

  protected function doIt(): static {
    $this
      ->doItRouting()
      ->doItLinksMenu();
    drupal_flush_all_caches();

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

    return $this;
  }

  protected function doItRouting(): static {
    $conf = $this->getConfiguration();

    $this->utils->ymlFileReplace(
      $conf['routing']['filePath'],
      [
        $conf['routing']['id'] => $conf['routing']['definition'],
      ],
    );
    $this->messageFilesystemEntryUpdate($conf['routing']['filePath']);

    return $this;
  }

  protected function doItLinksMenu(): static {
    $conf = $this->getConfiguration();

    $this->utils->ymlFileReplace(
      $conf['linksMenu']['filePath'],
      [
        $conf['linksMenu']['id'] => $conf['linksMenu']['definition'],
      ],
    );
    $this->messageFilesystemEntryUpdate($conf['linksMenu']['filePath']);

    return $this;
  }

}

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

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