FieldUiLocalTask.php 7.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\field_ui\Plugin\Derivative\FieldUiLocalTask.
 */

namespace Drupal\field_ui\Plugin\Derivative;

use Drupal\Core\Entity\EntityManagerInterface;
11 12
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
13
use Drupal\Core\Routing\RouteProviderInterface;
14
use Drupal\Core\StringTranslation\StringTranslationTrait;
15
use Drupal\Core\StringTranslation\TranslationInterface;
16
use Drupal\field_ui\FieldUI;
17 18 19 20 21
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides local task definitions for all entity bundles.
 */
22
class FieldUiLocalTask extends DeriverBase implements ContainerDeriverInterface {
23
  use StringTranslationTrait;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

  /**
   * The route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * The entity manager
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Creates an FieldUiLocalTask object.
   *
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
46
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
47 48
   *   The translation manager.
   */
49
  public function __construct(RouteProviderInterface $route_provider, EntityManagerInterface $entity_manager, TranslationInterface $string_translation) {
50 51
    $this->routeProvider = $route_provider;
    $this->entityManager = $entity_manager;
52
    $this->stringTranslation = $string_translation;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  }

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

  /**
   * {@inheritdoc}
   */
69
  public function getDerivativeDefinitions($base_plugin_definition) {
70 71
    $this->derivatives = array();

72
    foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
73
      if ($entity_type->get('field_ui_base_route')) {
74 75 76 77
        $field_entity_type = FieldUI::getRouteBundleEntityType($entity_type);

        $this->derivatives["overview_$field_entity_type"] = array(
          'route_name' => "entity.$field_entity_type.field_ui_fields",
78 79
          'weight' => 1,
          'title' => $this->t('Manage fields'),
80
          'base_route' => "entity.$field_entity_type.field_ui_fields",
81 82 83
        );

        // 'Manage form display' tab.
84
        $this->derivatives["form_display_overview_$field_entity_type"] = array(
85
          'route_name' => "entity.entity_form_display.$field_entity_type.default",
86 87
          'weight' => 2,
          'title' => $this->t('Manage form display'),
88
          'base_route' => "entity.$field_entity_type.field_ui_fields",
89 90 91
        );

        // 'Manage display' tab.
92
        $this->derivatives["display_overview_$field_entity_type"] = array(
93
          'route_name' => "entity.entity_view_display.$field_entity_type.default",
94 95
          'weight' => 3,
          'title' => $this->t('Manage display'),
96
          'base_route' => "entity.$field_entity_type.field_ui_fields",
97 98
        );

99 100
        // Field edit tab.
        $this->derivatives["field_edit_$entity_type_id"] = array(
101
          'route_name' => "entity.field_config.{$entity_type_id}_field_edit_form",
102
          'title' => $this->t('Edit'),
103
          'base_route' => "entity.field_config.{$entity_type_id}_field_edit_form",
104 105 106
        );

        // Field settings tab.
107
        $this->derivatives["field_storage_$entity_type_id"] = array(
108
          'route_name' => "entity.field_config.{$entity_type_id}_storage_edit_form",
109
          'title' => $this->t('Field settings'),
110
          'base_route' => "entity.field_config.{$entity_type_id}_field_edit_form",
111 112 113 114 115 116 117 118 119
        );

        // View and form modes secondary tabs.
        // The same base $path for the menu item (with a placeholder) can be
        // used for all bundles of a given entity type; but depending on
        // administrator settings, each bundle has a different set of view
        // modes available for customisation. So we define menu items for all
        // view modes, and use a route requirement to determine which ones are
        // actually visible for a given bundle.
120
        $this->derivatives['field_form_display_default_' . $field_entity_type] = array(
121
          'title' => 'Default',
122
          'route_name' => "entity.entity_form_display.$field_entity_type.default",
123
          'parent_id' => "field_ui.fields:form_display_overview_$field_entity_type",
124
          'weight' => -1,
125
        );
126
        $this->derivatives['field_display_default_' . $field_entity_type] = array(
127
          'title' => 'Default',
128
          'route_name' => "entity.entity_view_display.$field_entity_type.default",
129
          'parent_id' => "field_ui.fields:display_overview_$field_entity_type",
130
          'weight' => -1,
131 132 133 134
        );

        // One local task for each form mode.
        $weight = 0;
135
        foreach ($this->entityManager->getFormModes($entity_type_id) as $form_mode => $form_mode_info) {
136
          $this->derivatives['field_form_display_' . $form_mode . '_' . $field_entity_type] = array(
137
            'title' => $form_mode_info['label'],
138
            'route_name' => "entity.entity_form_display.$field_entity_type.form_mode",
139 140 141
            'route_parameters' => array(
              'form_mode_name' => $form_mode,
            ),
142
            'parent_id' => "field_ui.fields:form_display_overview_$field_entity_type",
143 144 145 146 147 148
            'weight' => $weight++,
          );
        }

        // One local task for each view mode.
        $weight = 0;
149
        foreach ($this->entityManager->getViewModes($entity_type_id) as $view_mode => $form_mode_info) {
150
          $this->derivatives['field_display_' . $view_mode . '_' . $field_entity_type] = array(
151
            'title' => $form_mode_info['label'],
152
            'route_name' => "entity.entity_view_display.$field_entity_type.view_mode",
153 154 155
            'route_parameters' => array(
              'view_mode_name' => $view_mode,
            ),
156
            'parent_id' => "field_ui.fields:display_overview_$field_entity_type",
157 158 159 160 161 162 163 164 165 166 167 168 169 170
            'weight' => $weight++,
          );
        }
      }
    }

    foreach ($this->derivatives as &$entry) {
      $entry += $base_plugin_definition;
    }

    return $this->derivatives;
  }

  /**
171
   * Alters the base_route definition for field_ui local tasks.
172 173 174 175 176
   *
   * @param array $local_tasks
   *   An array of local tasks plugin definitions, keyed by plugin ID.
   */
  public function alterLocalTasks(&$local_tasks) {
177
    foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
178 179
      $field_entity_type = FieldUI::getRouteBundleEntityType($entity_type);

180
      if ($route_name = $entity_type->get('field_ui_base_route')) {
181 182 183 184 185
        $local_tasks["field_ui.fields:overview_$field_entity_type"]['base_route'] = $route_name;
        $local_tasks["field_ui.fields:form_display_overview_$field_entity_type"]['base_route'] = $route_name;
        $local_tasks["field_ui.fields:display_overview_$field_entity_type"]['base_route'] = $route_name;
        $local_tasks["field_ui.fields:field_form_display_default_$field_entity_type"]['base_route'] = $route_name;
        $local_tasks["field_ui.fields:field_display_default_$field_entity_type"]['base_route'] = $route_name;
186 187

        foreach ($this->entityManager->getFormModes($entity_type_id) as $form_mode => $form_mode_info) {
188
          $local_tasks['field_ui.fields:field_form_display_' . $form_mode . '_' . $field_entity_type]['base_route'] = $route_name;
189 190
        }

191
        foreach ($this->entityManager->getViewModes($entity_type_id) as $view_mode => $form_mode_info) {
192
          $local_tasks['field_ui.fields:field_display_' . $view_mode . '_' . $field_entity_type]['base_route'] = $route_name;
193 194 195 196 197 198
        }
      }
    }
  }

}