Commit f14f724c authored by Volodymyr Mostepaniuk's avatar Volodymyr Mostepaniuk
Browse files

Issue #3295753 by mostepaniukvm: Start custom_elements_ui implementation.

parent b7e63c58
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
name: Custom Elements UI
description: 'Provides a user interface for the Custom Elements module.'
package: Other
type: module
core_version_requirement: ^9
dependencies:
  - custom_elements:custom_elements
+6 −0
Original line number Diff line number Diff line
services:
  custom_elements_ui.subscriber:
    class: Drupal\custom_elements_ui\Routing\RouteSubscriber
    arguments: [ '@entity_type.manager' ]
    tags:
      - { name: event_subscriber }
+105 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\custom_elements_ui\Routing;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Subscriber for Custom Elements UI routes.
 *
 * @todo Ensure it's not enough to announce new routes in .routing.yml.
 */
class RouteSubscriber extends RouteSubscriberBase {

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

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

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      if ($route_name = $entity_type->get('field_ui_base_route')) {
        // Try to get the route from the current collection.
        if (!$entity_route = $collection->get($route_name)) {
          continue;
        }
        $path = $entity_route->getPath();

        $options = $entity_route->getOptions();
        if ($bundle_entity_type = $entity_type->getBundleEntityType()) {
          $options['parameters'][$bundle_entity_type] = [
            'type' => 'entity:' . $bundle_entity_type,
          ];
        }
        // @todo Needed?
        // Special parameter used to easily recognize all Custom Elements UI
        // routes.
        $options['_custom_elements_ui'] = TRUE;

        $defaults = [
          'entity_type_id' => $entity_type_id,
        ];
        // If the entity type has no bundles and it doesn't use {bundle} in its
        // admin path, use the entity type.
        if (strpos($path, '{bundle}') === FALSE) {
          $defaults['bundle'] = !$entity_type->hasKey('bundle') ? $entity_type_id : '';
        }

        // @todo Use own route callback.
        $route = new Route(
          "$path/ce",
          [
            '_entity_form' => 'entity_view_display.edit',
            '_title' => 'Manage CE',
            'view_mode_name' => 'default',
          ] + $defaults,
          // @todo Own access checking.
          ['_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display'],
          $options
        );
        $collection->add("entity.entity_ce_display.{$entity_type_id}.default", $route);

        $route = new Route(
          "$path/ce/{view_mode_name}",
          [
            '_entity_form' => 'entity_view_display.edit',
            '_title' => 'Manage CE',
          ] + $defaults,
          // @todo Own access checking.
          ['_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display'],
          $options
        );
        $collection->add("entity.entity_ce_display.{$entity_type_id}.view_mode", $route);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
    $events[RoutingEvents::ALTER] = ['onAlterRoutes', -150];
    return $events;
  }

}