public function TextimageTextFieldFormatter::viewElements in Textimage 8.4
Same name and namespace in other branches
- 8.3 src/Plugin/Field/FieldFormatter/TextimageTextFieldFormatter.php \Drupal\textimage\Plugin\Field\FieldFormatter\TextimageTextFieldFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ TextimageTextFieldFormatter.php, line 268
Class
- TextimageTextFieldFormatter
- Plugin implementation of the Textimage text field formatter.
Namespace
Drupal\textimage\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
// Get image style.
$image_style = $this->imageStyleStorage
->load($this
->getSetting('image_style'));
// Collect bubbleable metadata.
$bubbleable_metadata = new BubbleableMetadata();
// Provide token data for the displayed entity.
$instance = $items
->getFieldDefinition();
$field = $instance
->getFieldStorageDefinition();
$token_data = [
$instance
->getTargetEntityTypeId() => $items
->getEntity(),
];
// Get text strings from the text field.
$text = $this->textimageFactory
->getTextFieldText($items);
// Get alt and title text from the formatter settings, and resolve tokens.
if ($image_alt = $this
->getSetting('image_alt')) {
$image_alt = $this->textimageFactory
->processTextString($image_alt, NULL, $token_data, $bubbleable_metadata);
}
if ($image_title = $this
->getSetting('image_title')) {
$image_title = $this->textimageFactory
->processTextString($image_title, NULL, $token_data, $bubbleable_metadata);
}
// Check if the formatter involves a link to the parent entity.
$entity_url = $this
->getSetting('image_link') == 'content' ? $items
->getEntity()
->toUrl() : NULL;
$elements = [];
if ($field
->getCardinality() != 1 && $this
->getSetting('image_text_values') == 'itemize') {
// Build separate image for each text value.
foreach ($text as $text_value) {
$textimage = $this->textimageFactory
->get($bubbleable_metadata)
->setStyle($image_style)
->setTokenData($token_data)
->process($text_value);
if (!$this
->getSetting('image_build_deferred')) {
$textimage
->buildImage();
}
// Check if the formatter involves a link to the derived image.
if (!$entity_url && $this
->getSetting('image_link') == 'file') {
$url = $textimage
->getUrl();
}
else {
$url = NULL;
}
$element = [
'#theme' => 'textimage_formatter',
'#uri' => $textimage
->getUri(),
'#width' => $textimage
->getWidth(),
'#height' => $textimage
->getHeight(),
'#alt' => $image_alt,
'#title' => $image_title,
'#anchor_url' => $entity_url ?: $url,
];
$bubbleable_metadata
->applyTo($element);
$elements[] = $element;
}
}
else {
// Build single image with all text values.
$textimage = $this->textimageFactory
->get($bubbleable_metadata)
->setStyle($image_style)
->setTokenData($token_data)
->process($text);
if (!$this
->getSetting('image_build_deferred')) {
$textimage
->buildImage();
}
// Check if the formatter involves a link to the derived image.
if (!$entity_url && $this
->getSetting('image_link') == 'file') {
$url = $textimage
->getUrl();
}
else {
$url = NULL;
}
$element = [
'#theme' => 'textimage_formatter',
'#uri' => $textimage
->getUri(),
'#width' => $textimage
->getWidth(),
'#height' => $textimage
->getHeight(),
'#alt' => $image_alt,
'#title' => $image_title,
'#anchor_url' => $entity_url ?: $url,
];
$bubbleable_metadata
->applyTo($element);
$elements[] = $element;
}
return $elements;
}