You are here

class TaxonomyMenuHelper in Taxonomy menu 8.3

Class TaxonomyMenu.

@package Drupal\taxonomy_menu

Hierarchy

Expanded class hierarchy of TaxonomyMenuHelper

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

File

src/TaxonomyMenuHelper.php, line 14

Namespace

Drupal\taxonomy_menu
View source
class TaxonomyMenuHelper {

  /**
   * Taxonomy Menu storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $menuStorage;

  /**
   * Menu Link Manager.
   *
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   */
  protected $manager;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $manager
   *   The menu link manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, MenuLinkManagerInterface $manager) {
    $this->menuStorage = $entity_type_manager
      ->getStorage('taxonomy_menu');
    $this->manager = $manager;
  }

  /**
   * A reverse lookup of a taxonomy term menus by vocabulary.
   *
   * @param string $vid
   *   The vocabulary id.
   *
   * @return \Drupal\taxonomy_menu\TaxonomyMenuInterface[]
   *   The Taxonomy Menu
   */
  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
   *   Term.
   * @param bool $rebuild_all
   *   Rebuild all.
   */
  public function generateTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) {

    // Load relevant taxonomy menus.
    $tax_menus = $this
      ->getTermMenusByVocabulary($term
      ->bundle());
    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
   *   Term.
   * @param bool $rebuild_all
   *   Rebuild all.
   */
  public function updateTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) {

    // Load relevant taxonomy menus.
    $tax_menus = $this
      ->getTermMenusByVocabulary($term
      ->bundle());

    /* @var $menu \Drupal\taxonomy_menu\TaxonomyMenuInterface */
    foreach ($tax_menus as $menu) {
      $links = $menu
        ->getLinks([], TRUE);
      foreach ($links 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;
          }
        }
        if ($this->manager
          ->hasDefinition($plugin_id)) {
          $this->manager
            ->updateDefinition($plugin_id, $plugin_def, FALSE);
        }
        else {

          // Remove specific menu link if vid term is different to this old vid.
          if ($term->original
            ->bundle() != $term
            ->bundle()) {
            $this
              ->removeTaxonomyMenuEntries($term->original);
          }
          $this->manager
            ->addDefinition($plugin_id, $plugin_def);
        }
      }
    }
  }

  /**
   * Remove menu entries associate with the vocabulary of this term.
   *
   * @param \Drupal\taxonomy\TermInterface $term
   *   Term.
   * @param bool $rebuild_all
   *   Whether to rebuild all links or not.
   */
  public function removeTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) {

    // Load relevant taxonomy menus.
    $tax_menus = $this
      ->getTermMenusByVocabulary($term
      ->bundle());

    /* @var $menu \Drupal\taxonomy_menu\TaxonomyMenuInterface */
    foreach ($tax_menus as $menu) {

      // Remove all links.
      if ($rebuild_all) {
        $links = array_keys($menu
          ->getLinks([], TRUE));
        foreach ($links as $plugin_id) {
          $this->manager
            ->removeDefinition($plugin_id, FALSE);
        }
      }
      elseif (!empty($term)) {
        $this->manager
          ->removeDefinition($menu
          ->buildMenuPluginId($term), FALSE);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TaxonomyMenuHelper::$manager protected property Menu Link Manager.
TaxonomyMenuHelper::$menuStorage protected property Taxonomy Menu storage.
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 Constructor.