View source
<?php
namespace Drupal\views\Plugin\views\display;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\State\StateInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Feed extends PathPluginBase implements ResponseDisplayPluginInterface {
protected $ajaxEnabled = FALSE;
protected $usesPager = FALSE;
protected $renderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, StateInterface $state, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider, $state);
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('router.route_provider'), $container
->get('state'), $container
->get('renderer'));
}
public function getType() {
return 'feed';
}
public static function buildResponse($view_id, $display_id, array $args = []) {
$build = static::buildBasicRenderable($view_id, $display_id, $args);
$response = new CacheableResponse('', 200);
$build['#response'] = $response;
$renderer = \Drupal::service('renderer');
$output = (string) $renderer
->renderRoot($build);
if (empty($output)) {
throw new NotFoundHttpException();
}
$response
->setContent($output);
$cache_metadata = CacheableMetadata::createFromRenderArray($build);
$response
->addCacheableDependency($cache_metadata);
return $response;
}
public function execute() {
parent::execute();
return $this->view
->render();
}
public function preview() {
$output = $this->view
->render();
if (!empty($this->view->live_preview)) {
$output = [
'#prefix' => '<pre>',
'#plain_text' => $this->renderer
->renderRoot($output),
'#suffix' => '</pre>',
];
}
return $output;
}
public function render() {
$build = $this->view->style_plugin
->render($this->view->result);
$this
->applyDisplayCacheabilityMetadata($build);
return $build;
}
public function defaultableSections($section = NULL) {
$sections = parent::defaultableSections($section);
if (in_array($section, [
'style',
'row',
])) {
return FALSE;
}
if ($section == 'title') {
$sections[] = 'sitename_title';
}
elseif (!$section) {
$sections['title'][] = 'sitename_title';
}
return $sections;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['displays'] = [
'default' => [],
];
$options['style']['contains']['type']['default'] = 'rss';
$options['style']['contains']['options']['default'] = [
'description' => '',
];
$options['sitename_title']['default'] = FALSE;
$options['row']['contains']['type']['default'] = 'rss_fields';
$options['defaults']['default']['style'] = FALSE;
$options['defaults']['default']['row'] = FALSE;
return $options;
}
public function newDisplay() {
parent::newDisplay();
if (empty($this->options['row']['type']) || $this->options['row']['type'] === 'rss_fields') {
$row_plugins = Views::fetchPluginNames('row', $this
->getType(), [
$this->view->storage
->get('base_table'),
]);
$default_row_plugin = key($row_plugins);
$options = $this
->getOption('row');
$options['type'] = $default_row_plugin;
$this
->setOption('row', $options);
}
}
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
$categories['page'] = [
'title' => $this
->t('Feed settings'),
'column' => 'second',
'build' => [
'#weight' => -10,
],
];
if ($this
->getOption('sitename_title')) {
$options['title']['value'] = $this
->t('Using the site name');
}
$displays = array_filter($this
->getOption('displays'));
if (count($displays) > 1) {
$attach_to = $this
->t('Multiple displays');
}
elseif (count($displays) == 1) {
$display = array_shift($displays);
$displays = $this->view->storage
->get('display');
if (!empty($displays[$display])) {
$attach_to = $displays[$display]['display_title'];
}
}
if (!isset($attach_to)) {
$attach_to = $this
->t('None');
}
$options['displays'] = [
'category' => 'page',
'title' => $this
->t('Attach to'),
'value' => $attach_to,
];
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
switch ($form_state
->get('section')) {
case 'title':
$title = $form['title'];
unset($form['title']);
$form['sitename_title'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use the site name for the title'),
'#default_value' => $this
->getOption('sitename_title'),
];
$form['title'] = $title;
$form['title']['#states'] = [
'visible' => [
':input[name="sitename_title"]' => [
'checked' => FALSE,
],
],
];
break;
case 'displays':
$form['#title'] .= $this
->t('Attach to');
$displays = [];
foreach ($this->view->storage
->get('display') as $display_id => $display) {
if ($this->view->displayHandlers
->has($display_id) && $this->view->displayHandlers
->get($display_id)
->acceptAttachments()) {
$displays[$display_id] = $display['display_title'];
}
}
$form['displays'] = [
'#title' => $this
->t('Displays'),
'#type' => 'checkboxes',
'#description' => $this
->t('The feed icon will be available only to the selected displays.'),
'#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', $displays),
'#default_value' => $this
->getOption('displays'),
];
break;
case 'path':
$form['path']['#description'] = $this
->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.');
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
$section = $form_state
->get('section');
switch ($section) {
case 'title':
$this
->setOption('sitename_title', $form_state
->getValue('sitename_title'));
break;
case 'displays':
$this
->setOption($section, $form_state
->getValue($section));
break;
}
}
public function attachTo(ViewExecutable $clone, $display_id, array &$build) {
$displays = $this
->getOption('displays');
if (empty($displays[$display_id])) {
return;
}
$clone
->setArguments($this->view->args);
$clone
->setDisplay($this->display['id']);
$clone
->buildTitle();
if ($plugin = $clone->display_handler
->getPlugin('style')) {
$plugin
->attachTo($build, $display_id, $clone
->getUrl(), $clone
->getTitle());
foreach ($clone->feedIcons as $feed_icon) {
$this->view->feedIcons[] = $feed_icon;
}
}
$clone
->destroy();
unset($clone);
}
public function usesLinkDisplay() {
return TRUE;
}
}