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 0000000000000000000000000000000000000000..10d566ea8a1dd7bcd44a742bf23c37bfcb9ad4f5 --- /dev/null +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php @@ -0,0 +1,59 @@ + $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 0000000000000000000000000000000000000000..33eeffcc585fc1a9995e3aa8951e3f77f02a9816 --- /dev/null +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php @@ -0,0 +1,48 @@ + $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 0000000000000000000000000000000000000000..7b7e6e44b17bd45a7213bcb852dddec1f3e5274e --- /dev/null +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php @@ -0,0 +1,58 @@ +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 0000000000000000000000000000000000000000..e743350b5e32898b86bfbb54019b6ee6a279b5c4 --- /dev/null +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php @@ -0,0 +1,72 @@ + $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 ddd164c88ed0980fa742282b3752d36ad0c26b62..89f334b496d41c9c3731f3d30f2a3b56fc7b4476 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -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. *