You are here

public function EntityReferenceRevisionsItem::postSave in Entity Reference Revisions 8

Defines custom post-save behavior for field values.

This method is called during the process of saving an entity, just after values are written into storage. This is useful mostly when the business logic to be implemented always requires the entity identifier, even when storing a new entity. For instance, when implementing circular entity references, the referenced entity will be created on pre-save with a dummy value for the referring entity identifier, which will be updated with the actual one on post-save.

In the rare cases where item properties depend on the entity identifier, massaging logic will have to be implemented on post-save and returning TRUE will allow them to be rewritten to the storage with the updated values.

Parameters

bool $update: Specifies whether the entity is being updated or created.

Return value

bool Whether field items should be rewritten to the storage as a consequence of the logic implemented by the custom behavior.

Overrides FieldItemBase::postSave

File

src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php, line 314

Class

EntityReferenceRevisionsItem
Defines the 'entity_reference_revisions' entity field type.

Namespace

Drupal\entity_reference_revisions\Plugin\Field\FieldType

Code

public function postSave($update) {
  parent::postSave($update);
  $needs_save = FALSE;

  // If any of entity, parent type or parent id is missing then return.
  if (!$this->entity || !$this->entity
    ->getEntityType()
    ->get('entity_revision_parent_type_field') || !$this->entity
    ->getEntityType()
    ->get('entity_revision_parent_id_field')) {
    return;
  }
  $entity = $this->entity;
  $parent_entity = $this
    ->getEntity();

  // If the entity has a parent field name get the key.
  if ($entity
    ->getEntityType()
    ->get('entity_revision_parent_field_name_field')) {
    $parent_field_name = $entity
      ->getEntityType()
      ->get('entity_revision_parent_field_name_field');

    // If parent field name has changed then set it.
    if ($entity
      ->get($parent_field_name)->value != $this
      ->getFieldDefinition()
      ->getName()) {
      $entity
        ->set($parent_field_name, $this
        ->getFieldDefinition()
        ->getName());
      $needs_save = TRUE;
    }
  }

  // Keep in sync the translation languages between the parent and the child.
  // For non translatable fields we have to do this in ::preSave but for
  // translatable fields we have all the information we need in ::delete.
  if (isset($parent_entity->original) && !$this
    ->getFieldDefinition()
    ->isTranslatable()) {
    $langcodes = array_keys($parent_entity
      ->getTranslationLanguages());
    $original_langcodes = array_keys($parent_entity->original
      ->getTranslationLanguages());
    if ($removed_langcodes = array_diff($original_langcodes, $langcodes)) {
      foreach ($removed_langcodes as $removed_langcode) {
        if ($entity
          ->hasTranslation($removed_langcode) && $entity
          ->getUntranslated()
          ->language()
          ->getId() != $removed_langcode) {
          $entity
            ->removeTranslation($removed_langcode);
        }
      }
      $needs_save = TRUE;
    }
  }
  $parent_type = $entity
    ->getEntityType()
    ->get('entity_revision_parent_type_field');
  $parent_id = $entity
    ->getEntityType()
    ->get('entity_revision_parent_id_field');

  // If the parent type has changed then set it.
  if ($entity
    ->get($parent_type)->value != $parent_entity
    ->getEntityTypeId()) {
    $entity
      ->set($parent_type, $parent_entity
      ->getEntityTypeId());
    $needs_save = TRUE;
  }

  // If the parent id has changed then set it.
  if ($entity
    ->get($parent_id)->value != $parent_entity
    ->id()) {
    $entity
      ->set($parent_id, $parent_entity
      ->id());
    $needs_save = TRUE;
  }
  if ($needs_save) {

    // Check if any of the keys has changed, save it, do not create a new
    // revision.
    $entity
      ->setNewRevision(FALSE);
    $entity
      ->save();
  }
}