Score:0

How to get the revision of a paragraph

sa flag

I have a code which is running in hook_node_update. This code grab the revision of the saved node and compare the values of the fields between current node and its revision to generate some kind of historic log (what is deleted/updated/added).

This is working very well except for the paragraph field... When my code is trying to get the revision of a paragraph (recursively), I got each time the same paragraph instead of the old revision (thus no differences to log)

Here is my code to get the revision of a the saved node (working well). This code is also used for the paragraphs (not working)

$updated_entity : the node/paragraph currently in the process to be saved

$old_entity : the revision to compare with

  $old_entity = $updated_entity->original;
  if (!$old_entity) {
    $storage = $this->getStorage($updated_entity);
    // no need to test if revisionable as we use only Node and Paragraphs
    if ($revision_id = $updated_entity->getLoadedRevisionId()) {
      $old_entity = $storage->loadRevision($revision_id);
     }
    else {
      //... handling new entity
      ]);
    }
  }

This code, when applied to the paragraphs, is producing an $old_entity == $updated_entity

The paragraphs are extracted from the currently saved node as follow:

$paragraphs = $updated_entity->get($paragraph_field)->referencedEntities();

after that, they become the $updated_entity variable in the upper function which is trying to get the revision...

Score:1
cn flag

The unnamed upper function works only for the one specific node you get via the hook as parameter. Load the old referenced paragraphs via the original entity:

$old_paragraphs = $node->original->get($paragraph_field)->referencedEntities();

For nested paragraphs you can then apply recursive loading, but each branch (old/new) separately and not with the upper function, use ->referencedEntities() for mulitple and ->entity for single field items, these functions load revisions.

In a multilingual site you have to get the translation of the paragraph using the node language:

$node_language = $node->language()->getId();
$value = $paragraph->getTranslation($node_language)->field_foo->value;
cn flag
Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/131668/discussion-on-answer-by-4k4-how-to-get-the-revision-of-a-paragraph).
4uk4 avatar
cn flag
@gagarine, one could argue if the paragraphs are rearranged this is a change. If you want to ignore that you could sort both arrays by paragraph id before comparing.
cn flag
@4uk4 yes, it's really depend of what you need to achieve. On my side, I need to detect changes at the paragraphe item level. But sorting is not enough, as you may have deleted and add a new one. I end up doing a loop on the paragraphs array and compare until I found one with the same entity ID. Simple and effective :).
cn flag
But what if paragraphs get reordered? A given array key in the array returned by referencedEntities may not point to the same entity. I think instead we should load the older entity from the entity ID of the paragraph entry.
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.