class EntityReferenceFieldFormatter in Entity Embed 8
Entity Embed Display reusing entity reference field formatters.
Plugin annotation
@EntityEmbedDisplay(
id = "entity_reference",
label = @Translation("Entity Reference"),
deriver = "Drupal\entity_embed\Plugin\Derivative\FieldFormatterDeriver",
field_type = "entity_reference",
supports_image_alt_and_title = TRUE
)
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\Plugin\entity_embed\EntityEmbedDisplay\EntityReferenceFieldFormatter implements TrustedCallbackInterface
- 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 EntityReferenceFieldFormatter
See also
\Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface
File
- src/
Plugin/ entity_embed/ EntityEmbedDisplay/ EntityReferenceFieldFormatter.php, line 29
Namespace
Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplayView source
class EntityReferenceFieldFormatter extends FieldFormatterEntityEmbedDisplayBase implements TrustedCallbackInterface {
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a new EntityReferenceFieldFormatter.
*
* @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.
* @param \Drupal\Core\Config\ConfigFactoryInterface|null $config_factory
* The configuration factory, or null to get from global container for
* backwards compatibility.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $formatter_plugin_manager, $typed_data_manager, $language_manager);
$this->configFactory = $config_factory instanceof ConfigFactoryInterface ? $config_factory : \Drupal::configFactory();
}
/**
* {@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'), $container
->get('config.factory'));
}
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return [
'disableContextualLinks',
'disableQuickEdit',
];
}
/**
* {@inheritdoc}
*/
public function getFieldDefinition() {
if (!isset($this->fieldDefinition)) {
$this->fieldDefinition = parent::getFieldDefinition();
$this->fieldDefinition
->setSetting('target_type', $this
->getEntityTypeFromContext());
}
return $this->fieldDefinition;
}
/**
* {@inheritdoc}
*/
public function getFieldValue() {
return [
'target_id' => $this
->getContextValue('entity')
->id(),
];
}
/**
* {@inheritdoc}
*/
protected function isApplicableFieldFormatter() {
$access = parent::isApplicableFieldFormatter();
// Don't bother checking if not allowed.
if ($access
->isAllowed()) {
if ($this
->getPluginId() === 'entity_reference:entity_reference_entity_view') {
// This option disables entity_reference_entity_view plugin for content
// entity types. If it is truthy then the plugin is enabled for all
// entity types.
$mode = $this->configFactory
->get('entity_embed.settings')
->get('rendered_entity_mode');
if ($mode) {
// Return *allowed* object.
return $access;
}
// Only allow this if this is not a content entity type.
$entity_type_id = $this
->getEntityTypeFromContext();
if ($entity_type_id) {
$definition = $this->entityTypeManager
->getDefinition($entity_type_id);
return $access
->andIf(AccessResult::allowedIf(!$definition
->entityClassImplements(ContentEntityInterface::class)));
}
}
}
return $access;
}
/**
* {@inheritdoc}
*/
public function build() {
$build = parent::build();
// Early return if this derived plugin is not using an EntityViewBuilder.
// @see \Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults()
if (!isset($build['#view_mode'])) {
return $build;
}
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $this
->getEntityFromContext();
// There are a few concerns when rendering an embedded media entity:
// - entity access checking happens not during rendering but during routing,
// and therefore we have to do it explicitly here for the embedded entity.
$build['#access'] = $entity
->access('view', NULL, TRUE);
// - caching an embedded entity separately is unnecessary; the host entity
// is already render cached; plus specific values may be overridden (such
// as an `alt` attribute) which would mean this particular rendered
// representation is unique to the host entity and hence nonsensical to
// cache separately anyway.
unset($build['#cache']['keys']);
// - Contextual Links do not make sense for embedded entities; we only allow
// the host entity to be contextually managed.
$build['#pre_render'][] = static::class . '::disableContextualLinks';
// - Quick Edit does not make sense for embedded entities; we only allow the
// host entity to be edited in-place.
$build['#pre_render'][] = static::class . '::disableQuickEdit';
// - default styling may break captioned media embeds; attach asset library
// to ensure captions behave as intended.
$build['#attached']['library'][] = 'entity_embed/caption';
return $build;
}
/**
* Disables Contextual Links for the embedded media by removing its property.
*
* @param array $build
* The render array for the embedded media.
*
* @return array
* The updated render array.
*
* @see \Drupal\Core\Entity\EntityViewBuilder::addContextualLinks()
*/
public static function disableContextualLinks(array $build) {
unset($build['#contextual_links']);
return $build;
}
/**
* Disables Quick Edit for the embedded media by removing its attributes.
*
* @param array $build
* The render array for the embedded media.
*
* @return array
* The updated render array.
*
* @see quickedit_entity_view_alter()
*/
public static function disableQuickEdit(array $build) {
unset($build['#attributes']['data-quickedit-entity-id']);
return $build;
}
}
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:: |
|
EntityReferenceFieldFormatter:: |
protected | property | The configuration factory. | |
EntityReferenceFieldFormatter:: |
public | function |
Builds the renderable array for this Entity Embed display plugin. Overrides FieldFormatterEntityEmbedDisplayBase:: |
|
EntityReferenceFieldFormatter:: |
public static | function |
Creates an instance of the plugin. Overrides FieldFormatterEntityEmbedDisplayBase:: |
1 |
EntityReferenceFieldFormatter:: |
public static | function | Disables Contextual Links for the embedded media by removing its property. | |
EntityReferenceFieldFormatter:: |
public static | function | Disables Quick Edit for the embedded media by removing its attributes. | |
EntityReferenceFieldFormatter:: |
public | function |
Get the FieldDefinition object required to render this field's formatter. Overrides FieldFormatterEntityEmbedDisplayBase:: |
|
EntityReferenceFieldFormatter:: |
public | function |
Get the field value required to pass into the field formatter. Overrides FieldFormatterEntityEmbedDisplayBase:: |
1 |
EntityReferenceFieldFormatter:: |
protected | function |
Checks if the field formatter is applicable. Overrides FieldFormatterEntityEmbedDisplayBase:: |
|
EntityReferenceFieldFormatter:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
|
EntityReferenceFieldFormatter:: |
public | function |
Constructs a new EntityReferenceFieldFormatter. Overrides FieldFormatterEntityEmbedDisplayBase:: |
1 |
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 |
Form constructor. Overrides EntityEmbedDisplayBase:: |
2 |
FieldFormatterEntityEmbedDisplayBase:: |
public | function |
Calculates dependencies for the configured 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 | Constructs a field formatter. | 1 |
FieldFormatterEntityEmbedDisplayBase:: |
public | function | Returns the field formatter id. | 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. | |
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. |