Skip to content
Feed.php 8.09 KiB
Newer Older
Earl Miles's avatar
Earl Miles committed
<?php

/**
 * @file
 * Contains Drupal\views\Plugin\views\display\Feed.
namespace Drupal\views\Plugin\views\display;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
Earl Miles's avatar
Earl Miles committed
/**
 * The plugin that handles a feed, such as RSS or atom.
 *
 * @ingroup views_display_plugins
Bram Goffings's avatar
Bram Goffings committed
 *   id = "feed",
 *   title = @Translation("Feed"),
 *   help = @Translation("Display the view as a feed, such as an RSS feed."),
 *   uses_hook_menu = TRUE,
  /**
   * Whether the display allows the use of AJAX or not.
   *
   * @var bool
   */
  protected $usesAJAX = FALSE;

  /**
   * Whether the display allows the use of a pager or not.
   *
   * @var bool
   */
  protected $usesPager = FALSE;

  public function init(ViewExecutable $view, &$display, $options = NULL) {
Earl Miles's avatar
Earl Miles committed
    parent::init($view, $display, $options);

    // Set the default row style. Ideally this would be part of the option
    // definition, but in this case it's dependent on the view's base table,
    // which we don't know until init().
    $row_plugins = views_fetch_plugin_names('row', $this->getStyleType(), array($view->storage->get('base_table')));
Earl Miles's avatar
Earl Miles committed
    $default_row_plugin = key($row_plugins);
    if (empty($this->options['row']['type'])) {
      $this->options['row']['type'] = $default_row_plugin;
  /**
   * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::getStyleType().
   */
  protected function getStyleType() {
    return 'feed';
  }
   * Overrides \Drupal\views\Plugin\views\display\PathPluginBase::execute().
Earl Miles's avatar
Earl Miles committed
    $output = $this->view->render();
Earl Miles's avatar
Earl Miles committed
    if (empty($output)) {
  /**
   * Overrides \Drupal\views\Plugin\views\display\PathPluginBase::preview().
   */
Earl Miles's avatar
Earl Miles committed
    if (!empty($this->view->live_preview)) {
      return '<pre>' . check_plain($this->view->render()) . '</pre>';
    }
Earl Miles's avatar
Earl Miles committed
    return $this->view->render();
  }

  /**
   * Overrides \Drupal\views\Plugin\views\display\PathPluginBase::render().
Earl Miles's avatar
Earl Miles committed
    return $this->view->style_plugin->render($this->view->result);
  }

  /**
   * Overrides \Drupal\views\Plugin\views\displays\DisplayPluginBase::defaultableSections().
   */
  public function defaultableSections($section = NULL) {
    $sections = parent::defaultableSections($section);

    if (in_array($section, array('style', 'row'))) {
Earl Miles's avatar
Earl Miles committed
      return FALSE;
    }

    // Tell views our sitename_title option belongs in the title section.
    if ($section == 'title') {
      $sections[] = 'sitename_title';
    }
    elseif (!$section) {
      $sections['title'][] = 'sitename_title';
    }
    return $sections;
  }

  /**
   * Overrides \Drupal\views\Plugin\views\display\PathPluginBase::defineOptions().
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
Earl Miles's avatar
Earl Miles committed

    $options['displays'] = array('default' => array());

    // Overrides for standard stuff:
    $options['style']['contains']['type']['default'] = 'rss';
    $options['style']['contains']['options']['default']  = array('description' => '');
Earl Miles's avatar
Earl Miles committed
    $options['sitename_title']['default'] = FALSE;
    $options['row']['contains']['type']['default'] = '';
    $options['defaults']['default']['style'] = FALSE;
    $options['defaults']['default']['row'] = FALSE;
Earl Miles's avatar
Earl Miles committed

    return $options;
  }

  /**
   * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::optionsSummary().
   */
  public function optionsSummary(&$categories, &$options) {
    parent::optionsSummary($categories, $options);
    // Since we're childing off the 'path' type, we'll still *call* our
Earl Miles's avatar
Earl Miles committed
    // category 'page' but let's override it so it says feed settings.
    $categories['page'] = array(
      'title' => t('Feed settings'),
      'column' => 'second',
      'build' => array(
        '#weight' => -10,
      ),
    );

    if ($this->getOption('sitename_title')) {
Earl Miles's avatar
Earl Miles committed
      $options['title']['value'] = t('Using the site name');
    }

    $displays = array_filter($this->getOption('displays'));
Earl Miles's avatar
Earl Miles committed
    if (count($displays) > 1) {
      $attach_to = t('Multiple displays');
    }
    elseif (count($displays) == 1) {
      $display = array_shift($displays);
      $displays = $this->view->storage->get('display');
      if (!empty($displays[$display])) {
        $attach_to = check_plain($displays[$display]['display_title']);
Earl Miles's avatar
Earl Miles committed
      }
    }

    if (!isset($attach_to)) {
      $attach_to = t('None');
    }

    $options['displays'] = array(
      'category' => 'page',
      'title' => t('Attach to'),
      'value' => $attach_to,
    );
  }

  /**
   * Overrides \Drupal\views\Plugin\views\display\PathPluginBase::buildOptionsForm().
  public function buildOptionsForm(&$form, &$form_state) {
Earl Miles's avatar
Earl Miles committed
    // It is very important to call the parent function here.
    parent::buildOptionsForm($form, $form_state);
Earl Miles's avatar
Earl Miles committed

    switch ($form_state['section']) {
      case 'title':
        $title = $form['title'];
        // A little juggling to move the 'title' field beyond our checkbox.
        unset($form['title']);
        $form['sitename_title'] = array(
          '#type' => 'checkbox',
          '#title' => t('Use the site name for the title'),
          '#default_value' => $this->getOption('sitename_title'),
Earl Miles's avatar
Earl Miles committed
        );
        $form['title'] = $title;
        $form['title']['#states'] = array(
          'visible' => array(
            ':input[name="sitename_title"]' => array('checked' => FALSE),
          ),
        );
Earl Miles's avatar
Earl Miles committed
        break;
      case 'displays':
        $form['#title'] .= t('Attach to');
        $displays = array();
        foreach ($this->view->storage->get('display') as $display_id => $display) {
          // @todo The display plugin should have display_title and id as well.
          if (!empty($this->view->displayHandlers[$display_id]) && $this->view->displayHandlers[$display_id]->acceptAttachments()) {
            $displays[$display_id] = $display['display_title'];
Earl Miles's avatar
Earl Miles committed
          }
        }
        $form['displays'] = array(
          '#type' => 'checkboxes',
          '#description' => t('The feed icon will be available only to the selected displays.'),
          '#options' => $displays,
          '#default_value' => $this->getOption('displays'),
Earl Miles's avatar
Earl Miles committed
        );
        break;
      case 'path':
        $form['path']['#description'] = t('This view will be displayed by visiting this path on your site. It is recommended that the path be something like "path/%/%/feed" or "path/%/%/rss.xml", putting one % in the path for each contextual filter you have defined in the view.');
    }
  }

  /**
   * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::submitOptionsForm().
  public function submitOptionsForm(&$form, &$form_state) {
    parent::submitOptionsForm($form, $form_state);
Earl Miles's avatar
Earl Miles committed
    switch ($form_state['section']) {
      case 'title':
        $this->setOption('sitename_title', $form_state['values']['sitename_title']);
Earl Miles's avatar
Earl Miles committed
        break;
      case 'displays':
        $this->setOption($form_state['section'], $form_state['values'][$form_state['section']]);
   * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::attachTo().
  public function attachTo(ViewExecutable $clone, $display_id) {
    $displays = $this->getOption('displays');
Earl Miles's avatar
Earl Miles committed
    if (empty($displays[$display_id])) {
      return;
    }

    // Defer to the feed style; it may put in meta information, and/or
    // attach a feed icon.
Earl Miles's avatar
Earl Miles committed
    if ($plugin) {
      $clone->setDisplay($this->display['id']);
      $plugin->attach_to($display_id, $this->getPath(), $clone->getTitle());
Earl Miles's avatar
Earl Miles committed

      // Clean up
      $clone->destroy();
      unset($clone);
    }
  }

  /**
   * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::usesLinkDisplay().
   */
Earl Miles's avatar
Earl Miles committed
    return TRUE;
  }