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()...

  • 1
    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(), e.g. hook_node_update(), to only act on an updated node.
    – sonfd
    Aug 9 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.