You are here

Recipe.php in Recipe 8.2

File

src/Plugin/views/display/Recipe.php
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;

/**
 * The plugin that handles a recipe format, such as RecipeML.
 *
 * @ingroup views_display_plugins
 *
 * @ViewsDisplay(
 *   id = "recipe",
 *   title = @Translation("Recipe"),
 *   help = @Translation("Display the view in a recipe format."),
 *   uses_route = TRUE,
 *   admin = @Translation("Recipe"),
 *   returns_response = TRUE
 * )
 */
class Recipe extends PathPluginBase implements ResponseDisplayPluginInterface {

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a Recipe Views display plugin object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key value store.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * Whether the display allows the use of AJAX or not.
   *
   * @var bool
   */
  protected $ajaxEnabled = FALSE;

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

  /**
   * {@inheritdoc}
   */
  public function getType() {
    return 'recipe';
  }

  /**
   * {@inheritdoc}
   */
  public static function buildResponse($view_id, $display_id, array $args = []) {
    $build = static::buildBasicRenderable($view_id, $display_id, $args);

    // Set up an empty response, so the style plugins can set the proper
    // Content-Type header.
    $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;
  }

  /**
   * {@inheritdoc}
   */
  public function execute() {
    parent::execute();
    return $this->view
      ->render();
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = $this->view->style_plugin
      ->render($this->view->result);
    $this
      ->applyDisplayCacheabilityMetadata($build);
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['displays'] = [
      'default' => [],
    ];

    // Overrides for standard stuff.
    $options['style']['contains']['type']['default'] = 'recipeml';
    $options['row'] = FALSE;
    $options['defaults']['default']['style'] = FALSE;
    $options['defaults']['default']['row'] = FALSE;
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function optionsSummary(&$categories, &$options) {
    parent::optionsSummary($categories, $options);

    // Since we're childing off the 'path' type, we'll still *call* our
    // category 'page' but let's override it so it says feed settings.
    $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,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {

    // It is very important to call the parent function here.
    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) {

          // @todo The display plugin should have display_title and id as well.
          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;
    }
  }

  /**
   * {@inheritdoc}
   */
  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;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function attachTo(ViewExecutable $clone, $display_id, array &$build) {
    $displays = $this
      ->getOption('displays');
    if (empty($displays[$display_id])) {
      return;
    }

    // Defer to the feed style; it may put in meta information, and/or
    // attach a feed icon.
    $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());
    }

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

  /**
   * {@inheritdoc}
   */
  public function usesLinkDisplay() {
    return FALSE;
  }

}

Classes

Namesort descending Description
Recipe The plugin that handles a recipe format, such as RecipeML.