LayoutBuilderLocalTaskDeriver.php 5.37 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Drupal\layout_builder\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\Entity\FieldableEntityInterface;
9 10 11 12 13 14 15
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides local task definitions for the layout builder user interface.
 *
16 17
 * @todo Remove this in https://www.drupal.org/project/drupal/issues/2936655.
 *
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 * @internal
 */
class LayoutBuilderLocalTaskDeriver extends DeriverBase implements ContainerDeriverInterface {

  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new LayoutBuilderLocalTaskDeriver.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
54
    foreach ($this->getEntityTypesForOverrides() as $entity_type_id => $entity_type) {
55
      // Overrides.
56 57
      $this->derivatives["layout_builder.overrides.$entity_type_id.view"] = $base_plugin_definition + [
        'route_name' => "layout_builder.overrides.$entity_type_id.view",
58 59 60 61 62
        'weight' => 15,
        'title' => $this->t('Layout'),
        'base_route' => "entity.$entity_type_id.canonical",
        'cache_contexts' => ['layout_builder_is_active:' . $entity_type_id],
      ];
63 64
      $this->derivatives["layout_builder.overrides.$entity_type_id.save"] = $base_plugin_definition + [
        'route_name' => "layout_builder.overrides.$entity_type_id.save",
65
        'title' => $this->t('Save Layout'),
66
        'parent_id' => "layout_builder_ui:layout_builder.overrides.$entity_type_id.view",
67 68
        'cache_contexts' => ['layout_builder_is_active:' . $entity_type_id],
      ];
69 70
      $this->derivatives["layout_builder.overrides.$entity_type_id.cancel"] = $base_plugin_definition + [
        'route_name' => "layout_builder.overrides.$entity_type_id.cancel",
71
        'title' => $this->t('Cancel Layout'),
72
        'parent_id' => "layout_builder_ui:layout_builder.overrides.$entity_type_id.view",
73 74 75
        'weight' => 5,
        'cache_contexts' => ['layout_builder_is_active:' . $entity_type_id],
      ];
76 77
      // @todo This link should be conditionally displayed, see
      //   https://www.drupal.org/node/2917777.
78 79
      $this->derivatives["layout_builder.overrides.$entity_type_id.revert"] = $base_plugin_definition + [
        'route_name' => "layout_builder.overrides.$entity_type_id.revert",
80
        'title' => $this->t('Revert to defaults'),
81
        'parent_id' => "layout_builder_ui:layout_builder.overrides.$entity_type_id.view",
82 83 84
        'weight' => 10,
        'cache_contexts' => ['layout_builder_is_active:' . $entity_type_id],
      ];
85
    }
86

87
    foreach ($this->getEntityTypesForDefaults() as $entity_type_id => $entity_type) {
88
      // Defaults.
89 90
      $this->derivatives["layout_builder.defaults.$entity_type_id.view"] = $base_plugin_definition + [
        'route_name' => "layout_builder.defaults.$entity_type_id.view",
91
        'title' => $this->t('Manage layout'),
92
        'base_route' => "layout_builder.defaults.$entity_type_id.view",
93
      ];
94 95
      $this->derivatives["layout_builder.defaults.$entity_type_id.save"] = $base_plugin_definition + [
        'route_name' => "layout_builder.defaults.$entity_type_id.save",
96
        'title' => $this->t('Save Layout'),
97
        'parent_id' => "layout_builder_ui:layout_builder.defaults.$entity_type_id.view",
98
      ];
99 100
      $this->derivatives["layout_builder.defaults.$entity_type_id.cancel"] = $base_plugin_definition + [
        'route_name' => "layout_builder.defaults.$entity_type_id.cancel",
101 102
        'title' => $this->t('Cancel Layout'),
        'weight' => 5,
103
        'parent_id' => "layout_builder_ui:layout_builder.defaults.$entity_type_id.view",
104
      ];
105 106 107 108 109 110
    }

    return $this->derivatives;
  }

  /**
111 112 113 114 115 116 117 118 119 120 121 122 123
   * Returns an array of entity types relevant for defaults.
   *
   * @return \Drupal\Core\Entity\EntityTypeInterface[]
   *   An array of entity types.
   */
  protected function getEntityTypesForDefaults() {
    return array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $entity_type) {
      return $entity_type->entityClassImplements(FieldableEntityInterface::class) && $entity_type->hasViewBuilderClass() && $entity_type->get('field_ui_base_route');
    });
  }

  /**
   * Returns an array of entity types relevant for overrides.
124 125 126 127
   *
   * @return \Drupal\Core\Entity\EntityTypeInterface[]
   *   An array of entity types.
   */
128
  protected function getEntityTypesForOverrides() {
129
    return array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $entity_type) {
130
      return $entity_type->entityClassImplements(FieldableEntityInterface::class) && $entity_type->hasViewBuilderClass() && $entity_type->hasLinkTemplate('canonical');
131 132 133 134
    });
  }

}