You are here

class MenuTokenTreeStorage in Menu Token 8

Same name and namespace in other branches
  1. 9.1.x src/Service/MenuTokenTreeStorage.php \Drupal\menu_token\Service\MenuTokenTreeStorage

Provides a menu tree storage using the database.

Hierarchy

Expanded class hierarchy of MenuTokenTreeStorage

1 string reference to 'MenuTokenTreeStorage'
menu_token.services.yml in ./menu_token.services.yml
menu_token.services.yml
1 service uses MenuTokenTreeStorage
menu_token.tree_storage in ./menu_token.services.yml
Drupal\menu_token\Service\MenuTokenTreeStorage

File

src/Service/MenuTokenTreeStorage.php, line 11

Namespace

Drupal\menu_token\Service
View source
class MenuTokenTreeStorage extends MenuTreeStorage {

  /**
   * {@inheritdoc}
   */
  public function rebuildNonDestructive(array $definitions) {
    $links = [];
    $children = [];
    $top_links = [];

    // Fetch the list of existing menus, in case some are not longer populated
    // after the rebuild.
    $before_menus = $this
      ->getMenuNames();
    if ($definitions) {
      foreach ($definitions as $id => $link) {

        // Flag this link as discovered, i.e. saved via rebuild().
        $link['discovered'] = 1;

        // Note: The parent we set here might be just stored in the {menu_tree}
        // table, so it will not end up in $top_links. Therefore the later loop
        // on the orphan links, will handle those cases.
        if (!empty($link['parent'])) {
          $children[$link['parent']][$id] = $id;
        }
        else {

          // A top level link - we need them to root our tree.
          $top_links[$id] = $id;
          $link['parent'] = '';
        }
        $links[$id] = $link;
      }
    }
    foreach ($top_links as $id) {
      $this
        ->saveRecursive($id, $children, $links);
    }

    // Handle any children we didn't find starting from top-level links.
    foreach ($children as $orphan_links) {
      foreach ($orphan_links as $id) {

        // Check for a parent that is not loaded above since only internal links
        // are loaded above.
        $parent = $this
          ->loadFull($links[$id]['parent']);

        // If there is a parent add it to the links to be used in
        // ::saveRecursive().
        if ($parent) {
          $links[$links[$id]['parent']] = $parent;
        }
        else {

          // Force it to the top level.
          $links[$id]['parent'] = '';
        }
        $this
          ->saveRecursive($id, $children, $links);
      }
    }
    $this
      ->resetDefinitions();
    $affected_menus = $this
      ->getMenuNames() + $before_menus;

    // Invalidate any cache tagged with any menu name.
    $cache_tags = Cache::buildTags('config:system.menu', $affected_menus, '.');
    $this->cacheTagsInvalidator
      ->invalidateTags($cache_tags);
    $this
      ->resetDefinitions();

    // Every item in the cache bin should have one of the menu cache tags but it
    // is not guaranteed, so invalidate everything in the bin.
    $this->menuCacheBackend
      ->invalidateAll();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuTokenTreeStorage::rebuildNonDestructive public function
MenuTreeStorage::$cacheTagsInvalidator protected property The cache tags invalidator.
MenuTreeStorage::$connection protected property The database connection.
MenuTreeStorage::$definitionFields protected property List of plugin definition fields.
MenuTreeStorage::$definitions protected property Stores definitions that have already been loaded for better performance.
MenuTreeStorage::$menuCacheBackend protected property Cache backend instance for the extracted tree data.
MenuTreeStorage::$options protected property Additional database connection options to use in queries.
MenuTreeStorage::$serializedFields protected property List of serialized fields.
MenuTreeStorage::$table protected property The database table name.
MenuTreeStorage::collectRoutesAndDefinitions protected function Traverses the menu tree and collects all the route names and definitions.
MenuTreeStorage::countMenuLinks public function Counts the total number of menu links in one menu or all menus. Overrides MenuTreeStorageInterface::countMenuLinks
MenuTreeStorage::definitionFields protected function Determines fields that are part of the plugin definition.
MenuTreeStorage::delete public function Deletes a menu link definition from the storage. Overrides MenuTreeStorageInterface::delete
MenuTreeStorage::doBuildTreeData protected function Prepares the data for calling $this->treeDataRecursive().
MenuTreeStorage::doCollectRoutesAndDefinitions protected function Collects all the route names and definitions.
MenuTreeStorage::doDeleteMultiple protected function Purge menu links from the database.
MenuTreeStorage::doFindChildrenRelativeDepth protected function Finds the relative depth of this link's deepest child.
MenuTreeStorage::doSave protected function Saves a link without clearing caches.
MenuTreeStorage::ensureTableExists protected function Checks if the tree table exists and create it if not.
MenuTreeStorage::findNoLongerExistingLinks protected function Find any previously discovered menu links that no longer exist.
MenuTreeStorage::findParent protected function Loads the parent definition if it exists.
MenuTreeStorage::getAllChildIds public function Loads all the IDs for menu links that are below the given ID. Overrides MenuTreeStorageInterface::getAllChildIds
MenuTreeStorage::getExpanded public function Finds expanded links in a menu given a set of possible parents. Overrides MenuTreeStorageInterface::getExpanded
MenuTreeStorage::getMenuNames public function Returns the used menu names in the tree storage. Overrides MenuTreeStorageInterface::getMenuNames
MenuTreeStorage::getRootPathIds public function Returns all the IDs that represent the path to the root of the tree. Overrides MenuTreeStorageInterface::getRootPathIds
MenuTreeStorage::getSubtreeHeight public function Finds the height of a subtree rooted by the given ID. Overrides MenuTreeStorageInterface::getSubtreeHeight
MenuTreeStorage::load public function Loads a menu link plugin definition from the storage. Overrides MenuTreeStorageInterface::load
MenuTreeStorage::loadAllChildren public function Loads all the enabled menu links that are below the given ID. Overrides MenuTreeStorageInterface::loadAllChildren
MenuTreeStorage::loadByProperties public function Loads multiple plugin definitions from the storage based on properties. Overrides MenuTreeStorageInterface::loadByProperties
MenuTreeStorage::loadByRoute public function Loads multiple plugin definitions from the storage based on route. Overrides MenuTreeStorageInterface::loadByRoute
MenuTreeStorage::loadFull protected function Loads all table fields, not just those that are in the plugin definition.
MenuTreeStorage::loadFullMultiple protected function Loads all table fields for multiple menu link definitions by ID.
MenuTreeStorage::loadLinks protected function Loads links in the given menu, according to the given tree parameters.
MenuTreeStorage::loadMultiple public function Loads multiple plugin definitions from the storage. Overrides MenuTreeStorageInterface::loadMultiple
MenuTreeStorage::loadSubtreeData public function Loads a subtree rooted by the given ID. Overrides MenuTreeStorageInterface::loadSubtreeData
MenuTreeStorage::loadTreeData public function Loads a menu link tree from the storage. Overrides MenuTreeStorageInterface::loadTreeData
MenuTreeStorage::maxDepth public function The maximum depth of tree the storage implementation supports. Overrides MenuTreeStorageInterface::maxDepth
MenuTreeStorage::MAX_DEPTH constant The maximum depth of a menu links tree.
MenuTreeStorage::menuNameInUse public function Determines whether a specific menu name is used in the tree. Overrides MenuTreeStorageInterface::menuNameInUse
MenuTreeStorage::moveChildren protected function Re-parents a link's children when the link itself is moved.
MenuTreeStorage::prepareLink protected function Prepares a link by unserializing values and saving the definition.
MenuTreeStorage::preSave protected function Fills in all the fields the database save needs, using the link definition.
MenuTreeStorage::purgeMultiple protected function Purges multiple menu links that no longer exist.
MenuTreeStorage::rebuild public function Rebuilds the stored menu link definitions. Overrides MenuTreeStorageInterface::rebuild
MenuTreeStorage::resetDefinitions public function Clears all definitions cached in memory. Overrides MenuTreeStorageInterface::resetDefinitions
MenuTreeStorage::safeExecuteSelect protected function Executes a select query while making sure the database table exists.
MenuTreeStorage::save public function Saves a plugin definition to the storage. Overrides MenuTreeStorageInterface::save
MenuTreeStorage::saveRecursive protected function Saves menu links recursively.
MenuTreeStorage::schemaDefinition protected static function Defines the schema for the tree table.
MenuTreeStorage::serializedFields protected function Determines serialized fields in the storage.
MenuTreeStorage::setParents protected function Sets the materialized path field values based on the parent.
MenuTreeStorage::treeDataRecursive protected function Builds the data representing a menu tree.
MenuTreeStorage::updateParentalStatus protected function Sets has_children for the link's parent if it has visible children.
MenuTreeStorage::__construct public function Constructs a new \Drupal\Core\Menu\MenuTreeStorage.