View source
<?php
namespace Drupal\views_oai_pmh\Plugin\views\display;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\State\StateInterface;
use Drupal\views\Render\ViewsRenderPipelineMarkup;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\PathPluginBase;
use Drupal\views_oai_pmh\Service\Repository;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OAIPMH extends PathPluginBase {
protected $usesAJAX = FALSE;
protected $usesMore = FALSE;
protected $usesAreas = FALSE;
protected $usesPager = TRUE;
protected $usesAttachments = FALSE;
protected $metadataPrefix = 'oai_dc';
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 'oai_pmh';
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['style']['contains']['type']['default'] = 'views_oai_pmh_record';
$options['defaults']['default']['style'] = FALSE;
$options['defaults']['default']['row'] = FALSE;
unset($options['exposed_form']);
unset($options['exposed_block']);
unset($options['css_class']);
return $options;
}
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
$categories['page']['title'] = $this
->t('OAI-PMH settings');
$categories['title']['title'] = $this
->t('Repository name');
$options['title']['title'] = $this
->t('Repository name');
}
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);
$response
->setContent($output);
$cache_metadata = CacheableMetadata::createFromRenderArray($build);
$response
->addCacheableDependency($cache_metadata);
$response->headers
->set('Content-Type', 'application/xml;charset=UTF-8');
return $response;
}
public function render() {
$build = [];
$build['#markup'] = $this->renderer
->executeInRenderContext(new RenderContext(), function () {
return $this->view->style_plugin
->render();
});
$this->view->element['#cache_properties'][] = '#content_type';
if (!empty($this->view->live_preview)) {
$build['#prefix'] = '<pre>';
$build['#plain_text'] = $build['#markup'];
$build['#suffix'] = '</pre>';
unset($build['#markup']);
}
else {
$build['#markup'] = ViewsRenderPipelineMarkup::create($build['#markup']);
}
parent::applyDisplayCacheabilityMetadata($build);
return $build;
}
public function execute() {
$this->view
->setCurrentPage($this
->pageByResumptionToken());
parent::execute();
return $this->view
->render();
}
public function preview() {
return $this->view
->render();
}
public function initDisplay(ViewExecutable $view, array &$display, array &$options = NULL) {
parent::initDisplay($view, $display, $options);
if (!($prefix = $this
->getMetadataPrefixByToken())) {
$prefix = 'oai_dc';
}
$this->metadataPrefix = $this->view
->getRequest()->query
->get('metadataPrefix', $prefix);
}
public function getCurrentMetadataPrefix() {
return $this->metadataPrefix;
}
private function pageByResumptionToken() {
$token = $this->view
->getRequest()->query
->get('resumptionToken', NULL);
if ($token) {
$repository = \Drupal::service('views_oai_pmh.repository');
$paginator = $repository
->decodeResumptionToken($token);
return $paginator['offset'];
}
return 0;
}
private function getMetadataPrefixByToken() {
$token = $this->view
->getRequest()->query
->get('resumptionToken', NULL);
if ($token) {
$repository = \Drupal::service('views_oai_pmh.repository');
$paginator = $repository
->decodeResumptionToken($token);
return $paginator['metadataPrefix'];
}
return NULL;
}
}