Commit 3b5cf041 authored by jurgenhaas's avatar jurgenhaas

Migrate module from D7 to D8

parent a6615c0d
name = "Colorbox Field Formatter"
description = "A field formatter for various field types like text, computed, entityreference or image to format those field in display styles or views as links to a colorbox."
core = "7.x"
package = "colorbox"
dependencies[] = "colorbox"
name: Colorbox Field Formatter
type: module
description: A field formatter for various field types like text, computed, entityreference or image to format those field in display styles or views as links to a colorbox.
package: Media
core: 8.x
dependencies:
- colorbox
This diff is collapsed.
{
"name": "drupal/colorbox_field_formatter",
"type": "drupal-module",
"description": "Colorbox Field Formatter",
"keywords": ["Drupal", "Colorbox"],
"license": "GPL-2.0+",
"homepage": "https://www.drupal.org/project/colorbox_field_formatter",
"minimum-stability": "dev",
"support": {
"issues": "http://drupal.org/project/issues/colorbox_field_formatter",
"source": "http://cgit.drupalcode.org/colorbox_field_formatter"
},
"require": { }
}
<?php
/**
* @file
* Contains \Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter\ColorboxFieldFormatter.
*/
namespace Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'colorbox_field_formatter' formatter.
*
* @FieldFormatter(
* id = "colorbox_field_formatter",
* label = @Translation("Colorbox FF"),
* field_types = {
* "computed",
* "string"
* }
* )
*/
class ColorboxFieldFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$config = \Drupal::config('colorbox.settings');
return array(
'style' => $config->get('custom.style'),
'link_type' => 'content',
'link' => '',
'width' => '500',
'height' => '500',
'iframe' => 0,
'anchor' => '',
'class' => '',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form += parent::settingsForm($form, $form_state);
$form['style'] = array(
'#title' => $this->t('Style of colorbox'),
'#type' => 'select',
'#default_value' => $this->getSetting('style'),
'#options' => $this->getStyles(),
);
$form['link_type'] = array(
'#title' => $this->t('Link colorbox to'),
'#type' => 'select',
'#default_value' => $this->getSetting('link_type'),
'#options' => $this->getLinkTypes(),
'#attributes' => array(
'class' => array('colorbox-field-formatter-link-type'),
),
);
$form['link'] = array(
'#title' => $this->t('URI'),
'#type' => 'textfield',
'#default_value' => $this->getSetting('link'),
'#states' => array(
'visible' => array(
'select.colorbox-field-formatter-link-type' => array('value' => 'manual'),
),
),
);
if (\Drupal::moduleHandler()->moduleExists('token')) {
$form['token_help_wrapper'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
'select.colorbox-field-formatter-link-type' => array('value' => 'manual'),
),
),
);
$form['token_help_wrapper']['token_help'] = array(
'#theme' => 'token_tree',
'#token_types' => array('entity' => $form['#entity_type']),
'#global_types' => FALSE,
);
}
$form['width'] = array(
'#title' => $this->t('Width'),
'#type' => 'textfield',
'#default_value' => $this->getSetting('width'),
);
$form['height'] = array(
'#title' => $this->t('Height'),
'#type' => 'textfield',
'#default_value' => $this->getSetting('height'),
);
$form['iframe'] = array(
'#title' => $this->t('iFrame Mode'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('iframe'),
);
$form['anchor'] = array(
'#title' => $this->t('Anchor'),
'#type' => 'textfield',
'#default_value' => $this->getSetting('anchor'),
);
$form['class'] = array(
'#title' => $this->t('Class'),
'#type' => 'textfield',
'#default_value' => $this->getSetting('class'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$styles = $this->getStyles();
$summary[] = $this->t('Style: @style', ['@style' => $styles[$this->getSetting('style')],]);
$types = $this->getLinkTypes();
switch ($this->getSetting('link_type')) {
case 'manual':
$summary[] = $this->t('Link to @link', ['@link' => $this->getSetting('link'),]);
break;
default:
$summary[] = $this->t('Link to @link', ['@link' => $types[$this->getSetting('link_type')],]);
break;
}
$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('style') ? $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'),]);
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = array();
foreach ($items as $delta => $item) {
$output = $this->viewValue($item);
/*if (!$uri) {
if ($settings['colorbox_field_formatter_link_type'] == 'content') {
$uri = entity_uri($entity_type, $entity);
}
else {
$link = module_exists('token') ? token_replace($settings['colorbox_field_formatter_link'], array($entity_type => $entity)) : $settings['colorbox_field_formatter_link'];
$uri = array('path' => $link);
}
}*/
$uri = '#';
$options = array(
'html' => TRUE,
'attributes' => array(
'class' => array($this->getSetting('style')),
),
'query' => array(
'width' => $this->getSetting('width'),
'height' => $this->getSetting('height'),
),
);
if ($this->getSetting('iframe')) {
$options['query']['iframe'] = 'true';
}
if (!empty($this->getSetting('anchor'))) {
$options['query']['fragment'] = $this->getSetting('anchor');
}
if (!empty($this->getSetting('class'))) {
$options['attributes']['class'] = array_merge($options['attributes']['class'], explode(' ', $this->getSetting('class')));
}
$element[$delta] = array(
'#markup' => l($output, $uri['path'], $options),
);
}
return $element;
}
/**
* Generate the output appropriate for one field item.
*
* @param \Drupal\Core\Field\FieldItemInterface $item
* One field item.
*
* @return string
* The textual output generated.
*/
protected function viewValue(FieldItemInterface $item) {
// The text value has no text format assigned to it, so the user input
// should equal the output, including newlines.
return nl2br(Html::escape($item->value));
}
/**
* Callback to provide an array for a select field containing all supported
* colorbox styles.
*
* @return array
*/
private function getStyles() {
$styles = array(
'default' => $this->t('Default'),
);
// TODO: Add more styles to the array
$config = \Drupal::config('colorbox.settings');
if (\Drupal::moduleHandler()->moduleExists('colorbox_node')) {
$styles['colorbox-node'] = $this->t('Colorbox node');
}
return $styles;
}
/**
* Callback to provide an arry for a select field containing all link types.
*
* @return array
*/
private function getLinkTypes() {
return array(
'content' => $this->t('Content'),
'manual' => $this->t('Manually provide a link'),
);
}
}
<?php
/**
* @file
* Contains \Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter\ColorboxFieldFormatterEntityreference.
*/
namespace Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'colorbox_field_formatter' formatter for entityreferences.
*
* @FieldFormatter(
* id = "colorbox_field_formatter_entityreference",
* label = @Translation("Colorbox FF"),
* field_types = {
* "entityreference"
* }
* )
*/
class ColorboxFieldFormatterEntityreference extends ColorboxFieldFormatter {
/**
* Generate the output appropriate for one field item.
*
* @param \Drupal\Core\Field\FieldItemInterface $item
* One field item.
*
* @return string
* The textual output generated.
*/
protected function viewValue(FieldItemInterface $item) {
$target = entity_load($instance['entity_type'], array($item['target_id']));
if (is_array($target)) {
$target = reset($target);
}
$output = entity_label($instance['entity_type'], $target);
$uri = entity_uri($instance['entity_type'], $target);
// The text value has no text format assigned to it, so the user input
// should equal the output, including newlines.
return nl2br(Html::escape($item->value));
}
}
<?php
/**
* @file
* Contains \Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter\ColorboxFieldFormatterImage.
*/
namespace Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'colorbox_field_formatter' formatter for images.
*
* @FieldFormatter(
* id = "colorbox_field_formatter_image",
* label = @Translation("Colorbox FF"),
* field_types = {
* "image"
* }
* )
*/
class ColorboxFieldFormatterImage extends ColorboxFieldFormatter {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'image_style' => 'original',
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$image_styles = image_style_options(FALSE);
$image_styles['hide'] = t('Hide (do not display image)');
$form['image_style'] = array(
'#title' => $this->t('Content image style'),
'#type' => 'select',
'#default_value' => $this->getSetting('image_style'),
'#empty_option' => $this->t('None (original image)'),
'#options' => $image_styles,
'#description' => $this->t('Image style to use in the content.'),
);
return parent::settingsForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$image_style = $this->getSetting('image_style');
$image_styles = image_style_options(FALSE);
unset($image_styles['']);
if (isset($image_styles[$image_style])) {
$style = $image_styles[$image_style];
}
elseif ($image_style == 'hide') {
$style = $this->t('Hide');
}
else {
$style = $this->t('Original image');
}
return array(
$this->t('Content image style: @style', ['@style' => $style]),
) + parent::settingsSummary();
}
/**
* Generate the output appropriate for one field item.
*
* @param \Drupal\Core\Field\FieldItemInterface $item
* One field item.
*
* @return string
* The textual output generated.
*/
protected function viewValue(FieldItemInterface $item) {
$image = array(
'path' => $item['uri'],
'alt' => isset($item['alt']) ? $item['alt'] : '',
'title' => isset($item['title']) ? $item['title'] : '',
'style_name' => $settings['colorbox_field_formatter_image_style'],
);
if (empty($settings['colorbox_field_formatter_image_style'])) {
$output = theme('image', $image);
}
else {
$output = theme('image_style', $image);
}
// The text value has no text format assigned to it, so the user input
// should equal the output, including newlines.
return nl2br(Html::escape($item->value));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment