You are here

podcast.module in Podcast (using Views) 8

Primary module hooks for the podcast module.

File

podcast.module
View source
<?php

/**
 * @file
 * Primary module hooks for the podcast module.
 */
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\Core\Template\Attribute;
use Drupal\podcast\Plugin\views\style\Rss;
use Drupal\views\Render\ViewsRenderPipelineMarkup;

/**
 * Implements hook_theme().
 */
function podcast_theme() {
  $theme_registry['views_view_rss_podcast_feed'] = [
    'base hook' => 'views_view_rss',
  ];
  $theme_registry['views_view_row_rss_podcast_feed'] = [
    'base hook' => 'views_view_row_rss',
  ];
  return $theme_registry;
}

/**
 * Implements hook_preprocess_HOOK().
 */
function podcast_preprocess_views_view_rss(&$variables) {
  $view = $variables['view'];

  /** @var \Drupal\views\Plugin\views\style\StylePluginBase $style */
  $style = $view->style_plugin;
  if (!$style instanceof Rss) {
    return;
  }
  unset($variables['title']);
  unset($variables['description']);
  unset($variables['link']);
  $variables['podcast_elements'] = [];
  $podcast_elements = is_array($style->podcast_elements) ? $style->podcast_elements : [];
  foreach (Element::children($podcast_elements) as $k) {
    $element = $podcast_elements[$k];
    if (isset($element['attributes']) && is_array($element['attributes'])) {
      $element['attributes'] = new Attribute($element['attributes']);
    }
    if (!empty($element['key'])) {
      if (empty($element['value']) && !empty($element['values'])) {
        podcast_process_nested_channel_element($element);
      }
      $variables['podcast_elements'][] = $element;
    }
  }
}

/**
 * If an element contains nested values we serialize the markup here.
 *
 * The serialization is then stored in the `'value'` key.
 *
 * @param array $element
 *   The element containing the nested values.
 */
function podcast_process_nested_channel_element(array &$element) {
  $values = $element['values'];
  if (!is_array($values)) {
    return;
  }
  $output = array_reduce($values, function ($out, $value) {
    if ($content = $value['value']) {
      $out .= sprintf('<%s>%s</%s>', $value['key'], $content, $value['key']);
    }
    else {
      $out .= sprintf('<%s />', $value['key']);
    }
    return $out;
  }, '');
  $element['value'] = Markup::create($output);
}

/**
 * Implements hook_preprocess_HOOK().
 */
function podcast_preprocess_views_view_row_rss(&$variables) {
  $view = $variables['view'];

  /** @var \Drupal\views\Plugin\views\style\StylePluginBase $style */
  $style = $view->style_plugin;
  if (!$style instanceof Rss) {
    return;
  }
  unset($variables['link']);
  unset($variables['description']);
}

/**
 * Implements hook_preprocess_HOOK().
 */
function podcast_preprocess_views_view_field(&$variables) {

  /** @var \Drupal\views\Plugin\views\field\EntityField $field */
  $field = $variables['field'];

  /** @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];
  $style = $view->style_plugin;
  if (!$style instanceof Rss) {
    return;
  }
  $find_mapping_key = function ($current_field, $options) {
    foreach ($options as $field_mapping => $field_name) {
      if ($current_field === $field_name) {
        $clean_key = preg_replace('/_field$/u', '', $field_mapping);
        $clean_key = Html::cleanCssIdentifier($clean_key);
        return $clean_key;
      }
    }
    return NULL;
  };
  $cdataFields = [
    'description',
    'itunessummary',
    'itunesseason',
    'itunesepisode',
  ];
  if (in_array($find_mapping_key($field->options['id'], $style->options), $cdataFields, TRUE) || in_array($find_mapping_key($field->options['id'], $view->rowPlugin->options), $cdataFields, TRUE)) {
    $variables['output'] = ViewsRenderPipelineMarkup::create('<![CDATA[' . $variables['output'] . ']]>');
  }
}

Functions