TaxonomyMenuMenuLink.php in Taxonomy menu 8.3
File
src/Plugin/Menu/TaxonomyMenuMenuLink.php
View source
<?php
namespace Drupal\taxonomy_menu\Plugin\Menu;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TaxonomyMenuMenuLink extends MenuLinkBase implements ContainerFactoryPluginInterface {
protected $overrideAllowed = [
'weight' => 1,
'expanded' => 1,
'enabled' => 1,
'parent' => 1,
];
protected $entityTypeManager;
protected $staticOverride;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, StaticMenuLinkOverridesInterface $static_override) {
$this->configuration = $configuration;
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
$this->entityTypeManager = $entity_type_manager;
$this->staticOverride = $static_override;
}
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('menu_link.static.overrides'));
}
public function getTitle() {
$link = $this->entityTypeManager
->getStorage('taxonomy_term')
->load($this->pluginDefinition['metadata']['taxonomy_term_id']);
$language = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
if (!empty($link) && $link
->hasTranslation($language)) {
$translation = $link
->getTranslation($language);
return $translation
->label();
}
else {
if ($link) {
return $link
->label();
}
}
return NULL;
}
public function getDescription() {
$link = $this->entityTypeManager
->getStorage('taxonomy_term')
->load($this->pluginDefinition['metadata']['taxonomy_term_id']);
$taxonomy_menu = $this->entityTypeManager
->getStorage('taxonomy_menu')
->load($this->pluginDefinition['metadata']['taxonomy_menu_id']);
$description_field_name = !empty($taxonomy_menu) ? $taxonomy_menu
->getDescriptionFieldName() : '';
$language = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
if (!empty($link) && $link
->hasTranslation($language)) {
$translation = $link
->getTranslation($language);
if (!empty($translation) && $translation
->hasField($description_field_name)) {
return $translation->{$description_field_name}->value;
}
}
elseif (!empty($link) && $link
->hasField($description_field_name)) {
return $link->{$description_field_name}->value;
}
}
public function updateLink(array $new_definition_values, $persist) {
$overrides = array_intersect_key($new_definition_values, $this->overrideAllowed);
$this->pluginDefinition = $overrides + $this->pluginDefinition;
if ($persist) {
$overrides['menu_name'] = $this->pluginDefinition['menu_name'];
$this->staticOverride
->saveOverride($this
->getPluginId(), $this->pluginDefinition);
}
return $this->pluginDefinition;
}
public function isDeletable() {
return TRUE;
}
public function deleteLink() {
}
public function isResettable() {
$override = $this->staticOverride
->loadOverride($this
->getPluginId());
return $override !== NULL && !empty($override);
}
}