diff --git a/core/modules/rest/src/Plugin/views/display/RestExport.php b/core/modules/rest/src/Plugin/views/display/RestExport.php index cae15f7918..c3c2188ff0 100644 --- a/core/modules/rest/src/Plugin/views/display/RestExport.php +++ b/core/modules/rest/src/Plugin/views/display/RestExport.php @@ -147,8 +147,24 @@ class RestExport extends PathPluginBase implements ResponseDisplayPluginInterfac * @param \Drupal\Core\Entity\EntityStorageInterface $menu_storage * The menu storage. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, StateInterface $state, RendererInterface $renderer, array $authentication_providers, array $serializer_format_providers, EntityStorageInterface $menu_storage) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider, $state); + public function __construct( + array $configuration, + $plugin_id, + $plugin_definition, + RouteProviderInterface $route_provider, + StateInterface $state, + RendererInterface $renderer, + array $authentication_providers, + array $serializer_format_providers, + EntityStorageInterface $menu_storage + ) { + parent::__construct( + $configuration, + $plugin_id, + $plugin_definition, + $route_provider, + $state + ); $this->renderer = $renderer; // $authentication_providers as defined in @@ -166,7 +182,12 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition /** * {@inheritdoc} */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + public static function create( + ContainerInterface $container, + array $configuration, + $plugin_id, + $plugin_definition + ) { return new static( $configuration, $plugin_id, @@ -179,15 +200,22 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('entity.manager')->getStorage('menu') ); } + /** * {@inheritdoc} */ - public function initDisplay(ViewExecutable $view, array &$display, array &$options = NULL) { + public function initDisplay( + ViewExecutable $view, + array &$display, + array &$options = NULL + ) { parent::initDisplay($view, $display, $options); // If the default 'json' format is not selected as a format option in the // view display, fallback to the first format available for the default. - if (!empty($options['style']['options']['formats']) && !isset($options['style']['options']['formats'][$this->getContentType()])) { + if (!empty($options['style']['options']['formats']) && !isset( + $options['style']['options']['formats'][$this->getContentType()] + )) { $default_format = reset($options['style']['options']['formats']); $this->setContentType($default_format); } @@ -200,7 +228,9 @@ public function initDisplay(ViewExecutable $view, array &$display, array &$optio $this->setContentType($request_content_type); } - $this->setMimeType($this->view->getRequest()->getMimeType($this->getContentType())); + $this->setMimeType( + $this->view->getRequest()->getMimeType($this->getContentType()) + ); } /** @@ -274,7 +304,10 @@ public function getContentType() { * An array to use as value for "#options" in the form element. */ public function getAuthOptions() { - return array_combine($this->authenticationProviderIds, $this->authenticationProviderIds); + return array_combine( + $this->authenticationProviderIds, + $this->authenticationProviderIds + ); } /** @@ -307,7 +340,10 @@ public function optionsSummary(&$categories, &$options) { parent::optionsSummary($categories, $options); // Authentication. - $auth = $this->getOption('auth') ? implode(', ', $this->getOption('auth')) : $this->t('No authentication is set'); + $auth = $this->getOption('auth') ? implode( + ', ', + $this->getOption('auth') + ) : $this->t('No authentication is set'); unset($categories['page'], $categories['exposed']); // Hide some settings, as they aren't useful for pure data output. @@ -344,11 +380,15 @@ public function optionsSummary(&$categories, &$options) { public function buildOptionsForm(&$form, FormStateInterface $form_state) { parent::buildOptionsForm($form, $form_state); if ($form_state->get('section') === 'auth') { - $form['#title'] .= $this->t('The supported authentication methods for this view'); + $form['#title'] .= $this->t( + 'The supported authentication methods for this view' + ); $form['auth'] = [ '#type' => 'checkboxes', '#title' => $this->t('Authentication methods'), - '#description' => $this->t('These are the supported authentication providers for this view. When this view is requested, the client will be forced to authenticate with one of the selected providers. Make sure you set the appropriate requirements at the Access section since the Authentication System will fallback to the anonymous user if it fails to authenticate. For example: require Access: Role | Authenticated User.'), + '#description' => $this->t( + 'These are the supported authentication providers for this view. When this view is requested, the client will be forced to authenticate with one of the selected providers. Make sure you set the appropriate requirements at the Access section since the Authentication System will fallback to the anonymous user if it fails to authenticate. For example: require Access: Role | Authenticated User.' + ), '#options' => $this->getAuthOptions(), '#default_value' => $this->getOption('auth'), ]; @@ -362,7 +402,10 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) { parent::submitOptionsForm($form, $form_state); if ($form_state->get('section') == 'auth') { - $this->setOption('auth', array_keys(array_filter($form_state->getValue('auth')))); + $this->setOption( + 'auth', + array_keys(array_filter($form_state->getValue('auth'))) + ); } } @@ -404,17 +447,28 @@ public function collectRoutes(RouteCollection $collection) { * @return bool * TRUE, when the view should override the given route. */ - protected function overrideApplies($view_path, Route $view_route, Route $route) { + protected function overrideApplies( + $view_path, + Route $view_route, + Route $route + ) { $route_formats = explode('|', $route->getRequirement('_format')); $view_route_formats = explode('|', $view_route->getRequirement('_format')); return $this->overrideAppliesPathAndMethod($view_path, $view_route, $route) - && (!$route->hasRequirement('_format') || array_intersect($route_formats, $view_route_formats) != []); + && (!$route->hasRequirement('_format') || array_intersect( + $route_formats, + $view_route_formats + ) != []); } /** * {@inheritdoc} */ - public static function buildResponse($view_id, $display_id, array $args = []) { + public static function buildResponse( + $view_id, + $display_id, + array $args = [] + ) { $build = static::buildBasicRenderable($view_id, $display_id, $args); // Setup an empty response so headers can be added as needed during views @@ -450,9 +504,12 @@ public function execute() { */ public function render() { $build = []; - $build['#markup'] = $this->renderer->executeInRenderContext(new RenderContext(), function () { - return $this->view->style_plugin->render(); - }); + $build['#markup'] = $this->renderer->executeInRenderContext( + new RenderContext(), + function () { + return $this->view->style_plugin->render(); + } + ); $this->view->element['#content_type'] = $this->getMimeType(); $this->view->element['#cache_properties'][] = '#content_type'; @@ -500,18 +557,29 @@ public function calculateDependencies() { $dependencies = parent::calculateDependencies(); $dependencies += ['module' => []]; - $dependencies['module'] = array_merge($dependencies['module'], array_filter(array_map(function ($provider) { - // During the update path the provider options might be wrong. This can - // happen when any update function, like block_update_8300() triggers a - // view to be saved. - return isset($this->authenticationProviderIds[$provider]) - ? $this->authenticationProviderIds[$provider] - : NULL; - }, $this->getOption('auth')))); + $dependencies['module'] = array_merge( + $dependencies['module'], + array_filter( + array_map( + function ($provider) { + // During the update path the provider options might be wrong. This can + // happen when any update function, like block_update_8300() triggers a + // view to be saved. + return isset($this->authenticationProviderIds[$provider]) + ? $this->authenticationProviderIds[$provider] + : NULL; + }, + $this->getOption('auth') + ) + ) + ); $menu = $this->getOption('menu'); - if ($menu['type'] === 'normal' && ($menu_entity = $this->menuStorage->load($menu['menu_name']))) { - $dependencies[$menu_entity->getConfigDependencyKey()][] = $menu_entity->getConfigDependencyName(); + if ($menu['type'] === 'normal' && ($menu_entity = $this->menuStorage->load( + $menu['menu_name'] + ))) { + $dependencies[$menu_entity->getConfigDependencyKey( + )][] = $menu_entity->getConfigDependencyName(); } return $dependencies; @@ -569,4 +637,17 @@ public function getPath() { return $path . '?_format=' . reset($formats); } -} + /** + * {@inheritdoc} + */ + public function getMenuLinks() { + $links = parent::getMenuLinks(); + $menu_link_id = key($links); + $formats = $this->getFormats(); + + $links[$menu_link_id]['options']['query']['_format'] = reset($formats); + + return $links; + } + +} \ No newline at end of file