public function ImagePopupFieldFormatter::viewElements in Simple Image Popup 2.x
Same name and namespace in other branches
- 8 src/Plugin/Field/FieldFormatter/ImagePopupFieldFormatter.php \Drupal\image_popup\Plugin\Field\FieldFormatter\ImagePopupFieldFormatter::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/ ImagePopupFieldFormatter.php, line 227
Class
- ImagePopupFieldFormatter
- Plugin implementation of the 'image_popup_field_formatter' formatter.
Namespace
Drupal\image_popup\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
// Set this->parent so that alt and title text from can be taken from the
// parent entity later on.
if (!$items
->isEmpty()) {
$this->parent = $items[0]
->getEntity();
}
$files = $this
->getEntitiesToView($items, $langcode);
// If there are no files, return an empty element.
if (empty($files)) {
return [];
}
$image_style_popup = $this
->getSetting('image_style_popup');
$image_style_name = $this
->getSetting('image_style');
$image_fid = $files[0]
->get('fid')
->getValue()[0]['value'];
$image_style = ImageStyle::load($image_style_name);
$config_name = "image.style." . $image_style_popup;
$image_style_popup_settings = $this->configFactory
->get($config_name)
->getRawData();
$popup_width = 750;
if (!empty($image_style_popup_settings['effects'])) {
foreach ($image_style_popup_settings['effects'] as $effect) {
if ($effect['id'] == 'image_scale') {
$popup_width = $effect['data']['width'];
}
}
}
foreach ($files as $delta => $file) {
$image_uri = $file
->getFileUri();
$parameters = [
'fid' => $image_fid,
];
if ($image_style_popup) {
$parameters['image_style'] = $image_style_popup;
}
if ($image_style) {
$absolute_path = $this->imageStyleStorage
->load($image_style_name)
->buildUrl($image_uri);
}
else {
// Get absolute path for original image.
$absolute_path = Url::fromUri(file_create_url($image_uri))
->getUri();
}
// Create a url query to tell the controller how to build the modal.
$options = [
'query' => [
'alt' => $this
->getText('image_popup', 'alt'),
'title' => $this
->getText('image_popup', 'title'),
'modal-options' => Json::encode([
'width' => $popup_width,
]),
],
];
// Create the link.
$link = [
'#type' => 'link',
'#title' => [
'#theme' => 'image',
'#alt' => $this
->getText('image', 'alt'),
'#title' => $this
->getText('image', 'title'),
'#uri' => $absolute_path,
],
'#attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => $popup_width,
]),
],
'#url' => Url::fromRoute('image_popup.image_popup_render', $parameters, $options),
'#attached' => [
'library' => [
'core/drupal.dialog.ajax',
],
],
];
$elements[$delta] = $link;
}
return $elements;
}