You are here

public function EntityUsageBase::trackOnEntityCreation in Entity Usage 8.4

1 call to EntityUsageBase::trackOnEntityCreation()
EntityUsageBase::trackOnEntityUpdate in src/EntityUsageBase.php

File

src/EntityUsageBase.php, line 88

Class

EntityUsageBase
Base implementation for track plugins.

Namespace

Drupal\entity_usage

Code

public function trackOnEntityCreation(EntityInterface $source_entity) {
  $trackable_field_types = $this
    ->getApplicableFieldTypes();
  $fields = array_keys($this
    ->getReferencingFields($source_entity, $trackable_field_types));
  foreach ($fields as $field_name) {
    if ($source_entity
      ->hasField($field_name) && !$source_entity->{$field_name}
      ->isEmpty()) {

      /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
      foreach ($source_entity->{$field_name} as $field_item) {

        // The entity is being created with value on this field, so we just
        // need to add a tracking record.
        $target_entities = $this
          ->getTargetEntities($field_item);
        foreach ($target_entities as $target_entity) {
          list($target_type, $target_id) = explode("|", $target_entity);
          $source_vid = $source_entity instanceof RevisionableInterface && $source_entity
            ->getRevisionId() ? $source_entity
            ->getRevisionId() : 0;
          $this->usageService
            ->registerUsage($target_id, $target_type, $source_entity
            ->id(), $source_entity
            ->getEntityTypeId(), $source_entity
            ->language()
            ->getId(), $source_vid, $this->pluginId, $field_name);
        }
      }
    }
  }
}