View source
<?php
namespace Drupal\background_image_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
class BackgroundImageFormatter extends ImageFormatter {
public static function defaultSettings() {
return [
'image_style' => '',
'background_image_output_type' => 'inline',
'background_image_selector' => '',
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$image_styles = image_style_options(FALSE);
$element['image_style'] = [
'#title' => $this
->t('Image style'),
'#type' => 'select',
'#options' => $image_styles,
'#default_value' => $this
->getSetting('image_style'),
'#empty_option' => $this
->t('None (original image)'),
'#description' => $this
->t('Select the image style to use.'),
];
$element['background_image_output_type'] = [
'#title' => $this
->t('Output To'),
'#type' => 'select',
'#options' => [
'inline' => $this
->t('Write background-image to inline style attribute'),
'css' => $this
->t('Write background-image to CSS selector'),
],
'#default_value' => $this
->getSetting('background_image_output_type'),
'#required' => TRUE,
'#description' => $this
->t('Define how background-image will be printed to the dom.'),
];
$element['background_image_selector'] = [
'#title' => $this
->t('CSS Selector'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('background_image_selector'),
'#required' => FALSE,
'#description' => $this
->t('CSS selector that image(s) are attached to.'),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$image_styles = image_style_options(FALSE);
unset($image_styles['']);
$select_style = $this
->getSetting('image_style');
if (isset($image_styles[$select_style])) {
$summary[] = $this
->t('URL for image style: @style', [
'@style' => $image_styles[$select_style],
]);
}
else {
$summary[] = $this
->t('Original image');
}
$summary[] = $this
->t('Output type: @output_type', [
'@output_type' => $this
->getSetting('background_image_output_type'),
]);
$summary[] = $this
->t('The CSS selector <code>@background_image_selector</code> will be created with the image set to the background-image property.', [
'@background_image_selector' => $this
->getSetting('background_image_selector') . '_id',
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$image_style = NULL;
if (!$this
->isBackgroundImageDisplay()) {
return $elements;
}
$image_style = $this
->getSetting('image_style');
if (!empty($image_style)) {
$image_style = ImageStyle::load($image_style);
}
$files = $this
->getEntitiesToView($items, $langcode);
foreach ($files as $delta => $entity) {
if ($files instanceof EntityInterface) {
continue;
}
$image_url = file_url_transform_relative(file_create_url($entity
->getFileUri()));
$id = $entity
->id();
if ($image_style) {
$image_uri = $entity
->getFileUri();
$image_url = ImageStyle::load($image_style
->getName())
->buildUrl($image_uri);
}
$selector = strip_tags($this
->getSetting('background_image_selector'));
if ($this
->getSetting('background_image_output_type') === 'inline') {
$selector .= '_' . $id;
}
$theme = [
'#background_image_selector' => $selector,
'#image_uri' => $image_url,
];
array_push($elements, $this
->renderElements($delta, $theme, $id));
}
return $elements;
}
protected function isBackgroundImageDisplay() {
return $this
->getPluginId() == 'background_image_formatter';
}
protected function generateCssString($theme) {
return $theme['#background_image_selector'] . ' {background-image: url("' . $theme['#image_uri'] . '");}';
}
public function renderElements($delta, array $theme, $id) {
$elements = NULL;
switch ($this
->getSetting('background_image_output_type')) {
case 'css':
$data = [
'#tag' => 'style',
'#value' => $this
->generateCssString($theme),
];
$elements['#attached']['html_head'][] = [
$data,
'background_image_formatter_' . $id,
];
break;
case 'inline':
$theme['#theme'] = 'background_image_formatter_inline';
$elements[$delta] = [
'#markup' => \Drupal::service('renderer')
->render($theme),
];
break;
}
return $elements;
}
}