ExtraFieldDisplayManager.php in Extra Field 8.2
File
src/Plugin/ExtraFieldDisplayManager.php
View source
<?php
namespace Drupal\extra_field\Plugin;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class ExtraFieldDisplayManager extends ExtraFieldManagerBase implements ExtraFieldDisplayManagerInterface {
const PLUGIN_SUBDIR = 'Plugin/ExtraField/Display';
const PLUGIN_INTERFACE = 'Drupal\\extra_field\\Plugin\\ExtraFieldDisplayInterface';
const PLUGIN_ANNOTATION_NAME = 'Drupal\\extra_field\\Annotation\\ExtraFieldDisplay';
const ALTER_HOOK = 'extra_field_display_info';
const CACHE_KEY = 'extra_field_display_plugins';
protected $entityTypeManager;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct(self::PLUGIN_SUBDIR, $namespaces, $module_handler, self::PLUGIN_INTERFACE, self::PLUGIN_ANNOTATION_NAME);
$this
->alterInfo(self::ALTER_HOOK);
$this
->setCacheBackend($cache_backend, self::CACHE_KEY);
$this->entityTypeManager = $entity_type_manager;
}
public function fieldInfo() {
$info = [];
$definitions = $this
->getDefinitions();
foreach ($definitions as $pluginId => $definition) {
$entityBundles = $this
->supportedEntityBundles($definition['bundles']);
foreach ($entityBundles as $entityBundle) {
$entityType = $entityBundle['entity'];
$bundle = $entityBundle['bundle'];
$fieldName = $this
->fieldName($pluginId);
$info[$entityType][$bundle]['display'][$fieldName] = [
'label' => $definition['label'],
'description' => isset($definition['description']) ? $definition['description'] : '',
'weight' => $definition['weight'],
'visible' => $definition['visible'],
];
}
}
return $info;
}
public function entityView(array &$build, ContentEntityInterface $entity, EntityViewDisplayInterface $display, $viewMode) {
$definitions = $this
->getDefinitions();
$entityBundleKey = $this
->entityBundleKey($entity
->getEntityTypeId(), $entity
->bundle());
foreach ($definitions as $pluginId => $definition) {
if (!$this
->matchEntityBundleKey($definition['bundles'], $entityBundleKey)) {
continue;
}
$factory = $this
->getFactory();
if (!$display
->getComponent($this
->fieldName($pluginId))) {
continue;
}
$plugin = $factory
->createInstance($pluginId);
$fieldName = $this
->fieldName($pluginId);
$plugin
->setEntity($entity);
$plugin
->setEntityViewDisplay($display);
$plugin
->setViewMode($viewMode);
$elements = $plugin
->view($entity);
if (empty($elements)) {
continue;
}
$build[$fieldName] = $elements;
}
}
protected function getEntityTypeManager() {
return $this->entityTypeManager;
}
}