View source
<?php
namespace Drupal\recipe\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\Plugin\views\display\PathPluginBase;
use Drupal\views\Plugin\views\display\ResponseDisplayPluginInterface;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Recipe extends PathPluginBase implements ResponseDisplayPluginInterface {
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'));
}
protected $ajaxEnabled = FALSE;
protected $usesPager = FALSE;
public function getType() {
return 'recipe';
}
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;
$output = (string) \Drupal::service('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;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['displays'] = [
'default' => [],
];
$options['style']['contains']['type']['default'] = 'recipeml';
$options['row'] = FALSE;
$options['defaults']['default']['style'] = FALSE;
$options['defaults']['default']['row'] = FALSE;
return $options;
}
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
$categories['page'] = [
'title' => $this
->t('Recipe settings'),
'column' => 'second',
'build' => [
'#weight' => -10,
],
];
$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 '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 format link will be available only to the selected displays.'),
'#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', $displays),
'#default_value' => $this
->getOption('displays'),
];
break;
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
$section = $form_state
->get('section');
switch ($section) {
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());
}
$clone
->destroy();
unset($clone);
}
public function usesLinkDisplay() {
return FALSE;
}
}