Same filename in this branch
- 10 core/modules/menu_link_content/src/Entity/MenuLinkContent.php
- 10 core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php
Same filename and directory in other branches
- 8.9.x core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php
- 9 core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php
Namespace
Drupal\menu_link_content\Plugin\Menu
File
core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php
View source
<?php
namespace Drupal\menu_link_content\Plugin\Menu;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\menu_link_content\MenuLinkContentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuLinkContent extends MenuLinkBase implements ContainerFactoryPluginInterface {
protected static $entityIdsToLoad = [];
protected $overrideAllowed = [
'menu_name' => 1,
'parent' => 1,
'weight' => 1,
'expanded' => 1,
'enabled' => 1,
'title' => 1,
'description' => 1,
'route_name' => 1,
'route_parameters' => 1,
'url' => 1,
'options' => 1,
];
protected $entity;
protected $listBuilderOperations;
protected $entityTypeManager;
protected $entityRepository;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
if (!empty($this->pluginDefinition['metadata']['entity_id'])) {
$entity_id = $this->pluginDefinition['metadata']['entity_id'];
static::$entityIdsToLoad[$entity_id] = $entity_id;
}
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
$this->entityRepository = $entity_repository;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('language_manager'), $container
->get('entity.repository'));
}
public function getEntity() : MenuLinkContentInterface {
if (empty($this->entity)) {
$entity = NULL;
$storage = $this->entityTypeManager
->getStorage('menu_link_content');
if (!empty($this->pluginDefinition['metadata']['entity_id'])) {
$entity_id = $this->pluginDefinition['metadata']['entity_id'];
static::$entityIdsToLoad[$entity_id] = $entity_id;
$entities = $storage
->loadMultiple(array_values(static::$entityIdsToLoad));
$entity = $entities[$entity_id] ?? NULL;
static::$entityIdsToLoad = [];
}
if (!$entity) {
$uuid = $this
->getUuid();
$entity = $this->entityRepository
->loadEntityByUuid('menu_link_content', $uuid);
}
if (!$entity) {
throw new PluginException("Entity not found through the menu link plugin definition and could not fallback on UUID '{$uuid}'");
}
$this->entity = clone $entity;
$the_entity = $this->entityRepository
->getTranslationFromContext($this->entity);
$this->entity = $the_entity;
$this->entity
->setInsidePlugin();
}
return $this->entity;
}
public function getTitle() {
if ($this->languageManager
->isMultilingual()) {
return $this
->getEntity()
->getTitle();
}
return $this->pluginDefinition['title'];
}
public function getDescription() {
if ($this->languageManager
->isMultilingual()) {
return $this
->getEntity()
->getDescription();
}
return $this->pluginDefinition['description'];
}
public function getDeleteRoute() {
$operations = $this
->getListBuilderOperations();
return isset($operations['delete']) ? $operations['delete']['url'] : NULL;
}
public function getEditRoute() {
$operations = $this
->getListBuilderOperations();
return isset($operations['edit']) ? $operations['edit']['url'] : NULL;
}
public function getTranslateRoute() {
$operations = $this
->getListBuilderOperations();
return isset($operations['translate']) ? $operations['translate']['url'] : NULL;
}
protected function getListBuilderOperations() {
if (is_null($this->listBuilderOperations)) {
$this->listBuilderOperations = $this->entityTypeManager
->getListBuilder($this
->getEntity()
->getEntityTypeId())
->getOperations($this
->getEntity());
}
return $this->listBuilderOperations;
}
public function getOperations() : array {
return $this
->getListBuilderOperations();
}
protected function getUuid() {
return $this
->getDerivativeId();
}
public function updateLink(array $new_definition_values, $persist) {
$overrides = array_intersect_key($new_definition_values, $this->overrideAllowed);
$this->pluginDefinition = $overrides + $this
->getPluginDefinition();
if ($persist) {
$entity = $this
->getEntity();
foreach ($overrides as $key => $value) {
$entity->{$key}->value = $value;
}
$entity
->save();
}
return $this->pluginDefinition;
}
public function isDeletable() {
return TRUE;
}
public function isTranslatable() {
return $this
->getEntity()
->isTranslatable();
}
public function deleteLink() {
$this
->getEntity()
->delete();
}
}
Classes