summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2013-04-21 19:32:28 (GMT)
committerwebchick2013-04-21 19:32:28 (GMT)
commit6c6d2335db65d1a0ba15060f4bd00b64eb2eb3a5 (patch)
tree73913e091eb37d1f4425616dd97931b26144d2a6
parentc285bbcc430a91d5676cee9cfa36478ceb880106 (diff)
Issue #1787248 by Stalski, pcambra, webflo, Sweetchuck, jibran, zuuperman: Convert taxonomy module formatters to Plugin system.
-rw-r--r--core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php59
-rw-r--r--core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php48
-rw-r--r--core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php58
-rw-r--r--core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php72
-rw-r--r--core/modules/taxonomy/taxonomy.module125
5 files changed, 237 insertions, 125 deletions
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php
new file mode 100644
index 0000000..10d566e
--- /dev/null
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php
@@ -0,0 +1,59 @@
+<?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;
+ }
+
+}
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php
new file mode 100644
index 0000000..33eeffc
--- /dev/null
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php
@@ -0,0 +1,48 @@
+<?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;
+ }
+
+}
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php
new file mode 100644
index 0000000..7b7e6e4
--- /dev/null
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php
@@ -0,0 +1,58 @@
+<?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,
+ ),
+ );
+ }
+ }
+
+}
diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php
new file mode 100644
index 0000000..e743350
--- /dev/null
+++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php
@@ -0,0 +1,72 @@
+<?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]);
+ }
+ }
+ }
+ }
+
+}
diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module
index ddd164c..89f334b 100644
--- a/core/modules/taxonomy/taxonomy.module
+++ b/core/modules/taxonomy/taxonomy.module
@@ -1078,81 +1078,6 @@ function taxonomy_field_is_empty($item, $field) {
}
/**
- * 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.
*
* @param $field
@@ -1182,56 +1107,6 @@ function taxonomy_allowed_values($field, $instance, EntityInterface $entity) {
}
/**
- * 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.
*
* @param Drupal\taxonomy\Plugin\Core\Entity\Term $term