Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / field_ui / src / Plugin / Derivative / FieldUiLocalTask.php
1 <?php
2
3 namespace Drupal\field_ui\Plugin\Derivative;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Component\Plugin\Derivative\DeriverBase;
7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8 use Drupal\Core\Routing\RouteProviderInterface;
9 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 use Drupal\Core\StringTranslation\TranslationInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides local task definitions for all entity bundles.
15  */
16 class FieldUiLocalTask extends DeriverBase implements ContainerDeriverInterface {
17   use StringTranslationTrait;
18
19   /**
20    * The route provider.
21    *
22    * @var \Drupal\Core\Routing\RouteProviderInterface
23    */
24   protected $routeProvider;
25
26   /**
27    * The entity manager
28    *
29    * @var \Drupal\Core\Entity\EntityManagerInterface
30    */
31   protected $entityManager;
32
33   /**
34    * Creates an FieldUiLocalTask object.
35    *
36    * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
37    *   The route provider.
38    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
39    *   The entity manager.
40    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
41    *   The translation manager.
42    */
43   public function __construct(RouteProviderInterface $route_provider, EntityManagerInterface $entity_manager, TranslationInterface $string_translation) {
44     $this->routeProvider = $route_provider;
45     $this->entityManager = $entity_manager;
46     $this->stringTranslation = $string_translation;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public static function create(ContainerInterface $container, $base_plugin_id) {
53     return new static(
54       $container->get('router.route_provider'),
55       $container->get('entity.manager'),
56       $container->get('string_translation')
57     );
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getDerivativeDefinitions($base_plugin_definition) {
64     $this->derivatives = [];
65
66     foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
67       if ($entity_type->get('field_ui_base_route')) {
68         $this->derivatives["overview_$entity_type_id"] = [
69           'route_name' => "entity.$entity_type_id.field_ui_fields",
70           'weight' => 1,
71           'title' => $this->t('Manage fields'),
72           'base_route' => "entity.$entity_type_id.field_ui_fields",
73         ];
74
75         // 'Manage form display' tab.
76         $this->derivatives["form_display_overview_$entity_type_id"] = [
77           'route_name' => "entity.entity_form_display.$entity_type_id.default",
78           'weight' => 2,
79           'title' => $this->t('Manage form display'),
80           'base_route' => "entity.$entity_type_id.field_ui_fields",
81         ];
82
83         // 'Manage display' tab.
84         $this->derivatives["display_overview_$entity_type_id"] = [
85           'route_name' => "entity.entity_view_display.$entity_type_id.default",
86           'weight' => 3,
87           'title' => $this->t('Manage display'),
88           'base_route' => "entity.$entity_type_id.field_ui_fields",
89         ];
90
91         // Field edit tab.
92         $this->derivatives["field_edit_$entity_type_id"] = [
93           'route_name' => "entity.field_config.{$entity_type_id}_field_edit_form",
94           'title' => $this->t('Edit'),
95           'base_route' => "entity.field_config.{$entity_type_id}_field_edit_form",
96         ];
97
98         // Field settings tab.
99         $this->derivatives["field_storage_$entity_type_id"] = [
100           'route_name' => "entity.field_config.{$entity_type_id}_storage_edit_form",
101           'title' => $this->t('Field settings'),
102           'base_route' => "entity.field_config.{$entity_type_id}_field_edit_form",
103         ];
104
105         // View and form modes secondary tabs.
106         // The same base $path for the menu item (with a placeholder) can be
107         // used for all bundles of a given entity type; but depending on
108         // administrator settings, each bundle has a different set of view
109         // modes available for customisation. So we define menu items for all
110         // view modes, and use a route requirement to determine which ones are
111         // actually visible for a given bundle.
112         $this->derivatives['field_form_display_default_' . $entity_type_id] = [
113           'title' => 'Default',
114           'route_name' => "entity.entity_form_display.$entity_type_id.default",
115           'parent_id' => "field_ui.fields:form_display_overview_$entity_type_id",
116           'weight' => -1,
117         ];
118         $this->derivatives['field_display_default_' . $entity_type_id] = [
119           'title' => 'Default',
120           'route_name' => "entity.entity_view_display.$entity_type_id.default",
121           'parent_id' => "field_ui.fields:display_overview_$entity_type_id",
122           'weight' => -1,
123         ];
124
125         // One local task for each form mode.
126         $weight = 0;
127         foreach ($this->entityManager->getFormModes($entity_type_id) as $form_mode => $form_mode_info) {
128           $this->derivatives['field_form_display_' . $form_mode . '_' . $entity_type_id] = [
129             'title' => $form_mode_info['label'],
130             'route_name' => "entity.entity_form_display.$entity_type_id.form_mode",
131             'route_parameters' => [
132               'form_mode_name' => $form_mode,
133             ],
134             'parent_id' => "field_ui.fields:form_display_overview_$entity_type_id",
135             'weight' => $weight++,
136             'cache_tags' => $this->entityManager->getDefinition('entity_form_display')->getListCacheTags(),
137           ];
138         }
139
140         // One local task for each view mode.
141         $weight = 0;
142         foreach ($this->entityManager->getViewModes($entity_type_id) as $view_mode => $form_mode_info) {
143           $this->derivatives['field_display_' . $view_mode . '_' . $entity_type_id] = [
144             'title' => $form_mode_info['label'],
145             'route_name' => "entity.entity_view_display.$entity_type_id.view_mode",
146             'route_parameters' => [
147               'view_mode_name' => $view_mode,
148             ],
149             'parent_id' => "field_ui.fields:display_overview_$entity_type_id",
150             'weight' => $weight++,
151             'cache_tags' => $this->entityManager->getDefinition('entity_view_display')->getListCacheTags(),
152           ];
153         }
154       }
155     }
156
157     foreach ($this->derivatives as &$entry) {
158       $entry += $base_plugin_definition;
159     }
160
161     return $this->derivatives;
162   }
163
164   /**
165    * Alters the base_route definition for field_ui local tasks.
166    *
167    * @param array $local_tasks
168    *   An array of local tasks plugin definitions, keyed by plugin ID.
169    */
170   public function alterLocalTasks(&$local_tasks) {
171     foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
172       if ($route_name = $entity_type->get('field_ui_base_route')) {
173         $local_tasks["field_ui.fields:overview_$entity_type_id"]['base_route'] = $route_name;
174         $local_tasks["field_ui.fields:form_display_overview_$entity_type_id"]['base_route'] = $route_name;
175         $local_tasks["field_ui.fields:display_overview_$entity_type_id"]['base_route'] = $route_name;
176         $local_tasks["field_ui.fields:field_form_display_default_$entity_type_id"]['base_route'] = $route_name;
177         $local_tasks["field_ui.fields:field_display_default_$entity_type_id"]['base_route'] = $route_name;
178
179         foreach ($this->entityManager->getFormModes($entity_type_id) as $form_mode => $form_mode_info) {
180           $local_tasks['field_ui.fields:field_form_display_' . $form_mode . '_' . $entity_type_id]['base_route'] = $route_name;
181         }
182
183         foreach ($this->entityManager->getViewModes($entity_type_id) as $view_mode => $form_mode_info) {
184           $local_tasks['field_ui.fields:field_display_' . $view_mode . '_' . $entity_type_id]['base_route'] = $route_name;
185         }
186       }
187     }
188   }
189
190 }