Admin theme not active when using batch export method and a display path starting with admin/

Created on 23 June 2023, over 1 year ago

Problem/Motivation

When using the batch export method in combination with a path that starts with admin/ it is expected that the admin theme would be active and used for the batch export.

Currently the batch will use the default theme instead of the admin theme, as neither Drupal\views_data_export\Plugin\views\display\DataExport::collectRoutes (via parent Drupal\rest\Plugin\views\display\RestExport::collectRoutes) nor Drupal\system\EventSubscriber\AdminRouteSubscriber::alterRoutes set the route option _admin_route - which is used to determine the active theme.

Steps to reproduce

I have only tested this with 9.5

  1. Install Drupal using core profile
  2. Enable views_data_export
  3. Add a Data Export display to the content view, use Method: Batch under export settings and use admin/content/export (or any path beginning with admin) as the path.
  4. Visit /admin/content/export
  5. Observe the batch process is using the site theme, not the admin theme

From debugging we can see

When the routes are built:

  1. Drupal\rest\Plugin\views\display\RestExport::collectRoutes returns the route object with _format set to the export format selected in "Accepted request formats" in the view (e.g csv)
  2. AdminRouteSubscriber::isHtmlRoute returns FALSE and so does not mark this as an admin route - it does this by checking the _format requirement of the route to see if it has the html format. In this case, the format is csv as so it does not get marked as an admin route

When the request is handled:

  1. \Drupal\user\Theme\AdminNegotiator does not apply as the Drupal\Core\Routing\AdminContext::isAdminRoute returns FALSE for the route
  2. DataExport::buildBatch calls batch_process which calls \Drupal::theme()->getActiveTheme()->getName() to set the theme for the batch - which is not the admin theme due to the above conditions.

Proposed resolution

I believe if batch it in use, it is expected behaviour that using an admin path /admin should use the admin theme.

This would involve the DataExport plugin overriding the collectRoutes method to add html to the format requirements if batch option is used.

I have not investigated this further as to if that is technically appropriate given the plugins use of formats / allowed formats - I have only evaluated that if html is included in the format, that the admin theme is correctly used.

Example proposed solution:

  public function collectRoutes(RouteCollection $collection) {
    parent::collectRoutes($collection);

    // \Drupal\system\EventSubscriber\AdminRouteSubscriber will not mark routes being with admin as an admin route unless it has the html format.
    if ($this->getOption('export_method') === 'batch') {
      $view_id = $this->view->storage->id();
      $display_id = $this->display['id'];

      if ($route = $collection->get("view.$view_id.$display_id")) {
        $formats = $route->getRequirement('_format');
        $formats .= '|html'; 
        $route->setRequirement('_format', $formats);
      }

    }
  }

Remaining tasks

  1. Agree on approach
  2. Patch
  3. Review

User interface changes

TBC

API changes

TBC

Data model changes

TBC

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇳🇿New Zealand ericgsmith

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024