MenuLinkField.php in Menu Link (Field) 2.0.x
File
src/Plugin/Menu/MenuLinkField.php
View source
<?php
namespace Drupal\menu_link\Plugin\Menu;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuLinkField extends MenuLinkBase implements ContainerFactoryPluginInterface {
protected $entity;
protected $entityTypeManager;
protected $entityRepository;
protected $languageManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_manager, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_manager;
$this->entityRepository = $entity_repository;
$this->languageManager = $language_manager;
}
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('entity.repository'), $container
->get('language_manager'));
}
public function getTitle() {
return $this
->getProperty('title');
}
public function getUrlObject($title_attribute = TRUE) {
$options = $this
->getOptions();
if ($title_attribute && ($description = $this
->getDescription())) {
$options['attributes']['title'] = $description;
}
return $this
->getEntity()
->toUrl('canonical', $options);
}
protected function getProperty($property) {
if (!empty($this->pluginDefinition['metadata']['translatable']) && $this->languageManager
->isMultilingual()) {
$entity = $this
->getEntity();
$field_name = $this
->getFieldName();
if ($property_value = $entity
->get($field_name)->{$property}) {
return $property_value;
}
return $entity
->label();
}
return $this->pluginDefinition[$property];
}
public function getDescription() {
return $this
->getProperty('description');
}
public function getWeight() {
$weight = $this
->getProperty('weight');
if (!$weight || !is_int($weight)) {
return parent::getWeight();
}
return $weight;
}
public function updateLink(array $new_definition_values, $persist) {
$field_name = $this
->getFieldName();
$this->pluginDefinition = $new_definition_values + $this
->getPluginDefinition();
if ($persist) {
$updated = [];
foreach ($new_definition_values as $key => $value) {
$field = $this
->getEntity()->{$field_name};
if (isset($field->{$key})) {
$field->{$key} = $value;
$updated[] = $key;
}
}
if ($updated) {
$entity = $this
->getEntity();
$entity
->setSyncing(TRUE);
$entity
->isDefaultRevision(TRUE);
$entity
->save();
}
}
return $this->pluginDefinition;
}
public function getEntity() {
if (empty($this->entity)) {
$entity_type_id = $this->pluginDefinition['metadata']['entity_type_id'];
$entity_id = $this->pluginDefinition['metadata']['entity_id'];
$entity = $this->entityTypeManager
->getStorage($entity_type_id)
->load($entity_id);
if ($entity instanceof TranslatableInterface && $this->pluginDefinition['metadata']['langcode'] !== LanguageInterface::LANGCODE_NOT_SPECIFIED && $entity
->hasTranslation($this->pluginDefinition['metadata']['langcode'])) {
$this->entity = $entity
->getTranslation($this->pluginDefinition['metadata']['langcode']);
}
else {
$this->entity = $this->entityRepository
->getTranslationFromContext($entity);
}
}
return $this->entity;
}
public function isDeletable() {
return TRUE;
}
public function deleteLink() {
$entity = $this
->getEntity();
$field_name = $this
->getFieldName();
$field_item_list = $entity
->get($field_name);
$field_item_list->title = '';
$field_item_list->description = '';
$field_item_list->menu_name = '';
$field_item_list->parent = '';
$this->entity
->save();
}
protected function getFieldName() {
return $this
->getPluginDefinition()['metadata']['field_name'];
}
}
Classes
Name |
Description |
MenuLinkField |
Provides a menu link plugin for field-based menu links. |