Score:1

How to get Paragraph Changed Time?

nz flag

I know how get User entity changed time ($user->getChangedTime()).
Node has the same value ($node->changed), but I need to get this changed time from a Paragraph.

When I try this method $paragraph->getChangedTime() (the value changed do not exists) i got this:

Error: Call to undefined method Drupal\paragraphs\Entity\Paragraph::getChangedTime() ...

I have this field in my User entity, and I need to know when this field is changed. Is there any way to achieve that?

4uk4 avatar
cn flag
The Paragraph entity doesn't have a changed field. You need your own date field and fill it in a presave hook.
ru flag
Paragraphs (and all entity reference revision fields) are directly tied to their parent entity, AFAIK their revisioning is completely tied to the host too. If you save the host, a new revision of all paragraphs is created, so a changed timestamp doesn't really make sense here by architecture of the module. If you need a separate changed timestamp, Paragraphs is the wrong tool.
Score:0
cn flag

You can add a date/time field to the paragraph type and fill in the current time if the paragraph is changed:

mymodule.module

<?php

use Drupal\paragraphs\ParagraphInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

/**
 * Implements hook_ENTITY_TYPE_presave() for paragraph entities.
 */
function mymodule_paragraph_presave(ParagraphInterface $paragraph) {
  if ($paragraph->getType() == 'my_paragraph_type') {
    if ($paragraph->isChanged()) {
      $paragraph->field_date->value = (new DrupalDateTime('now', DateTimeItemInterface::STORAGE_TIMEZONE))
        ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
    }
  }
}
ru flag
I did not knew something like `$paragraph->isChanged()` existed. Is this paragraphs specific or can this be used with other entities too?
4uk4 avatar
cn flag
This is specific to paragraphs but it's basically the same as if you would compare with $entity->original. So you could define a custom changed time for the content of a specific field or a set of fields in any entity.
I sit in a Tesla and translated this thread with Ai:

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.