0

I've been struggling with trying to create a custom xml REST API in D8 to replace a simple Views RSS feed (that is imported into other D7 sites using a custom Feeds importer) in a D6 site that's being migrated, and after pulling my hair out with the custom REST endpoint, I'm looking at doing what I did in D6, which is to create a Views RSS display. However, the feed requires some customization, so I need to determine how to add custom fields to the RSS feed. For other feeds in D7 sites, I've used the Views RSS module to add fields, but the D8 branch is still apparently not stable, so that does not seem to be an option.

To customize my RSS feed, I need to do two things:

  1. Add custom elements (hook_nodeapi($op = 'rss item') in D6)
  2. Modify the query so only nodes with specified tids in a term reference field based on a views global null param passed in (hook_views_query_alter() in D6)

So it appears that for an RSS Feed display in D8 views, the only fields displayed in the feed are the ones predefined in the row style settings (using Fields as the row type). With the Views RSS module not being available, is there a way with core Views to override the view to add custom fields (such as a class override)? I appears I can still use hook_views_query_alter() to modify my query, but it doesn't do me much good if I can't add fields.

I could probably spend a few hours reading through code, but if anyone has a quick answer as to the best way to add custom elements to a D8 RSS display, or could point me in the right direction, I'd appreciate it.

Thanks.

0

As documented here, I went with overriding the views row style plugin. All I did in the override was add the render() method and add my own modifications to the $build array to make it work.

<?php

namespace Drupal\video_export\Plugin\views\row;

use Drupal\views\Plugin\views\row\RssFields;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Renders an RSS item based on fields.
 *
 * @ViewsRow(
 *   id = "mymodule_rss_fields",
 *   title = @Translation("Custom Fields"),
 *   help = @Translation("Display fields as RSS items."),
 *   theme = "views_view_row_rss",
 *   display_types = {"feed"}
 * )
 */
class MyModuleVRssFields extends RssFields {

  /**
   * Override of RssFields::render() with additional fields.
   *
   * @param object $row
   *
   * @return array
   */
  public function render($row) {
    $build = parent:: render();
    $item = $build['#row'];

    // Add MRCTV nid
    $item->elements[] = array(
      'key' => 'source-nid',
      'value' => $row->nid,
    );

    // Add channels and their target nids. We can get them from $row->_entity.
    $site = $this->view->args[0];
    // Get source nids from view.
    $channel_tids = array_column($row->_entity->field_channels->getValue(), 'target_id');
    // Now, get destination tids from config.
    $mapping_config = \Drupal::config('video_export.mappings');
    $all_mappings = $mapping_config->get('sites');

    foreach($channel_tids as $source_channel) {
      if(in_array($source_channel, array_keys($all_mappings[$site]['mappings']))) {
        $item->elements[] = array(
          'key' => $site . '-channel-map',
          'value' => $all_mappings[$site]['mappings'][$source_channel],
        );
      }
    }

    // Re-populate the $build array with the updated row.
    $build['#row'] = $item;

    return $build;
  }
}
  • is there something else that needs to be done in order to register the plugin in your custom module? I have a custom module, modified the namespace accordingly and added a simple $item->elements[] array with key and value, returned the modified $build like yours and not having any luck... – scotself Jul 17 '17 at 2:17
  • I was wondering the same, perhaps one needs to create also a corresponding style plugin ? – GiorgosK Aug 11 '18 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.