View source
<?php
namespace Drupal\magnific_popup\Plugin\Field\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase;
use Drupal\image\Entity\ImageStyle;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
class MagnificPopup extends ImageFormatterBase {
public static function defaultSettings() {
return [
'thumbnail_image_style' => '',
'popup_image_style' => '',
'gallery_type' => 'all_items',
];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$image_styles = image_style_options(FALSE);
$form['thumbnail_image_style'] = [
'#title' => $this
->t('Thumbnail Image Style'),
'#type' => 'select',
'#default_value' => $this
->getSetting('thumbnail_image_style'),
'#empty_option' => $this
->t('None (original image)'),
'#options' => $image_styles,
];
$form['popup_image_style'] = [
'#title' => $this
->t('Popup Image Style'),
'#type' => 'select',
'#default_value' => $this
->getSetting('popup_image_style'),
'#empty_option' => $this
->t('None (original image)'),
'#options' => $image_styles,
];
$form['gallery_type'] = [
'#title' => $this
->t('Gallery Type'),
'#type' => 'select',
'#default_value' => $this
->getSetting('gallery_type'),
'#options' => $this
->getGalleryTypes(),
];
return $form;
}
public function settingsSummary() {
$image_styles = image_style_options(FALSE);
$thumb_image_style = $this
->getSetting('thumbnail_image_style');
$popup_image_style = $this
->getSetting('popup_image_style');
$summary[] = $this
->t('Thumbnail image style: @thumb_style. Popup image style: @popup_style', [
'@thumb_style' => isset($image_styles[$thumb_image_style]) ? $thumb_image_style : 'Original Image',
'@popup_style' => isset($image_styles[$popup_image_style]) ? $popup_image_style : 'Original Image',
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$thumb_image_style = $this
->getSetting('thumbnail_image_style');
$popup_image_style = $this
->getSetting('popup_image_style');
$gallery_type = $this
->getSetting('gallery_type');
$files = $this
->getEntitiesToView($items, $langcode);
foreach ($files as $delta => $file) {
$image_uri = $file
->getFileUri();
$popup_image_path = !empty($popup_image_style) ? ImageStyle::load($popup_image_style)
->buildUrl($image_uri) : $image_uri;
$url = Url::fromUri(file_create_url($popup_image_path));
$item = $file->_referringItem;
$item_attributes = $file->_attributes;
unset($file->_attributes);
$item_attributes['class'][] = 'mfp-thumbnail';
if ($gallery_type === 'first_item' && $delta > 0) {
$item_attributes['class'][] = 'visually-hidden';
}
$elements[$delta] = [
'#theme' => 'image_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $thumb_image_style,
'#url' => $url,
'#attached' => [
'library' => [
'magnific_popup/magnific_popup',
],
],
];
}
return $elements;
}
public function view(FieldItemListInterface $items, $langcode = NULL) {
$elements = parent::view($items, $langcode);
$gallery_type = $this
->getSetting('gallery_type');
$elements['#attributes']['class'][] = 'mfp-field';
$elements['#attributes']['class'][] = 'mfp-' . Html::cleanCssIdentifier($gallery_type);
return $elements;
}
protected function getGalleryTypes() {
return [
'all_items' => $this
->t('Gallery: All Items Displayed'),
'first_item' => $this
->t('Gallery: First Item Displayed'),
'separate_items' => $this
->t('No Gallery: Display Each Item Separately'),
];
}
}