abstract class FieldFormatterEntityEmbedDisplayBase in Entity Embed 8
Base class for field formatter display plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase implements ContainerFactoryPluginInterface, EntityEmbedDisplayInterface
- class \Drupal\entity_embed\EntityEmbedDisplay\FieldFormatterEntityEmbedDisplayBase uses PluginDependencyTrait
- class \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase implements ContainerFactoryPluginInterface, EntityEmbedDisplayInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of FieldFormatterEntityEmbedDisplayBase
1 file declares its use of FieldFormatterEntityEmbedDisplayBase
- EntityReferenceFieldFormatter.php in src/
Plugin/ entity_embed/ EntityEmbedDisplay/ EntityReferenceFieldFormatter.php
File
- src/
EntityEmbedDisplay/ FieldFormatterEntityEmbedDisplayBase.php, line 20
Namespace
Drupal\entity_embed\EntityEmbedDisplayView source
abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBase {
use PluginDependencyTrait;
/**
* The field formatter plugin manager.
*
* @var \Drupal\Core\Field\FormatterPluginManager
*/
protected $formatterPluginManager;
/**
* The typed data manager.
*
* @var \Drupal\Core\TypedData\TypedDataManager
*/
protected $typedDataManager;
/**
* The field definition.
*
* @var \Drupal\Core\Field\BaseFieldDefinition
*/
protected $fieldDefinition;
/**
* The field formatter.
*
* @var \Drupal\Core\Field\FormatterInterface
*/
protected $fieldFormatter;
/**
* Constructs a FieldFormatterEntityEmbedDisplayBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager
* The field formatter plugin manager.
* @param \Drupal\Core\TypedData\TypedDataManager $typed_data_manager
* The typed data manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager, LanguageManagerInterface $language_manager) {
$this->formatterPluginManager = $formatter_plugin_manager;
$this
->setConfiguration($configuration);
$this->typedDataManager = $typed_data_manager;
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $language_manager);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('plugin.manager.field.formatter'), $container
->get('typed_data_manager'), $container
->get('language_manager'));
}
/**
* Get the FieldDefinition object required to render this field's formatter.
*
* @return \Drupal\Core\Field\BaseFieldDefinition
* The field definition.
*
* @see \Drupal\entity_embed\FieldFormatterEntityEmbedDisplayBase::build()
*/
public function getFieldDefinition() {
if (!isset($this->fieldDefinition)) {
$field_type = $this
->getPluginDefinition()['field_type'];
$this->fieldDefinition = BaseFieldDefinition::create($field_type);
// Ensure the field name is unique for each Entity Embed Display plugin
// instance.
static $index = 0;
$this->fieldDefinition
->setName('_entity_embed_' . $index++);
}
return $this->fieldDefinition;
}
/**
* Get the field value required to pass into the field formatter.
*
* @return mixed
* The field value.
*/
public abstract function getFieldValue();
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account = NULL) {
return parent::access($account)
->andIf($this
->isApplicableFieldFormatter());
}
/**
* Checks if the field formatter is applicable.
*
* @return \Drupal\Core\Access\AccessResult
* Returns the access result.
*/
protected function isApplicableFieldFormatter() {
$definition = $this->formatterPluginManager
->getDefinition($this
->getFieldFormatterId());
return AccessResult::allowedIf($definition['class']::isApplicable($this
->getFieldDefinition()));
}
/**
* Returns the field formatter id.
*
* @return string|null
* Returns field formatter id or null.
*/
public function getFieldFormatterId() {
return $this
->getDerivativeId();
}
/**
* {@inheritdoc}
*/
public function build() {
// Create a temporary entity to which our fake field value can be
// added.
$fakeEntity = EntityEmbedFakeEntity::create([
'type' => '_entity_embed',
]);
$definition = $this
->getFieldDefinition();
/* @var \Drupal\Core\Field\FieldItemListInterface $items $items */
// Create a field item list object, 1 is the value, array('target_id' => 1)
// would work too, or multiple values. 1 is passed down from the list to the
// field item, which knows that an integer is the ID.
$items = $this->typedDataManager
->create($definition, $this
->getFieldValue($definition), $definition
->getName(), $fakeEntity
->getTypedData());
// Prepare, expects an array of items, keyed by parent entity ID.
$formatter = $this
->getFieldFormatter();
$formatter
->prepareView([
$fakeEntity
->id() => $items,
]);
$build = $formatter
->viewElements($items, $this
->getLangcode());
// For some reason $build[0]['#printed'] is TRUE, which means it will fail
// to render later. So for now we manually fix that.
// @todo Investigate why this is needed.
show($build[0]);
return $build[0];
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return $this->formatterPluginManager
->getDefaultSettings($this
->getFieldFormatterId());
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $this
->getFieldFormatter()
->settingsForm($form, $form_state);
}
/**
* Constructs a field formatter.
*
* @return \Drupal\Core\Field\FormatterInterface
* The formatter object.
*/
public function getFieldFormatter() {
if (!isset($this->fieldFormatter)) {
$display = [
'type' => $this
->getFieldFormatterId(),
'settings' => $this
->getConfiguration(),
'label' => 'hidden',
];
// Create the formatter plugin. Will use the default formatter for that
// field type if none is passed.
$this->fieldFormatter = $this->formatterPluginManager
->getInstance([
'field_definition' => $this
->getFieldDefinition(),
'view_mode' => '_entity_embed',
'configuration' => $display,
]);
}
return $this->fieldFormatter;
}
/**
* Creates a new faux-field definition.
*
* @param string $type
* The type of the field.
*
* @return \Drupal\Core\Field\BaseFieldDefinition
* A new field definition.
*/
protected function createFieldDefinition($type) {
$definition = BaseFieldDefinition::create($type);
static $index = 0;
$definition
->setName('_entity_embed_' . $index++);
return $definition;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$this
->addDependencies(parent::calculateDependencies());
$definition = $this->formatterPluginManager
->getDefinition($this
->getFieldFormatterId());
$this
->addDependency('module', $definition['provider']);
// @todo Investigate why this does not work currently.
// $this->calculatePluginDependencies($this->getFieldFormatter());
return $this->dependencies;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
EntityEmbedDisplayBase:: |
public | property | The attributes on the embedded entity. | |
EntityEmbedDisplayBase:: |
public | property | The context for the plugin. | |
EntityEmbedDisplayBase:: |
protected | property | The entity type manager service. | |
EntityEmbedDisplayBase:: |
protected | property | The language manager. | |
EntityEmbedDisplayBase:: |
public | function | Gets the value for an attribute. | |
EntityEmbedDisplayBase:: |
public | function | Gets the values for all attributes. | |
EntityEmbedDisplayBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
EntityEmbedDisplayBase:: |
public | function | Gets a configuration value. | |
EntityEmbedDisplayBase:: |
public | function | Gets the value for a defined context. | |
EntityEmbedDisplayBase:: |
public | function | Gets the values for all defined contexts. | |
EntityEmbedDisplayBase:: |
public | function | Gets the entity from the current context. | |
EntityEmbedDisplayBase:: |
public | function | Gets the entity type from the current context. | |
EntityEmbedDisplayBase:: |
public | function | Gets the current language code. | |
EntityEmbedDisplayBase:: |
public | function | Checks if an attribute is set. | |
EntityEmbedDisplayBase:: |
public | function | Returns whether or not value is set for a defined context. | |
EntityEmbedDisplayBase:: |
protected | function | Validates that this display plugin applies to the current entity type. | |
EntityEmbedDisplayBase:: |
public | function | Sets the values for all attributes. | |
EntityEmbedDisplayBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
EntityEmbedDisplayBase:: |
public | function | Sets the value for a defined context. | |
EntityEmbedDisplayBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
1 |
EntityEmbedDisplayBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
FieldFormatterEntityEmbedDisplayBase:: |
protected | property | The field definition. | |
FieldFormatterEntityEmbedDisplayBase:: |
protected | property | The field formatter. | |
FieldFormatterEntityEmbedDisplayBase:: |
protected | property | The field formatter plugin manager. | |
FieldFormatterEntityEmbedDisplayBase:: |
protected | property | The typed data manager. | |
FieldFormatterEntityEmbedDisplayBase:: |
public | function |
Indicates whether this Entity Embed display can be used. Overrides EntityEmbedDisplayBase:: |
1 |
FieldFormatterEntityEmbedDisplayBase:: |
public | function |
Builds the renderable array for this Entity Embed display plugin. Overrides EntityEmbedDisplayBase:: |
1 |
FieldFormatterEntityEmbedDisplayBase:: |
public | function |
Form constructor. Overrides EntityEmbedDisplayBase:: |
2 |
FieldFormatterEntityEmbedDisplayBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides EntityEmbedDisplayBase:: |
1 |
FieldFormatterEntityEmbedDisplayBase:: |
public static | function |
Creates an instance of the plugin. Overrides EntityEmbedDisplayBase:: |
1 |
FieldFormatterEntityEmbedDisplayBase:: |
protected | function | Creates a new faux-field definition. | |
FieldFormatterEntityEmbedDisplayBase:: |
public | function |
Gets default configuration for this plugin. Overrides EntityEmbedDisplayBase:: |
1 |
FieldFormatterEntityEmbedDisplayBase:: |
public | function | Get the FieldDefinition object required to render this field's formatter. | 1 |
FieldFormatterEntityEmbedDisplayBase:: |
public | function | Constructs a field formatter. | 1 |
FieldFormatterEntityEmbedDisplayBase:: |
public | function | Returns the field formatter id. | 1 |
FieldFormatterEntityEmbedDisplayBase:: |
abstract public | function | Get the field value required to pass into the field formatter. | 1 |
FieldFormatterEntityEmbedDisplayBase:: |
protected | function | Checks if the field formatter is applicable. | 1 |
FieldFormatterEntityEmbedDisplayBase:: |
public | function |
Constructs a FieldFormatterEntityEmbedDisplayBase object. Overrides EntityEmbedDisplayBase:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. | 1 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |