View source
<?php
namespace Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\colorbox\ColorboxAttachment;
use Drupal\Component\Utility\Html;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\Token;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ColorboxFieldFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $moduleHandler;
protected $colorboxAttachment;
protected $token;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ModuleHandlerInterface $module_handler, ColorboxAttachment $colorbox_attachment, Token $token) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->moduleHandler = $module_handler;
$this->colorboxAttachment = $colorbox_attachment;
$this->token = $token;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('module_handler'), $container
->get('colorbox.attachment'), $container
->get('token'));
}
public static function defaultSettings() {
return [
'style' => 'default',
'link_type' => 'content',
'link' => '',
'width' => '500',
'height' => '500',
'iframe' => 0,
'inline_selector' => '',
'anchor' => '',
'class' => '',
'rel' => '',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['style'] = [
'#title' => $this
->t('Style of colorbox'),
'#type' => 'select',
'#default_value' => $this
->getSetting('style'),
'#options' => $this
->getStyles(),
'#attributes' => [
'class' => [
'colorbox-field-formatter-style',
],
],
];
$form['link_type'] = [
'#title' => $this
->t('Link colorbox to'),
'#type' => 'select',
'#default_value' => $this
->getSetting('link_type'),
'#options' => $this
->getLinkTypes(),
'#attributes' => [
'class' => [
'colorbox-field-formatter-link-type',
],
],
'#states' => [
'visible' => [
'select.colorbox-field-formatter-style' => [
'value' => 'default',
],
],
],
];
$form['link'] = [
'#title' => $this
->t('URI'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('link'),
'#states' => [
'visible' => [
'select.colorbox-field-formatter-style' => [
'value' => 'default',
],
'select.colorbox-field-formatter-link-type' => [
'value' => 'manual',
],
],
],
];
if (isset($form['#entity_type']) && $this->moduleHandler
->moduleExists('token')) {
$form['token_help_wrapper'] = [
'#type' => 'container',
'#states' => [
'visible' => [
'select.colorbox-field-formatter-style' => [
'value' => 'default',
],
'select.colorbox-field-formatter-link-type' => [
'value' => 'manual',
],
],
],
];
$form['token_help_wrapper']['token_help'] = [
'#theme' => 'token_tree',
'#token_types' => [
'entity' => $form['#entity_type'],
],
'#global_types' => FALSE,
];
}
$form['inline_selector'] = [
'#title' => $this
->t('Inline selector'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('inline_selector'),
'#states' => [
'visible' => [
'select.colorbox-field-formatter-style' => [
'value' => 'colorbox-inline',
],
],
],
];
$form['width'] = [
'#title' => $this
->t('Width'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('width'),
];
$form['height'] = [
'#title' => $this
->t('Height'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('height'),
];
$form['iframe'] = [
'#title' => $this
->t('iFrame Mode'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('iframe'),
];
$form['anchor'] = [
'#title' => $this
->t('Anchor'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('anchor'),
];
$form['class'] = [
'#title' => $this
->t('Class'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('class'),
];
$form['rel'] = [
'#title' => $this
->t('Rel'),
'#type' => 'textfield',
'#default_value' => $this
->getSetting('rel'),
'#description' => $this
->t('This can be used to identify a group for Colorbox to cycle through.'),
];
return $form;
}
public function settingsSummary() {
$summary = [];
$styles = $this
->getStyles();
$summary[] = $this
->t('Style: @style', [
'@style' => $styles[$this
->getSetting('style')],
]);
if ($this
->getSetting('style') === 'default') {
$types = $this
->getLinkTypes();
if ($this
->getSetting('link_type') === 'manual') {
$summary[] = $this
->t('Link to @link', [
'@link' => $this
->getSetting('link'),
]);
}
else {
$summary[] = $this
->t('Link to @link', [
'@link' => $types[$this
->getSetting('link_type')],
]);
}
}
if ($this
->getSetting('style') === 'colorbox-inline') {
$summary[] = $this
->t('Inline selector: @selector', [
'@selector' => $this
->getSetting('inline_selector'),
]);
}
$summary[] = $this
->t('Width: @width', [
'@width' => $this
->getSetting('width'),
]);
$summary[] = $this
->t('Height: @height', [
'@height' => $this
->getSetting('height'),
]);
$summary[] = $this
->t('iFrame Mode: @mode', [
'@mode' => $this
->getSetting('iframe') ? $this
->t('Yes') : $this
->t('No'),
]);
if (!empty($this
->getSetting('anchor'))) {
$summary[] = $this
->t('Anchor: #@anchor', [
'@anchor' => $this
->getSetting('anchor'),
]);
}
if (!empty($this
->getSetting('class'))) {
$summary[] = $this
->t('Classes: @class', [
'@class' => $this
->getSetting('class'),
]);
}
if (!empty($this
->getSetting('rel'))) {
$summary[] = $this
->t('Rel: @rel', [
'@rel' => $this
->getSetting('rel'),
]);
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) : array {
$element = [];
foreach ($items as $delta => $item) {
$output = $this
->viewValue($item);
$url = $this
->getUrl($item);
$options = [
'html' => TRUE,
'attributes' => [
'class' => [
'colorbox',
$this
->getSetting('style'),
],
],
'query' => [
'width' => $this
->getSetting('width'),
'height' => $this
->getSetting('height'),
],
];
if ($this
->getSetting('iframe')) {
$options['query']['iframe'] = 'true';
}
if (!empty($this
->getSetting('anchor'))) {
$options['fragment'] = $this
->getSetting('anchor');
}
if (!empty($this
->getSetting('class'))) {
$options['attributes']['class'] = array_merge($options['attributes']['class'], explode(' ', $this
->getSetting('class')));
}
if (!empty($this
->getSetting('rel'))) {
$options['attributes']['rel'] = $this
->getSetting('rel');
}
if ($this
->getSetting('style') === 'colorbox-inline') {
$options['attributes']['data-colorbox-inline'] = $this
->getSetting('inline_selector');
}
$url
->setOptions($options);
$link = Link::fromTextAndUrl($output, $url);
$element[$delta] = $link
->toRenderable();
}
if ($this->colorboxAttachment
->isApplicable()) {
$this->colorboxAttachment
->attach($element);
}
return $element;
}
protected function viewValue(FieldItemInterface $item) {
return nl2br(Html::escape($item->value));
}
protected function getUrl(FieldItemInterface $item) : Url {
$entity = $item
->getEntity();
if ($this
->getSetting('link_type') === 'content') {
return $entity
->toUrl();
}
$link = $this
->getSetting('link');
if ($this->moduleHandler
->moduleExists('token')) {
$link = $this->token
->replace($this
->getSetting('link'), [
$entity
->bundle() => $entity,
], [
'clear' => TRUE,
]);
}
return Url::fromUserInput($link);
}
private function getStyles() : array {
$styles = [
'default' => $this
->t('Default'),
];
if ($this->moduleHandler
->moduleExists('colorbox_inline')) {
$styles['colorbox-inline'] = $this
->t('Colorbox inline');
}
if ($this->moduleHandler
->moduleExists('colorbox_node')) {
$styles['colorbox-node'] = $this
->t('Colorbox node');
}
return $styles;
}
private function getLinkTypes() : array {
return [
'content' => $this
->t('Content'),
'manual' => $this
->t('Manually provide a link'),
];
}
}