Score:0

How to get node id when update a page related to menu link

gb flag

In my module I use the mymodulename_entity_update() function to listen when an entity is updated:

function mymodulename_entity_update($entity) {
    file_put_contents(__DIR__.'/test.txt', $entity->bundle());
}

When I update an article or a page which is not related to menu link, I receive 'node' as value of the ->bundle() property, as expected.

But if I update a page which is related to a menu link, I receive the 'menu_link_content' as the bundle value and all the data of the $entity object is the data of menu link. The same data I receive when update a menu link. And the only thing that helps me to detect whether a page was updated or menu link is the ->expanded->value property: in the case of a page it equals 0.

The second problem is, how to get ID of the node which is related to the menu link?

I was trying $entity->getRouteParameters()['node'] as recommended by the link https://drupal.stackexchange.com/a/281646/104765 but I receive the error

Call to undefined method Drupal\menu_link_content\Entity\MenuLinkContent::getRouteParameters()...

sonfd avatar
in flag
If you save a node with a menu link, it will save the menu link (from the form on the node edit form) as well as the node. It's not clear what you're trying to do, but you may want to use [hook_ENTITY_TYPE_update()](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_update/9.2.x), e.g. `hook_node_update()`, to only act on an updated node.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.