Commit 6c6d2335 authored by webchick's avatar webchick

Issue #1787248 by Stalski, pcambra, webflo, Sweetchuck, jibran, zuuperman:...

Issue #1787248 by Stalski, pcambra, webflo, Sweetchuck, jibran, zuuperman: Convert taxonomy module formatters to Plugin system.
parent c285bbcc
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\field\formatter\LinkFormatter.
*/
namespace Drupal\taxonomy\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\taxonomy\Plugin\field\formatter\TaxonomyFormatterBase;
/**
* Plugin implementation of the 'taxonomy_term_reference_link' formatter.
*
* @Plugin(
* id = "taxonomy_term_reference_link",
* module = "taxonomy",
* label = @Translation("Link"),
* field_types = {
* "taxonomy_term_reference"
* }
* )
*/
class LinkFormatter extends TaxonomyFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
// Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is
// not set. Theme such terms as just their name.
foreach ($items as $delta => $item) {
if ($item['tid'] == 'autocreate') {
$elements[$delta] = array(
'#markup' => check_plain($item['name']),
);
}
else {
$term = $item['entity'];
$uri = $term->uri();
$elements[$delta] = array(
'#type' => 'link',
'#title' => $term->label(),
'#href' => $uri['path'],
'#options' => $uri['options'],
);
}
}
return $elements;
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\field\formatter\PlainFormatter.
*/
namespace Drupal\taxonomy\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\taxonomy\Plugin\field\formatter\TaxonomyFormatterBase;
/**
* Plugin implementation of the 'taxonomy_term_reference_plain' formatter.
*
* @Plugin(
* id = "taxonomy_term_reference_plain",
* module = "taxonomy",
* label = @Translation("Plain text"),
* field_types = {
* "taxonomy_term_reference"
* }
* )
*/
class PlainFormatter extends TaxonomyFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
// Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is
// not set. Theme such terms as just their name.
foreach ($items as $delta => $item) {
$name = ($item['tid'] != 'autocreate' ? $item['entity']->label() : $item['name']);
$elements[$delta] = array(
'#markup' => check_plain($name),
);
}
return $elements;
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\field\formatter\RSSCategoryFormatter.
*/
namespace Drupal\taxonomy\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
use Drupal\taxonomy\Plugin\field\formatter\TaxonomyFormatterBase;
/**
* Plugin implementation of the 'taxonomy_term_reference_rss_category' formatter.
*
* @Plugin(
* id = "taxonomy_term_reference_rss_category",
* module = "taxonomy",
* label = @Translation("RSS category"),
* field_types = {
* "taxonomy_term_reference"
* }
* )
*/
class RSSCategoryFormatter extends TaxonomyFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
// Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is
// not set. Theme such terms as just their name.
foreach ($items as $item) {
if ($item['tid'] != 'autocreate') {
$value = $item['entity']->label();
$uri = $item['entity']->uri();
$uri['options']['absolute'] = TRUE;
$domain = url($uri['path'], $uri['options']);
}
else {
$value = $item['name'];
$domain = '';
}
$entity->rss_elements[] = array(
'key' => 'category',
'value' => $value,
'attributes' => array(
'domain' => $domain,
),
);
}
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\field\formatter\TaxonomyFormatterBase.
*/
namespace Drupal\taxonomy\Plugin\field\formatter;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;
/**
* Base class for the taxonomy_term formatters.
*/
abstract class TaxonomyFormatterBase extends FormatterBase {
/**
* Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView().
*
* This preloads all taxonomy terms for multiple loaded objects at once and
* unsets values for invalid terms that do not exist.
*/
public function prepareView(array $entities, $langcode, array &$items) {
$tids = array();
// Collect every possible term attached to any of the fieldable entities.
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
// Force the array key to prevent duplicates.
if ($item['tid'] != 'autocreate' && $item['tid'] !== FALSE) {
$tids[$item['tid']] = $item['tid'];
}
}
}
if ($tids) {
$terms = taxonomy_term_load_multiple($tids);
// Iterate through the fieldable entities again to attach the loaded term
// data.
foreach ($entities as $id => $entity) {
$rekey = FALSE;
foreach ($items[$id] as $delta => $item) {
// Check whether the taxonomy term field instance value could be
// loaded.
if (isset($terms[$item['tid']])) {
// Replace the instance value with the term data.
$items[$id][$delta]['entity'] = $terms[$item['tid']];
}
// Terms to be created are not in $terms, but are still legitimate.
elseif ($item['tid'] == 'autocreate') {
// Leave the item in place.
}
// Otherwise, unset the instance value, since the term does not exist.
else {
unset($items[$id][$delta]);
$rekey = TRUE;
}
}
if ($rekey) {
// Rekey the items array.
$items[$id] = array_values($items[$id]);
}
}
}
}
}
......@@ -1077,81 +1077,6 @@ function taxonomy_field_is_empty($item, $field) {
return !is_array($item) || (empty($item['tid']) && empty($item['entity']));
}
/**
* Implements hook_field_formatter_info().
*/
function taxonomy_field_formatter_info() {
return array(
'taxonomy_term_reference_link' => array(
'label' => t('Link'),
'field types' => array('taxonomy_term_reference'),
),
'taxonomy_term_reference_plain' => array(
'label' => t('Plain text'),
'field types' => array('taxonomy_term_reference'),
),
'taxonomy_term_reference_rss_category' => array(
'label' => t('RSS category'),
'field types' => array('taxonomy_term_reference'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function taxonomy_field_formatter_view(EntityInterface $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is not
// set. Theme such terms as just their name.
switch ($display['type']) {
case 'taxonomy_term_reference_link':
foreach ($items as $delta => $item) {
if ($item['tid'] == 'autocreate') {
$element[$delta] = array(
'#markup' => check_plain($item['name']),
);
}
else {
$term = $item['entity'];
$uri = $term->uri();
$element[$delta] = array(
'#type' => 'link',
'#title' => $term->label(),
'#href' => $uri['path'],
'#options' => $uri['options'],
);
}
}
break;
case 'taxonomy_term_reference_plain':
foreach ($items as $delta => $item) {
$name = ($item['tid'] != 'autocreate' ? $item['entity']->label() : $item['name']);
$element[$delta] = array(
'#markup' => check_plain($name),
);
}
break;
case 'taxonomy_term_reference_rss_category':
foreach ($items as $delta => $item) {
$entity->rss_elements[] = array(
'key' => 'category',
'value' => $item['tid'] != 'autocreate' ? $item['entity']->label() : $item['name'],
'attributes' => array(
'domain' => $item['tid'] != 'autocreate' ? url('taxonomy/term/' . $item['tid'], array('absolute' => TRUE)) : '',
),
);
}
break;
}
return $element;
}
/**
* Returns the set of valid terms for a taxonomy field.
*
......@@ -1181,56 +1106,6 @@ function taxonomy_allowed_values($field, $instance, EntityInterface $entity) {
return $options;
}
/**
* Implements hook_field_formatter_prepare_view().
*
* This preloads all taxonomy terms for multiple loaded objects at once and
* unsets values for invalid terms that do not exist.
*/
function taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
$tids = array();
// Collect every possible term attached to any of the fieldable entities.
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
// Force the array key to prevent duplicates.
if ($item['tid'] != 'autocreate' && $item['tid'] !== FALSE) {
$tids[$item['tid']] = $item['tid'];
}
}
}
if ($tids) {
$terms = taxonomy_term_load_multiple($tids);
// Iterate through the fieldable entities again to attach the loaded term data.
foreach ($entities as $id => $entity) {
$rekey = FALSE;
foreach ($items[$id] as $delta => $item) {
// Check whether the taxonomy term field instance value could be loaded.
if (isset($terms[$item['tid']])) {
// Replace the instance value with the term data.
$items[$id][$delta]['entity'] = $terms[$item['tid']];
}
// Terms to be created are not in $terms, but are still legitimate.
elseif ($item['tid'] == 'autocreate') {
// Leave the item in place.
}
// Otherwise, unset the instance value, since the term does not exist.
else {
unset($items[$id][$delta]);
$rekey = TRUE;
}
}
if ($rekey) {
// Rekey the items array.
$items[$id] = array_values($items[$id]);
}
}
}
}
/**
* Title callback for term pages.
*
......
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