You are here

public function Link::trackOnEntityCreation in Entity Usage 8

Track usage updates on the creation of entities.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity we are dealing with.

Overrides EntityUsageTrackBase::trackOnEntityCreation

File

src/Plugin/EntityUsage/Track/Link.php, line 85

Class

Link
Tracks usage of entities related in entity_reference fields.

Namespace

Drupal\entity_usage\Plugin\EntityUsage\Track

Code

public function trackOnEntityCreation(ContentEntityInterface $entity) {
  foreach ($this
    ->linkFieldsAvailable($entity) as $field_name) {
    if ($entity
      ->hasField($field_name) && !$entity->{$field_name}
      ->isEmpty()) {

      /** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $field_item */
      foreach ($entity->{$field_name} as $field_item) {

        // This item got added. Track the usage up.
        $target_entity = $this
          ->getTargetEntity($field_item);
        if ($target_entity) {
          list($target_type, $target_id) = explode('|', $target_entity);
          $this->usageService
            ->add($target_id, $target_type, $entity
            ->id(), $entity
            ->getEntityTypeId(), $this->pluginId);
        }
      }
    }
  }
}