You are here

function views_rss_core_preprocess_item_category in Views RSS 8.2

Same name and namespace in other branches
  1. 8.3 modules/views_rss_core/views_rss_core.inc \views_rss_core_preprocess_item_category()

Preprocess function for item <category> element.

See http://www.rssboard.org/rss-profile#element-channel-item-category for RSS Advisory Board requirements/recommendations.

See also

RSSCategoryFormatter::viewElements()

1 string reference to 'views_rss_core_preprocess_item_category'
views_rss_core_views_rss_item_elements in modules/views_rss_core/views_rss_core.module
Implements hook_views_rss_item_elements().

File

modules/views_rss_core/views_rss_core.inc, line 213
Preprocess functions for Views RSS: Core Elements module.

Code

function views_rss_core_preprocess_item_category(&$variables) {

  // No raw values = no preprocessing.
  if (empty($variables['raw']['items'])) {
    return;
  }
  $variables['elements'] = array();
  foreach ($variables['raw']['items'] as $item) {

    // When 'Content: <vocabulary_name>' was selected for source of category
    // element, we will get all we need in raw values.
    if (!empty($item['raw']) && is_object($item['raw']) && $item['raw']->target_id) {
      $term = $item['raw']->entity;
    }
    elseif (!empty($item['tid'])) {
      $term = Term::load($item['tid']);
    }

    // According to RSS Advisory Board, the category's value should be
    // a slash-delimited string that identifies a hierarchical position
    // in the taxonomy.
    $values = array();

    // Load parent term objects (this includes original term as well).
    if ($parents = \Drupal::service('entity_type.manager')
      ->getStorage('taxonomy_term')
      ->loadAllParents($term
      ->id())) {
      foreach ($parents as $parent) {
        $values[] = $parent
          ->label();
      }
    }
    $element = array(
      'key' => 'category',
      'value' => implode('/', array_reverse($values)),
      // Drupal uses term URL for domain attribute. RSS Best Practices say that
      // a domain attribute identifies the category's taxonomy - which suggests
      // either vocabulary name or its URL. We don't have any safe way to know
      // public vocabulary URL, could use its name instead though? @TODO?
      'attributes' => array(
        'domain' => $term
          ->url('canonical', array(
          'absolute' => TRUE,
        )),
      ),
    );
    $variables['elements'][] = $element;
  }
}