You are here

protected function RssFields::getChannelElements in Views RSS 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/style/RssFields.php \Drupal\views_rss\Plugin\views\style\RssFields::getChannelElements()

Return an array of additional XHTML elements to add to the channel.

Return value

array $elements An array that can be passed to the Drupal renderer.

1 call to RssFields::getChannelElements()
RssFields::render in src/Plugin/views/style/RssFields.php
Render the display in this style.

File

src/Plugin/views/style/RssFields.php, line 304

Class

RssFields
Default style plugin to render an RSS feed from fields.

Namespace

Drupal\views_rss\Plugin\views\style

Code

protected function getChannelElements() {
  $elements = array();
  foreach (views_rss_get('channel_elements') as $module => $module_channel_elements) {
    foreach ($module_channel_elements as $element => $definition) {
      list($element_namespace, $element_name) = views_rss_extract_element_names($element, 'core');

      // Try to fetch namespace value from view configuration.
      if (isset($this->options['channel'][$element_namespace][$module][$element_name])) {
        $element_value = $this->options['channel'][$element_namespace][$module][$element_name];
      }
      elseif (isset($definition['default_value'])) {
        $element_value = $definition['default_value'];
      }
      else {
        $element_value = NULL;
      }

      // Start building XML channel element array compatible with
      // the Drupal renderer.
      $rss_element = array(
        'key' => $element,
        'value' => $element_value,
      );
      if (!empty($element_namespace) && $element_namespace != 'core') {
        $rss_element['namespace'] = $element_namespace;
      }

      // It might happen than a preprocess function will need to split one
      // element into multiple ones - this will for example happen for channel
      // <category> element, if multiple categories were provided (separated
      // by a comma) - they will need to be printed as multiple <category>
      // elements - therefore we need to work on array of RSS elements here.
      $rss_elements = array(
        $rss_element,
      );

      // Preprocess element value.
      if (isset($definition['preprocess functions']) && is_array($definition['preprocess functions'])) {
        foreach ($definition['preprocess functions'] as $preprocess_function) {
          if (function_exists($preprocess_function)) {
            $item_variables = array(
              'elements' => &$rss_elements,
              'item' => $this->options['channel'],
              'view' => $this->view,
            );
            $preprocess_function($item_variables);
          }
        }
      }
      foreach ($rss_elements as $rss_element) {

        // Keep certain elements from rendering in channel_elements array.
        // These have placeholders in the twig file.
        // @todo find a better way of setting and passing these where they don't pass through rendering.
        $key = $rss_element['key'];
        if (in_array($key, array(
          'title',
          'description',
          'link',
          'language',
        ))) {
          $render_element = [
            '#type' => 'markup',
            '#markup' => $rss_element['value'],
            '#printed' => TRUE,
          ];
          $elements[] = $render_element;
        }
        elseif (!empty($rss_element['value']) || !empty($rss_element['attributes'])) {
          $render_element = [
            '#type' => 'html_tag',
            '#tag' => $rss_element['key'],
          ];
          if (!empty($rss_element['value'])) {
            $render_element['#value'] = $rss_element['value'];
          }
          if (!empty($rss_element['attributes'])) {
            $render_element['#attributes'] = $rss_element['attributes'];
          }
          $elements[] = $render_element;
        }
      }
    }
  }
  return $elements;
}