Commit 1fc25893 authored by Eric Smith's avatar Eric Smith Committed by Steven Jones
Browse files

Issue #3368855: Admin theme not active when using batch export method and a...

Issue #3368855: Admin theme not active when using batch export method and a display path starting with admin/
parent f3d4a5ff
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ use Drupal\views\Views;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Symfony\Component\Routing\RouteCollection;

/**
 * Provides a data export display plugin.
@@ -37,6 +38,26 @@ use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
 */
class DataExport extends RestExport {

  /**
   * {@inheritdoc}
   */
  public function collectRoutes(RouteCollection $collection) {
    parent::collectRoutes($collection);

    // \Drupal\system\EventSubscriber\AdminRouteSubscriber will not apply the
    // _admin_route to the view as the route is not an HTML route - but the
    // batch should pick up the admin theme for this route if it begins with
    // /admin.
    if ($this->getOption('export_method') === 'batch' && str_starts_with($this->getOption('path') ?? '', 'admin/')) {
      $view_id = $this->view->storage->id();
      $display_id = $this->display['id'];

      if ($route = $collection->get("view.$view_id.$display_id")) {
        $route->setOption('_admin_route', TRUE);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
+99 −0
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  module:
    - rest
    - user
id: test_admin_path
label: 'Test admin path'
module: views_data_export
description: ''
tag: ''
base_table: entity_test
base_field: id
core: 8.x
display:
  default:
    display_plugin: default
    id: default
    display_title: Master
    position: null
    display_options:
      access:
        type: perm
        options:
          perm: 'access content'
      cache:
        type: tag
      query:
        type: views_query
      exposed_form:
        type: basic
      style:
        type: data_export
      row:
        type: data_entity
      sorts:
        id:
          id: standard
          table: entity_test
          field: id
          order: DESC
          plugin_id: date
          entity_type: entity_test
          entity_field: id
      title: 'Test admin path'
      arguments: {  }
  data_export_1:
    display_plugin: data_export
    id: data_export_1
    display_title: serializer
    position: null
    display_options:
      path: admin/test/data_export/entity
      filename: foo.csv
      style:
        type: data_export
        options:
          formats:
            json: json
      export_method: batch
      export_batch_size: 4
      export_limit: 3
      displays:
        page_1: page_1
        default: '0'
  data_export_2:
    display_plugin: data_export
    id: data_export_2
    display_title: serializer
    position: null
    display_options:
      path: test/data_export/entity
      filename: foo.csv
      style:
        type: data_export
        options:
          formats:
            json: json
      export_method: batch
      export_batch_size: 4
      export_limit: 3
      displays:
        page_1: '0'
        default: '0'
  page_1:
    display_plugin: page
    id: page_1
    display_title: page
    position: null
    display_options:
      defaults:
        access: false
        style: false
        row: false
      style:
        type: default
      row:
        type: entity:entity_test
      path: test/data_export/page
+53 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\views_data_export\Kernel\Plugin\views\display;

use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;

/**
 * Tests the data export view plugin routes.
 *
 * @group views_data_export
 */
class DataExportRoutesTest extends ViewsKernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $testViews = ['test_admin_path'];

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'views_data_export',
    'entity_test',
    'serialization',
    'rest',
    'views_data_export_test',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE, $modules = ['views_test_config']):void {
    parent::setUp($import_test_views);

    ViewTestData::createTestViews(get_class($this), ['views_data_export_test']);
  }

  /**
   * Tests if routes are using batch export are marked as admin routes.
   */
  public function testBatchExportAdminPath() {
    $route_provider = \Drupal::service('router.route_provider');

    $admin_route = $route_provider->getRouteByName('view.test_admin_path.data_export_1');
    $this->assertTrue($admin_route->getOption('_admin_route'));

    $non_admin_route = $route_provider->getRouteByName('view.test_admin_path.data_export_2');
    $this->assertNotTrue($non_admin_route->getOption('_admin_route'));
  }

}