You are here

protected function PbfSynchronize::delete in Permissions by field 8

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

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::delete()
PbfSynchronize::synchronizeTarget in src/PbfSynchronize.php
Launch the synchronization belong the operation done on the entity.

File

src/PbfSynchronize.php, line 308

Class

PbfSynchronize
Class PbfSynchronize.

Namespace

Drupal\pbf

Code

protected function delete($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);
    foreach ($target_entities as $target_entity) {
      $ids_referenced_from_target = $this
        ->getReferenceIds($target_entity, $target_field);
      foreach ($ids_referenced_from_target as $delta => $reference) {
        if ($reference == $entity
          ->id()) {
          if ($target_entity
            ->hasField($target_field_name)) {
            $target_entity
              ->get($target_field_name)
              ->removeItem($delta);
            $target_entity
              ->save();
          }
        }
      }
    }
  }
}