Commit a91fa80c authored by jurgenhaas's avatar jurgenhaas

Migrate module from D7 to D8

parent 3b5cf041
......@@ -8,10 +8,14 @@
namespace Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Field\Annotation\FieldFormatter;
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\Url;
/**
* Plugin implementation of the 'colorbox_field_formatter' formatter.
......@@ -142,7 +146,7 @@ class ColorboxFieldFormatter extends FormatterBase {
$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')),]);
$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'),]);
}
......@@ -161,21 +165,11 @@ class ColorboxFieldFormatter extends FormatterBase {
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 = '#';
$url = $this->getUrl($item);
$options = array(
'html' => TRUE,
'attributes' => array(
'class' => array($this->getSetting('style')),
'class' => array('colorbox', $this->getSetting('style')),
),
'query' => array(
'width' => $this->getSetting('width'),
......@@ -192,9 +186,10 @@ class ColorboxFieldFormatter extends FormatterBase {
$options['attributes']['class'] = array_merge($options['attributes']['class'], explode(' ', $this->getSetting('class')));
}
$element[$delta] = array(
'#markup' => l($output, $uri['path'], $options),
);
$url->setOptions($options);
$ext = $url->isExternal();
$link = Link::fromTextAndUrl($output, $url);
$element[$delta] = $link->toRenderable();
}
return $element;
......@@ -215,6 +210,26 @@ class ColorboxFieldFormatter extends FormatterBase {
return nl2br(Html::escape($item->value));
}
/**
* @param \Drupal\Core\Field\FieldItemInterface $item
* One field item.
*
* @return \Drupal\Core\Url
*/
protected function getUrl(FieldItemInterface $item) {
$entity = $item->getEntity();
if ($this->getSetting('link_type') == 'content') {
return $entity->toUrl();
}
$link = $this->getSetting('link');
if (\Drupal::moduleHandler()->moduleExists('token')) {
$token_service = \Drupal::token();
$link = $token_service->replace($this->getSetting('link'), array($entity->bundle() => $entity), array('clear' => TRUE));
}
return Url::fromUri($link);
}
/**
* Callback to provide an array for a select field containing all supported
* colorbox styles.
......
......@@ -7,10 +7,9 @@
namespace Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Field\Annotation\FieldFormatter;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
......@@ -20,31 +19,34 @@ use Drupal\Core\Form\FormStateInterface;
* id = "colorbox_field_formatter_entityreference",
* label = @Translation("Colorbox FF"),
* field_types = {
* "entityreference"
* "entity_reference"
* }
* )
*/
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.
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['link_type']['#access'] = FALSE;
$form['link']['#access'] = FALSE;
return $form;
}
/**
* {@inheritdoc}
*/
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));
return $item->entity->label();
}
/**
* {@inheritdoc}
*/
protected function getUrl(FieldItemInterface $item) {
return $item->entity->toUrl();
}
}
......@@ -7,10 +7,9 @@
namespace Drupal\colorbox_field_formatter\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Field\Annotation\FieldFormatter;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
......@@ -74,30 +73,10 @@ class ColorboxFieldFormatterImage 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.
* {@inheritdoc}
*/
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));
return $item->view(array('image_style' => $this->getSetting('image_style')));
}
}
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