You are here

public function MenuLinkContent::postSave in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/menu_link_content/src/Entity/MenuLinkContent.php \Drupal\menu_link_content\Entity\MenuLinkContent::postSave()

Acts on a saved entity before the insert or update hook is invoked.

Used after the entity is saved, but before invoking the insert or update hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides ContentEntityBase::postSave

File

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

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);
  }
}