You are here

class BlockField in Entity Usage 8.4

Tracks usage of entities related in block_field fields.

Plugin annotation


@EntityTrack(
  id = "block_field",
  label = @Translation("Block Field"),
  description = @Translation("Tracks relationships created with 'Block Field' fields."),
  field_types = {"block_field"},
)

Hierarchy

  • class \Drupal\entity_usage\EntityUsageBase extends \Drupal\entity_track\EntityTrackBase
    • class \Drupal\entity_usage\Plugin\EntityTrack\Track\BlockField

Expanded class hierarchy of BlockField

File

src/Plugin/EntityTrack/Track/BlockField.php, line 20

Namespace

Drupal\entity_usage\Plugin\EntityTrack\Track
View source
class BlockField extends EntityUsageBase {

  /**
   * {@inheritdoc}
   */
  public function getTargetEntities(FieldItemInterface $item) {

    /** @var \Drupal\block_field\BlockFieldItemInterface $item */
    $block_instance = $item
      ->getBlock();
    if (!$block_instance) {
      return [];
    }
    $target_type = NULL;
    $target_id = NULL;

    // If there is a view inside this block, track the view entity instead.
    if ($block_instance
      ->getBaseId() === 'views_block') {
      list($view_name, $display_id) = explode('-', $block_instance
        ->getDerivativeId(), 2);

      // @todo worth trying to track the display id as well?
      // At this point the view is supposed to exist. Only track it if so.
      if ($this->entityTypeManager
        ->getStorage('view')
        ->load($view_name)) {
        $target_type = 'view';
        $target_id = $view_name;
      }
    }
    elseif ($block_instance instanceof BlockContentBlock && ($uuid = $block_instance
      ->getDerivativeId())) {
      $blocks = $this->entityTypeManager
        ->getStorage('block_content')
        ->loadByProperties([
        'uuid' => $uuid,
      ]);
      if (!empty($blocks)) {

        // Doing this here means that an initial save operation of a host entity
        // will likely not track this block, once it does not exist at this
        // point. However, it's preferable to miss that and ensure we only track
        // lodable entities.
        $block = reset($blocks);
        $target_id = $block
          ->id();
        $target_type = 'block_content';
      }
    }
    elseif ($block_instance instanceof BlockPluginInterface && !$block_instance instanceof BlockContentBlock) {
      $target_id = $block_instance
        ->getPluginId();
      $target_type = 'block';
    }
    else {
      throw new \Exception('Block saved as target entity is not one of the trackable block types.');
    }
    return $target_type && $target_id ? [
      $target_type . '|' . $target_id,
    ] : [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockField::getTargetEntities public function Retrieve the target entity(ies) from a field item value. Overrides EntityUsageBase::getTargetEntities
EntityUsageBase::$entityRepository protected property The EntityRepository service.
EntityUsageBase::$usageConfig protected property The usage config.
EntityUsageBase::$usageService protected property The usage tracking service.
EntityUsageBase::create public static function 2
EntityUsageBase::trackOnEntityCreation public function
EntityUsageBase::trackOnEntityDelete public function
EntityUsageBase::trackOnEntityUpdate public function
EntityUsageBase::__construct public function Plugin constructor. 2