function MenuLinkContent::postSave

Same name in other branches
  1. 9 core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::postSave()
  2. 8.9.x core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::postSave()
  3. 11.x core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::postSave()

Overrides ContentEntityBase::postSave

File

core/modules/menu_link_content/src/Entity/MenuLinkContent.php, line 210

Class

MenuLinkContent
Defines the menu link content entity class.

Namespace

Drupal\menu_link_content\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);
    // Don't update the menu tree if a pending revision was saved.
    if (!$this->isDefaultRevision()) {
        return;
    }
    
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    // The menu link can just be updated if there is already a menu link entry
    // on both entity and menu link plugin level.
    $definition = $this->getPluginDefinition();
    // Even when $update is FALSE, for top level links it is possible the link
    // already is in the storage because of the getPluginDefinition() call
    // above, see https://www.drupal.org/node/2605684#comment-10515450 for the
    // call chain. Because of this the $update flag is ignored and only the
    // existence of the definition (equals to being in the tree storage) is
    // checked.
    if ($menu_link_manager->getDefinition($this->getPluginId(), FALSE)) {
        // When the entity is saved via a plugin instance, we should not call
        // the menu tree manager to update the definition a second time.
        if (!$this->insidePlugin) {
            $menu_link_manager->updateDefinition($this->getPluginId(), $definition, FALSE);
        }
    }
    else {
        $menu_link_manager->addDefinition($this->getPluginId(), $definition);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.