You are here

protected function Rss::processCategories in Podcast (using Views) 8

Processes categories to output the format expected by iTunes.

Parameters

array $element: The keyvalue to process.

Return value

array An array of keyvalue elements representing podcast categories.

1 call to Rss::processCategories()
Rss::getPodcastElements in src/Plugin/views/style/Rss.php
Return an array of additional XHTML elements to add to the podcast channel.

File

src/Plugin/views/style/Rss.php, line 316

Class

Rss
Default style plugin to render an RSS feed.

Namespace

Drupal\podcast\Plugin\views\style

Code

protected function processCategories(array $element) {
  $tag_name = 'itunes:category';

  /** @var string[] $values */
  $values = array_map('trim', explode(',', $element['value']));

  // We need to parse out an optional leading category.
  $hierarchical_categories = array_reduce($values, function ($carry, $value) {
    $parts = explode('/', $value);
    if (empty($parts)) {
      return $carry;
    }
    $category = array_shift($parts);

    // Initialize the category section if it does not exist.
    if (!array_key_exists($category, $carry)) {
      $carry[$category] = [];
    }
    if (empty($parts)) {
      return $carry;
    }
    $carry[$category][] = array_shift($parts);
    return $carry;
  }, []);
  if (empty($hierarchical_categories)) {
    return [];
  }
  $new_elements = [];
  foreach ($hierarchical_categories as $category => $subcategories) {
    $category_container = $this
      ->buildElementFromOptions('itunes:category');
    $category_container['attributes'] = [
      'text' => html_entity_decode($category),
    ];
    $category_container['value'] = Markup::create(implode(array_map(function ($subcategory) use ($tag_name) {
      return sprintf('<%s text="%s"/>', $tag_name, $subcategory);
    }, $subcategories)));
    $new_elements[] = $category_container;
  }
  return $new_elements;
}