You are here

public function Paragraph::isChanged in Paragraphs 8

Returns a flag whether a current revision has been changed.

The current instance is being compared with the latest saved revision.

Return value

bool TRUE in case the current revision changed. Otherwise, FALSE.

Overrides ParagraphInterface::isChanged

See also

\Drupal\Core\Entity\ContentEntityBase::hasTranslationChanges()

File

src/Entity/Paragraph.php, line 595

Class

Paragraph
Defines the Paragraph entity.

Namespace

Drupal\paragraphs\Entity

Code

public function isChanged() {
  if ($this
    ->isNew()) {
    return TRUE;
  }

  // $this->original only exists during save. If it exists we re-use it here
  // for performance reasons.

  /** @var \Drupal\paragraphs\ParagraphInterface $original */
  $original = $this->original ?: NULL;
  if (!$original) {
    $original = $this
      ->entityTypeManager()
      ->getStorage($this
      ->getEntityTypeId())
      ->loadRevision($this
      ->getLoadedRevisionId());
  }

  // If the current revision has just been added, we have a change.
  if ($original
    ->isNewRevision()) {
    return TRUE;
  }

  // The list of fields to skip from the comparision.
  $skip_fields = $this
    ->getFieldsToSkipFromChangedCheck();

  // Compare field item current values with the original ones to determine
  // whether we have changes. We skip also computed fields as comparing them
  // with their original values might not be possible or be meaningless.
  foreach ($this
    ->getFieldDefinitions() as $field_name => $definition) {
    if (in_array($field_name, $skip_fields, TRUE)) {
      continue;
    }
    $field = $this
      ->get($field_name);

    // When saving entities in the user interface, the changed timestamp is
    // automatically incremented by ContentEntityForm::submitForm() even if
    // nothing was actually changed. Thus, the changed time needs to be
    // ignored when determining whether there are any actual changes in the
    // entity.
    if (!$field instanceof ChangedFieldItemList && !$definition
      ->isComputed()) {
      $items = $field
        ->filterEmptyItems();
      $original_items = $original
        ->get($field_name)
        ->filterEmptyItems();
      if (!$items
        ->equals($original_items)) {
        return TRUE;
      }
    }
  }
  return FALSE;
}