entity_hierarchy-8.x-2.24/src/Plugin/Derivative/DynamicLocalTasks.php

src/Plugin/Derivative/DynamicLocalTasks.php
<?php

declare(strict_types=1);

namespace Drupal\entity_hierarchy\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Generates children tab for relevant entities.
 */
class DynamicLocalTasks extends DeriverBase implements ContainerDeriverInterface {

  use StringTranslationTrait;

  /**
   * Creates the DynamicLocalTasks object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   Entity field manager.
   */
  public function __construct(
    readonly protected EntityTypeManagerInterface $entityTypeManager,
    readonly protected EntityFieldManagerInterface $entityFieldManager,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static(
      $container->get('entity_type.manager'),
      $container->get('entity_field.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->derivatives = [];
    foreach ($this->entityFieldManager->getFieldMapByFieldType('entity_reference_hierarchy') as $entity_type_id => $fields) {
      /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
      $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
      if (!$entity_type->hasLinkTemplate('canonical') || isset($this->derivatives["$entity_type_id.entity_hierarchy_reorder"])) {
        continue;
      }
      $this->derivatives["$entity_type_id.entity_hierarchy_reorder"] = [
        'route_name' => "entity.$entity_type_id.entity_hierarchy_reorder",
        'title' => $this->t('Children'),
        'base_route' => "entity.$entity_type_id.canonical",
        'weight' => 30,
      ] + $base_plugin_definition;
    }
    return $this->derivatives;
  }

}

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

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