Namespace
Drupal\devel
File
-
src/EntityTypeInfo.php
View source
<?php
namespace Drupal\devel;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityTypeInfo implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $currentUser;
public function __construct(AccountInterface $current_user) {
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container) {
return new static($container->get('current_user'));
}
public function entityTypeAlter(array &$entity_types) {
foreach ($entity_types as $entity_type_id => $entity_type) {
if (($entity_type->getFormClass('default') || $entity_type->getFormClass('edit')) && $entity_type->hasLinkTemplate('edit-form')) {
$entity_link = $entity_type->getLinkTemplate('edit-form');
$this->setEntityTypeLinkTemplate($entity_type, $entity_link, 'devel-load', "/devel/{$entity_type_id}", $entity_link);
}
if ($entity_type->hasViewBuilderClass() && $entity_type->hasLinkTemplate('canonical')) {
$entity_link = $entity_type->getLinkTemplate('canonical');
$this->setEntityTypeLinkTemplate($entity_type, $entity_link, 'devel-render', "/devel/render/{$entity_type_id}", 'canonical');
}
if ($entity_type->hasLinkTemplate('devel-render') || $entity_type->hasLinkTemplate('devel-load')) {
$entity_link = $entity_type->getLinkTemplate('edit-form');
if (empty($entity_link)) {
$entity_link = $entity_type->getLinkTemplate('canonical');
}
$this->setEntityTypeLinkTemplate($entity_type, $entity_link, 'devel-definition', "/devel/definition/{$entity_type_id}");
}
}
}
protected function setEntityTypeLinkTemplate(EntityTypeInterface $entity_type, $entity_link, $devel_link_key, $base_path) {
$path_parts = $this->getPathParts($entity_link);
$entity_type->setLinkTemplate($devel_link_key, $base_path . $path_parts);
}
protected function getPathParts($entity_path) {
$path = '';
if (preg_match_all('/{\\w*}/', $entity_path, $matches)) {
foreach ($matches[0] as $match) {
$path .= "/{$match}";
}
}
return $path;
}
public function entityOperation(EntityInterface $entity) {
$operations = [];
if ($this->currentUser
->hasPermission('access devel information')) {
if ($entity->hasLinkTemplate('devel-load')) {
$operations['devel'] = [
'title' => $this->t('Devel'),
'weight' => 100,
'url' => $entity->toUrl('devel-load'),
];
}
elseif ($entity->hasLinkTemplate('devel-render')) {
$operations['devel'] = [
'title' => $this->t('Devel'),
'weight' => 100,
'url' => $entity->toUrl('devel-render'),
];
}
}
return $operations;
}
}
Classes
| Title |
Deprecated |
Summary |
| EntityTypeInfo |
|
Manipulates entity type information. |