You are here

public function EntityUsageTrackBase::trackOnEntityCreation in Entity Usage 8.2

Same name and namespace in other branches
  1. 8 src/EntityUsageTrackBase.php \Drupal\entity_usage\EntityUsageTrackBase::trackOnEntityCreation()
  2. 8.3 src/EntityUsageTrackBase.php \Drupal\entity_usage\EntityUsageTrackBase::trackOnEntityCreation()

Track usage updates on the creation of entities.

Parameters

\Drupal\Core\Entity\EntityInterface $source_entity: The source entity.

Overrides EntityUsageTrackInterface::trackOnEntityCreation

1 call to EntityUsageTrackBase::trackOnEntityCreation()
EntityUsageTrackBase::trackOnEntityUpdate in src/EntityUsageTrackBase.php
Track usage updates on the edition of entities.

File

src/EntityUsageTrackBase.php, line 139

Class

EntityUsageTrackBase
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);
        }
      }
    }
  }
}