class ExampleMultilingualField in Extra Field 8.2
Same name and namespace in other branches
- 8 modules/extra_field_example/src/Plugin/ExtraField/Display/ExampleMultilingualField.php \Drupal\extra_field_example\Plugin\ExtraField\Display\ExampleMultilingualField
Example Extra field Display.
Plugin annotation
@ExtraFieldDisplay(
id = "multilingual_field",
label = @Translation("Concatenated tags"),
description = @Translation("An extra field to display concatenated tags."),
bundles = {
"node.article"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\extra_field\Plugin\ExtraFieldDisplayBase implements ExtraFieldDisplayInterface
- class \Drupal\extra_field\Plugin\ExtraFieldDisplayFormattedBase implements ExtraFieldDisplayFormattedInterface
- class \Drupal\extra_field_example\Plugin\ExtraField\Display\ExampleMultilingualField
- class \Drupal\extra_field\Plugin\ExtraFieldDisplayFormattedBase implements ExtraFieldDisplayFormattedInterface
- class \Drupal\extra_field\Plugin\ExtraFieldDisplayBase implements ExtraFieldDisplayInterface
Expanded class hierarchy of ExampleMultilingualField
File
- modules/
extra_field_example/ src/ Plugin/ ExtraField/ Display/ ExampleMultilingualField.php, line 21
Namespace
Drupal\extra_field_example\Plugin\ExtraField\DisplayView source
class ExampleMultilingualField extends ExtraFieldDisplayFormattedBase {
/**
* {@inheritdoc}
*/
public function viewElements(ContentEntityInterface $entity) {
$elements = [];
$cache = new CacheableMetadata();
$tagsField = $this
->getTagsField();
if ($tagsField && !$tagsField
->isEmpty()) {
// Build the field output as a concatenated string of tags.
$tags = [];
foreach ($tagsField as $item) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $tag */
$tag = $item->entity;
$tags[] = $tag
->label();
$cache
->addCacheableDependency($tag);
}
$elements = [
'#markup' => implode(', ', $tags),
];
}
else {
// Mark the result as empty to make sure no field wrapper is applied.
$this->isEmpty = TRUE;
}
$cache
->applyTo($elements);
return $elements;
}
/**
* {@inheritdoc}
*/
public function getLabel() {
$label = '';
if ($tagsField = $this
->getTagsField()) {
// Use the Tags field's label.
$label = $tagsField
->getFieldDefinition()
->getLabel();
}
return $label;
}
/**
* {@inheritdoc}
*/
public function getLabelDisplay() {
// Override the default label display setting.
return 'inline';
}
/**
* {@inheritdoc}
*/
public function isTranslatable() {
// Override the default translatability setting.
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getLangcode() {
if ($tagsField = $this
->getTagsField()) {
$langcode = $tagsField
->getLangcode();
}
else {
$langcode = parent::getLangcode();
}
return $langcode;
}
/**
* Returns the Tags field this plugin uses.
*
* @return \Drupal\Core\Field\FieldItemListInterface|null
* The taxonomy terms this field references.
*/
protected function getTagsField() {
$field = NULL;
$entity = $this
->getEntity();
if ($entity
->hasField('field_tags')) {
$field = $entity
->get('field_tags');
}
return $field;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExampleMultilingualField:: |
public | function |
The label of the field. Overrides ExtraFieldDisplayFormattedBase:: |
|
ExampleMultilingualField:: |
public | function |
How to display the field label will be displayed. Overrides ExtraFieldDisplayFormattedBase:: |
|
ExampleMultilingualField:: |
public | function |
Gets the langcode of the field values. Overrides ExtraFieldDisplayFormattedBase:: |
|
ExampleMultilingualField:: |
protected | function | Returns the Tags field this plugin uses. | |
ExampleMultilingualField:: |
public | function |
The field is translatable. Overrides ExtraFieldDisplayFormattedBase:: |
|
ExampleMultilingualField:: |
public | function |
Returns the renderable array of the field item(s). Overrides ExtraFieldDisplayFormattedInterface:: |
|
ExtraFieldDisplayBase:: |
protected | property | The field's parent entity. | |
ExtraFieldDisplayBase:: |
protected | property | The entity view display. | |
ExtraFieldDisplayBase:: |
protected | property | The view mode the entity is rendered in. | |
ExtraFieldDisplayBase:: |
public | function |
Returns the field's parent entity. Overrides ExtraFieldDisplayInterface:: |
|
ExtraFieldDisplayBase:: |
public | function |
Returns the entity view display object of the field's host entity. Overrides ExtraFieldDisplayInterface:: |
|
ExtraFieldDisplayBase:: |
public | function |
Returns the entity view mode object of the field's host entity. Overrides ExtraFieldDisplayInterface:: |
|
ExtraFieldDisplayBase:: |
public | function |
Stores the field's parent entity. Overrides ExtraFieldDisplayInterface:: |
|
ExtraFieldDisplayBase:: |
public | function |
Stores the entity view display. Overrides ExtraFieldDisplayInterface:: |
|
ExtraFieldDisplayBase:: |
public | function |
Stores the entity view mode. Overrides ExtraFieldDisplayInterface:: |
|
ExtraFieldDisplayFormattedBase:: |
protected | property | Flag to indicate that the extra field has no content. | |
ExtraFieldDisplayFormattedBase:: |
protected | property | The langcode of the field values. | |
ExtraFieldDisplayFormattedBase:: |
protected | property | The language manager. | |
ExtraFieldDisplayFormattedBase:: |
public | function |
The machine name of the field. Overrides ExtraFieldDisplayFormattedInterface:: |
1 |
ExtraFieldDisplayFormattedBase:: |
public | function |
The type of field. Overrides ExtraFieldDisplayFormattedInterface:: |
1 |
ExtraFieldDisplayFormattedBase:: |
public | function |
Check if the extra field has data. Overrides ExtraFieldDisplayFormattedInterface:: |
|
ExtraFieldDisplayFormattedBase:: |
public | function | Set the language code. | |
ExtraFieldDisplayFormattedBase:: |
public | function |
Builds a renderable array for the field. Overrides ExtraFieldDisplayInterface:: |
|
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. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |