View source
<?php
namespace Drupal\menu_link_content\Plugin\Menu;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
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 Symfony\Component\DependencyInjection\ContainerInterface;
class MenuLinkContent extends MenuLinkBase implements ContainerFactoryPluginInterface {
use DeprecatedServicePropertyTrait;
protected $deprecatedProperties = [
'entityManager' => 'entity.manager',
];
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 $entityTypeManager;
protected $entityRepository;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, EntityRepositoryInterface $entity_repository = NULL) {
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;
if (!$entity_repository) {
@trigger_error('Calling MenuLinkContent::__construct() with the $entity_repository argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$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'));
}
protected function getEntity() {
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 = isset($entities[$entity_id]) ? $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() {
return $this
->getEntity()
->toUrl('delete-form');
}
public function getEditRoute() {
return $this
->getEntity()
->toUrl();
}
public function getTranslateRoute() {
return $this
->getEntity()
->toUrl('drupal:content-translation-overview');
}
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();
}
}