1

I built a custom configuration form with the following settings

The contents of my aaep_settings_main.routing.yml

my_settings:
  path: '/admin/config/system/my_settings'
  defaults:
    _form: 'Drupal\my_settings\Form\SettingsForm'
  requirements:
    _permission: 'administer site configuration'

And then inside my SettingsForm.php I have

class SettingsForm extends ConfigFormBase {

protected function getEditableConfigNames() {
    return array(
        'my_settings.settings',
    );
}

public function submitForm(array&$form, FormStateInterface $form_state) {

    $this->config('my_settings.settings')
         ->set('test', $form_state->getValue('test'))
         ->save();
}

public function buildForm(array $form, FormStateInterface $form_state){
    $config = $this->config('my_settings.settings');
    $form['test'] = [
         '#type' => 'textfield',
         '#title' => t('Test'),
         '#default_value' => $config->get('test'),
    ];
}

It works just fine, but when I visit the page it's using the admin theme instead of the regular theme. I'm assuming that's because it's using a configuration form and extending ConfigFormBase. I'm assuming that that's the default behavior. But is there a way to override that and have my configuration page use my site theme instead?

  • I believe you need to implement a ThemeNegotiator to change the active theme for these routes. I have not done that yet so I'll leave it to an expert to answer. – Kevin Mar 10 '17 at 17:26
2

You can control this in the route definition.

If you give non admin users access to the form they will see it in a regular theme:

my_settings:
  path: '/admin/config/system/my_settings'
  defaults:
    _form: 'Drupal\my_settings\Form\SettingsForm'
  requirements:
    _permission: 'access content'

If you want to have admins see the form in a regular theme then move the path outside of /admin:

my_settings:
  path: '/other-path/my_settings'
  defaults:
    _form: 'Drupal\my_settings\Form\SettingsForm'
  requirements:
    _permission: 'administer site configuration'

Or do both at the same time.

Details:

If you give a non admin user permission for an admin route, then the form will be displayed in the regular theme, because the admin theme is only accessible for admin users or users who have the "administration theme" permission.

Whether a route is considered as admin route is controlled by the route option _admin_route. If this option is not set, it is added by AdminRouteSubscriber::alterRoutes to all routes with an admin path:

  protected function alterRoutes(RouteCollection $collection) {
    foreach ($collection->all() as $route) {
      if (strpos($route->getPath(), '/admin') === 0 && !$route->hasOption('_admin_route')) {
        $route->setOption('_admin_route', TRUE);
      }
    }
  }

If you want to override this you can set the option to false:

  options:
    _admin_route: FALSE
  • Isn't there a way to force the theme besides this, too? – Kevin Mar 10 '17 at 18:07
  • Is there a way to force a preexisting core admin settings page like the user edit page to use the regular theme as well? – Matt Mar 10 '17 at 18:39
  • There will be @Matt, probably a combination of the unaccepted answer here and the _admin_route option as explained here. Probably worth asking a new question if that's not enough to go off, I don't think the solution would easily fit in comments – Clive Mar 10 '17 at 18:55
  • @Matt, set the admin_route to FALSE, see the edited answer – 4k4 Mar 10 '17 at 18:57
  • Clarification on the "admin user". to be more specific, it is about having or not having the use administration theme permission. So you can give a user as many "admin" permission as you want, as long as you don't give them that, they won't see the admin theme. – Berdir Mar 10 '17 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.