TaxonomyMenuHelper.php in Zircon Profile 8
File
modules/taxonomy_menu/src/TaxonomyMenuHelper.php
View source
<?php
namespace Drupal\taxonomy_menu;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\taxonomy\TermInterface;
class TaxonomyMenuHelper {
protected $menuStorage;
protected $manager;
public function __construct(EntityManagerInterface $entity_manager, MenuLinkManagerInterface $manager) {
$this->menuStorage = $entity_manager
->getStorage('taxonomy_menu');
$this->manager = $manager;
}
public function getTermMenusByVocabulary($vid) {
return $this->menuStorage
->loadByProperties([
'vocabulary' => $vid,
]);
}
public function generateTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) {
$tax_menus = $this
->getTermMenusByVocabulary($term
->getVocabularyId());
foreach ($tax_menus as $menu) {
foreach ($menu
->getLinks([], TRUE) as $plugin_id => $plugin_def) {
if (!$rebuild_all) {
$plugin_id_parts = explode('.', $plugin_id);
$term_id = array_pop($plugin_id_parts);
if ($term
->id() != $term_id) {
continue;
}
}
if ($this->manager
->hasDefinition($plugin_id)) {
$this->manager
->updateDefinition($plugin_id, $plugin_def);
}
else {
$this->manager
->addDefinition($plugin_id, $plugin_def);
}
}
}
}
public function updateTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) {
$tax_menus = $this
->getTermMenusByVocabulary($term
->getVocabularyId());
foreach ($tax_menus as $menu) {
foreach ($menu
->getLinks([], TRUE) as $plugin_id => $plugin_def) {
if (!$rebuild_all) {
$plugin_id_explode = explode('.', $plugin_id);
$term_id = array_pop($plugin_id_explode);
if ($term
->id() != $term_id) {
continue;
}
}
$this->manager
->updateDefinition($plugin_id, $plugin_def, FALSE);
}
}
}
public function removeTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) {
$tax_menus = $this
->getTermMenusByVocabulary($term
->getVocabularyId());
foreach ($tax_menus as $menu) {
foreach (array_keys($menu
->getLinks([], TRUE)) as $plugin_id) {
if (!$rebuild_all) {
$plugin_id_parts = explode('.', $plugin_id);
$term_id = array_pop($plugin_id_parts);
if ($term
->id() != $term_id) {
continue;
}
}
$this->manager
->removeDefinition($plugin_id, FALSE);
}
}
}
}