You are here

class TaxonomyMenuHelper in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 modules/taxonomy_menu/src/TaxonomyMenuHelper.php \Drupal\taxonomy_menu\TaxonomyMenuHelper

Class TaxonomyMenu.

@package Drupal\taxonomy_menu

Hierarchy

Expanded class hierarchy of TaxonomyMenuHelper

1 string reference to 'TaxonomyMenuHelper'
taxonomy_menu.services.yml in modules/taxonomy_menu/taxonomy_menu.services.yml
modules/taxonomy_menu/taxonomy_menu.services.yml
1 service uses TaxonomyMenuHelper
taxonomy_menu.helper in modules/taxonomy_menu/taxonomy_menu.services.yml
Drupal\taxonomy_menu\TaxonomyMenuHelper

File

modules/taxonomy_menu/src/TaxonomyMenuHelper.php, line 19
Contains Drupal\taxonomy_menu\Controller\TaxonomyMenu.

Namespace

Drupal\taxonomy_menu
View source
class TaxonomyMenuHelper {

  /**
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $menuStorage;

  /**
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   */
  protected $manager;
  public function __construct(EntityManagerInterface $entity_manager, MenuLinkManagerInterface $manager) {
    $this->menuStorage = $entity_manager
      ->getStorage('taxonomy_menu');
    $this->manager = $manager;
  }

  /**
   * A reverse lookup of a taxonomy term menus by vocabulary.
   *
   * @return \Drupal\taxonomy_menu\TaxonomyMenuInterface[]
   */
  public function getTermMenusByVocabulary($vid) {
    return $this->menuStorage
      ->loadByProperties([
      'vocabulary' => $vid,
    ]);
  }

  /**
   * Create menu entries associate with the vocabulary of this term.
   *
   * @param \Drupal\taxonomy\TermInterface $term
   */
  public function generateTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) {

    // Load relevant taxonomy menus.
    $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);
        }
      }
    }
  }

  /**
   * Update menu entries associate with the vocabulary of this term.
   *
   * @param \Drupal\taxonomy\TermInterface $term
   */
  public function updateTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) {

    // Load relevant taxonomy menus.
    $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);
      }
    }
  }

  /**
   * Remove menu entries associate with the vocabulary of this term.
   *
   * @param \Drupal\taxonomy\TermInterface $term
   */
  public function removeTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) {

    // Load relevant taxonomy menus.
    $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);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TaxonomyMenuHelper::$manager protected property
TaxonomyMenuHelper::$menuStorage protected property
TaxonomyMenuHelper::generateTaxonomyMenuEntries public function Create menu entries associate with the vocabulary of this term.
TaxonomyMenuHelper::getTermMenusByVocabulary public function A reverse lookup of a taxonomy term menus by vocabulary.
TaxonomyMenuHelper::removeTaxonomyMenuEntries public function Remove menu entries associate with the vocabulary of this term.
TaxonomyMenuHelper::updateTaxonomyMenuEntries public function Update menu entries associate with the vocabulary of this term.
TaxonomyMenuHelper::__construct public function