BlockField.php in Entity Usage 8.3
File
src/Plugin/EntityUsage/Track/BlockField.php
View source
<?php
namespace Drupal\entity_usage\Plugin\EntityUsage\Track;
use Drupal\block_content\Plugin\Block\BlockContentBlock;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\entity_usage\EntityUsageTrackBase;
class BlockField extends EntityUsageTrackBase {
public function getTargetEntities(FieldItemInterface $item) {
$block_instance = $item
->getBlock();
if (!$block_instance) {
return [];
}
$target_type = NULL;
$target_id = NULL;
if ($block_instance
->getBaseId() === 'views_block') {
list($view_name, $display_id) = explode('-', $block_instance
->getDerivativeId(), 2);
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)) {
$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,
] : [];
}
}
Classes
Name |
Description |
BlockField |
Tracks usage of entities related in block_field fields. |