RouteSubscriber.php 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\field_ui\Routing\RouteSubscriber.
 */

namespace Drupal\field_ui\Routing;

10
use Drupal\Core\Entity\EntityManagerInterface;
11
use Drupal\Core\Routing\RouteSubscriberBase;
12
use Drupal\Core\Routing\RoutingEvents;
13 14
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
15 16 17 18

/**
 * Subscriber for Field UI routes.
 */
19
class RouteSubscriber extends RouteSubscriberBase {
20 21 22 23

  /**
   * The entity type manager
   *
24
   * @var \Drupal\Core\Entity\EntityManagerInterface
25 26 27 28 29 30
   */
  protected $manager;

  /**
   * Constructs a RouteSubscriber object.
   *
31
   * @param \Drupal\Core\Entity\EntityManagerInterface $manager
32 33
   *   The entity type manager.
   */
34
  public function __construct(EntityManagerInterface $manager) {
35 36 37 38 39 40
    $this->manager = $manager;
  }

  /**
   * {@inheritdoc}
   */
41
  protected function alterRoutes(RouteCollection $collection) {
42
    foreach ($this->manager->getDefinitions() as $entity_type_id => $entity_type) {
43
      $defaults = array();
44
      if ($entity_type->isFieldable() && $entity_type->hasLinkTemplate('admin-form')) {
45
        // Try to get the route from the current collection.
46
        if (!$entity_route = $collection->get($entity_type->getLinkTemplate('admin-form'))) {
47
          continue;
48 49
        }
        $path = $entity_route->getPath();
50 51

        $route = new Route(
52
          "$path/fields/{field_instance_config}",
53 54 55 56
          array(
            '_form' => '\Drupal\field_ui\Form\FieldInstanceEditForm',
            '_title_callback' => '\Drupal\field_ui\Form\FieldInstanceEditForm::getTitle',
          ),
57
          array('_entity_access' => 'field_instance_config.update')
58
        );
59
        $collection->add("field_ui.instance_edit_$entity_type_id", $route);
60 61

        $route = new Route(
62
          "$path/fields/{field_instance_config}/field",
63
          array('_form' => '\Drupal\field_ui\Form\FieldEditForm'),
64
          array('_entity_access' => 'field_instance_config.update')
65
        );
66
        $collection->add("field_ui.field_edit_$entity_type_id", $route);
67 68

        $route = new Route(
69 70
          "$path/fields/{field_instance_config}/delete",
          array('_entity_form' => 'field_instance_config.delete'),
71
          array('_entity_access' => 'field_instance_config.delete')
72
        );
73
        $collection->add("field_ui.delete_$entity_type_id", $route);
74 75

        // If the entity type has no bundles, use the entity type.
76 77 78
        $defaults['entity_type_id'] = $entity_type_id;
        if (!$entity_type->hasKey('bundle')) {
          $defaults['bundle'] = $entity_type_id;
79 80 81
        }
        $route = new Route(
          "$path/fields",
82 83 84 85
          array(
            '_form' => '\Drupal\field_ui\FieldOverview',
            '_title' => 'Manage fields',
          ) + $defaults,
86
          array('_permission' => 'administer ' . $entity_type_id . ' fields')
87
        );
88
        $collection->add("field_ui.overview_$entity_type_id", $route);
89

90 91
        $route = new Route(
          "$path/form-display",
92 93 94 95
          array(
            '_form' => '\Drupal\field_ui\FormDisplayOverview',
            '_title' => 'Manage form display',
          ) + $defaults,
96
          array('_field_ui_form_mode_access' => 'administer ' . $entity_type_id . ' form display')
97
        );
98
        $collection->add("field_ui.form_display_overview_$entity_type_id", $route);
99

100 101 102 103
        $route = new Route(
          "$path/form-display/{form_mode_name}",
          array(
            '_form' => '\Drupal\field_ui\FormDisplayOverview',
104
            '_title' => 'Manage form display',
105
          ) + $defaults,
106
          array('_field_ui_form_mode_access' => 'administer ' . $entity_type_id . ' form display')
107
        );
108
        $collection->add("field_ui.form_display_overview_form_mode_$entity_type_id", $route);
109

110 111
        $route = new Route(
          "$path/display",
112 113 114 115
          array(
            '_form' => '\Drupal\field_ui\DisplayOverview',
            '_title' => 'Manage display',
          ) + $defaults,
116
          array('_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display')
117
        );
118
        $collection->add("field_ui.display_overview_$entity_type_id", $route);
119

120 121 122 123
        $route = new Route(
          "$path/display/{view_mode_name}",
          array(
            '_form' => '\Drupal\field_ui\DisplayOverview',
124
            '_title' => 'Manage display',
125
          ) + $defaults,
126
          array('_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display')
127
        );
128
        $collection->add("field_ui.display_overview_view_mode_$entity_type_id", $route);
129 130 131 132
      }
    }
  }

133 134 135 136 137
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
138
    $events[RoutingEvents::ALTER] = array('onAlterRoutes', -100);
139 140 141
    return $events;
  }

142
}