8 changed files with 307 additions and 63 deletions
@ -0,0 +1,3 @@
|
||||
metadata_profile.display: |
||||
class: \Drupal\Core\Menu\LocalTaskDefault |
||||
deriver: \Drupal\metadata_profile\Plugin\Derivative\MetadataProfileDisplayTask |
||||
@ -0,0 +1,15 @@
|
||||
<?php |
||||
|
||||
/** |
||||
* Implements hook_entity_type_alter(). |
||||
* |
||||
* Adds the Metadata Profile tab to the entity configuration page. |
||||
*/ |
||||
function metadata_profile_entity_type_alter(array &$entity_types) { |
||||
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ |
||||
foreach ($entity_types as $entity_type) { |
||||
if ($entity_type->getBundleOf() && $entity_type->hasLinkTemplate('edit-form')) { |
||||
$entity_type->setLinkTemplate('metadata-profile', $entity_type->getLinkTemplate('edit-form') . "/metadata-profile"); |
||||
} |
||||
} |
||||
} |
||||
@ -1,21 +0,0 @@
|
||||
metadata_profile.node_profile: |
||||
path: '/admin/structure/types/manage/{content_type}/metadata_profile' |
||||
defaults: |
||||
_controller: '\Drupal\metadata_profile\Controller\MetadataProfileController::profile' |
||||
_title: 'Metadata Profile' |
||||
requirements: |
||||
_access: 'TRUE' |
||||
content_type: '[a-zA-Z_]+' |
||||
options: |
||||
parameters: |
||||
content_type: |
||||
type: string |
||||
metadata_profile.test: |
||||
path: '/test' |
||||
defaults: |
||||
_controller: '\Drupal\metadata_profile\Controller\MetadataProfileController::test' |
||||
_title: 'Metadata Profile Test' |
||||
requirements: |
||||
_access: 'TRUE' |
||||
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
services: |
||||
metadata_profile.route_subscriber: |
||||
class: Drupal\metadata_profile\Routing\RouteSubscriber |
||||
arguments: ['@entity_type.manager'] |
||||
tags: |
||||
- { name: event_subscriber } |
||||
metadata_profile.route_enhancer: |
||||
class: Drupal\metadata_profile\Routing\RouteEnhancer |
||||
arguments: ['@entity_type.manager'] |
||||
tags: |
||||
- { name: route_enhancer } |
||||
@ -0,0 +1,75 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\metadata_profile\Plugin\Derivative; |
||||
|
||||
use Drupal\Component\Plugin\Derivative\DeriverBase; |
||||
use Drupal\Core\Entity\EntityTypeManagerInterface; |
||||
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; |
||||
use Drupal\Core\StringTranslation\StringTranslationTrait; |
||||
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||
|
||||
class MetadataProfileDisplayTask extends DeriverBase implements ContainerDeriverInterface |
||||
{ |
||||
use StringTranslationTrait; |
||||
|
||||
/** |
||||
* The entity type manager. |
||||
* |
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||||
*/ |
||||
protected $entityTypeManager; |
||||
|
||||
/** |
||||
* Creates an FieldUiLocalTask 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} |
||||
*/ |
||||
public static function create(ContainerInterface $container, $base_plugin_id) { |
||||
return new static( |
||||
$container->get('entity_type.manager') |
||||
); |
||||
} |
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getDerivativeDefinitions($base_plugin_definition) { |
||||
$this->derivatives = []; |
||||
|
||||
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { |
||||
// Special handling of Taxonomy. See https://www.drupal.org/node/2822546 |
||||
if ($entity_type_id == "taxonomy_vocabulary") { |
||||
$base_route = "entity.{$entity_type_id}.overview_form"; |
||||
} |
||||
else { |
||||
$base_route = "entity.{$entity_type_id}.edit_form"; |
||||
} |
||||
if ($entity_type->hasLinkTemplate('metadata-profile')) { |
||||
$this->derivatives["$entity_type_id.metadata_profile_tab"] = [ |
||||
'route_name' => "entity.{$entity_type_id}.metadata_profile", |
||||
'title' => $this->t('Metadata Profile'), |
||||
'base_route' => $base_route, |
||||
'weight' => 100, |
||||
]; |
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
foreach ($this->derivatives as &$entry) { |
||||
$entry += $base_plugin_definition; |
||||
} |
||||
return $this->derivatives; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,61 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\metadata_profile\Routing; |
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface; |
||||
use Drupal\Core\Routing\EnhancerInterface; |
||||
use Drupal\Core\Routing\RouteObjectInterface; |
||||
use Symfony\Component\HttpFoundation\Request; |
||||
use Symfony\Component\Routing\Route; |
||||
|
||||
/** |
||||
* RouteEnhancer Class. |
||||
* |
||||
* Enhances Metadata Profile routes by adding proper information about the |
||||
* bundle name. |
||||
*/ |
||||
class RouteEnhancer implements EnhancerInterface { |
||||
|
||||
/** |
||||
* The entity type manager. |
||||
* |
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||||
*/ |
||||
protected $entityTypeManager; |
||||
|
||||
/** |
||||
* Constructs a RouteEnhancer 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} |
||||
*/ |
||||
public function enhance(array $defaults, Request $request) { |
||||
if (!$this->applies($defaults[RouteObjectInterface::ROUTE_OBJECT])) { |
||||
return $defaults; |
||||
} |
||||
|
||||
if (($bundle = $this->entityTypeManager->getDefinition($defaults['entity_type_id'])->getBundleEntityType()) && isset($defaults[$bundle])) { |
||||
// Metadata Profiles only need the actual name of the bundle they're |
||||
// dealing with, not an upcasted entity object, so provide a simple way |
||||
// for them to get it. |
||||
$defaults['bundle'] = $defaults['_raw_variables']->get($bundle); |
||||
} |
||||
|
||||
return $defaults; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function applies(Route $route) { |
||||
return ($route->hasOption('metadata_profile')); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,86 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\metadata_profile\Routing; |
||||
|
||||
use Drupal\Core\Entity\EntityTypeInterface; |
||||
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 metadata profile routes. |
||||
*/ |
||||
class RouteSubscriber extends RouteSubscriberBase { |
||||
|
||||
/** |
||||
* The entity type manager. |
||||
* |
||||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||||
*/ |
||||
protected $entityTypeManager; |
||||
|
||||
/** |
||||
* Constructs a new RouteSubscriber object. |
||||
* |
||||
* @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} |
||||
*/ |
||||
protected function alterRoutes(RouteCollection $collection) { |
||||
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { |
||||
if ($route = $this->getEntityProfileRoute($entity_type)) { |
||||
$collection->add("entity.$entity_type_id.metadata_profile", $route); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Gets the Metadata Profile route. |
||||
* |
||||
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type |
||||
* The entity type. |
||||
* |
||||
* @return \Symfony\Component\Routing\Route|null |
||||
* The generated route, if available. |
||||
*/ |
||||
protected function getEntityProfileRoute(EntityTypeInterface $entity_type) { |
||||
if ($route_load = $entity_type->getLinkTemplate('metadata-profile')) { |
||||
$entity_type_id = $entity_type->id(); |
||||
$route = new Route($route_load); |
||||
$route |
||||
->addDefaults([ |
||||
'_controller' => '\Drupal\metadata_profile\Controller\MetadataProfileController::profile', |
||||
'_title' => 'Metadata Profile', |
||||
]) |
||||
->addRequirements([ |
||||
'_permission' => 'administer content', |
||||
]) |
||||
->setOption('_admin_route', TRUE) |
||||
->setOption('parameters', [ |
||||
$entity_type_id => ['type' => 'entity:' . $entity_type_id], |
||||
]); |
||||
return $route; |
||||
} |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public static function getSubscribedEvents(): array { |
||||
$events = parent::getSubscribedEvents(); |
||||
$events[RoutingEvents::ALTER] = ['onAlterRoutes', -100]; |
||||
|
||||
return $events; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue