You are here

protected function PbfSynchronize::insert in Permissions by field 8

Update entities ref. by a Pbf field synchronized when entity is created.

Parameters

string $target_entity_type_id: The entity type id of targeted entity.

\Drupal\field\FieldConfigInterface $target_field: The Pbf field synchronized by on the referenced entity.

\Drupal\Core\Entity\EntityInterface $entity: The entity updated.

\Drupal\field\FieldConfigInterface $field: The Pbf field synchronized with on the entity.

1 call to PbfSynchronize::insert()
PbfSynchronize::synchronizeTarget in src/PbfSynchronize.php
Launch the synchronization belong the operation done on the entity.

File

src/PbfSynchronize.php, line 278

Class

PbfSynchronize
Class PbfSynchronize.

Namespace

Drupal\pbf

Code

protected function insert($target_entity_type_id, FieldConfigInterface $target_field, EntityInterface $entity, FieldConfigInterface $field) {
  $target_field_name = $target_field
    ->getName();
  $target_ids = $this
    ->getReferenceIds($entity, $field);
  if ($target_ids) {
    $target_entities = $this->entityTypeManager
      ->getStorage($target_entity_type_id)
      ->loadMultiple($target_ids);

    /** @var \Drupal\Core\Entity\FieldableEntityInterface $target_entity */
    foreach ($target_entities as $target_entity) {
      $ids_referenced_from_target = $this
        ->getReferenceIds($target_entity, $target_field);
      if (!in_array($entity
        ->id(), $ids_referenced_from_target)) {
        if ($target_entity
          ->hasField($target_field_name)) {
          $target_entity->{$target_field_name}
            ->appendItem($entity
            ->id());
          $target_entity
            ->save();
        }
      }
    }
  }
}