Problem/Motivation

After changing the admin theme to 'Olivero' or "Standard', I'm getting this error.

Steps to reproduce

- Do a clean install of Drupal core 11.3.5
- After installation, you're logged in as an admin with the admintheme Claro. The Olivero theme is selected for the anonymous user. Check this in .../admin/appearance.
- Access the page .../admin/appearance/settings and you will see the general settings
- Go back to the page .../admin/appearance and go to the bottom of this page.
- Change the admin theme to Olivera or Standard. (Because Olivera was the standard theme, the result is the same)
- Access again the .../admin/appearance/settings page

I'm seeing this PHP error:
InvalidArgumentException: Class "olivero_form_system_theme_settings_alter" does not exist. in Drupal\Core\DependencyInjection\ClassResolver->getInstanceFromDefinition() (line 32 of core/lib/Drupal/Core/DependencyInjection/ClassResolver.php).
Drupal\Core\Utility\CallableResolver->getCallableFromDefinition() (Line: 592)
Drupal\Core\Theme\ThemeManager->getImplementationsForTheme() (Line: 556)
Drupal\Core\Theme\ThemeManager->alterForTheme() (Line: 620)
Drupal\Core\Theme\ThemeManager->alter() (Line: 893)
Drupal\Core\Form\FormBuilder->prepareForm() (Line: 304)
Drupal\Core\Form\FormBuilder->buildForm() (Line: 73)
Drupal\Core\Controller\FormController->getContentResult()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 634)
Drupal\Core\Render\Renderer::Drupal\Core\Render\{closure}()
Fiber->start() (Line: 635)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 183)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 118)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 92)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 53)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 54)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 745)
Drupal\Core\DrupalKernel->handle() (Line: 19)

This error does not appair on a Drupal 10 system.
My PHP-version is 8.3

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3581226

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ericvl created an issue. See original summary.

quietone’s picture

Version: 11.3.x-dev » main
Component: theme system » Olivero theme
Issue summary: View changes
Status: Active » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

Hi, Issues for Drupal core should be targeted to the 'main' branch, our primary development branch. Changes are made on the main branch first, and are then back ported as needed according to the Core change policies. The version the problem was discovered on should be stated in the issue summary Problem/Motivation section. Thanks.

I followed the steps in the issue summary and was not able to reproduce the problem. I am not sure what "Change the admin theme to Olivera or Standard" means because there is no 'Standard' theme in core. Where does Standard come from?

ericvl’s picture

Hi,
Thank you for your quick response.
Sorry, I didn't know about the policy to make new issues against the main branch. Glad it is on the right track now.

About the selection of the admin theme on this page. I had to translate it from Dutch to English and that probably didn't work out so good.
There is a listbox on the bottom of the page with 3 possibilities; "Standard" or " Default if you wish, Olivero and Claro. This is the one that you should change to Standard or Olivero. Save it and go to the page .../admin/appearance/settings. Then you will see the php error.
Greetings

ericvl’s picture

Status: Postponed (maintainer needs more info) » Active
cilefen’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs steps to reproduce

I am unable to reproduce this bug as written. Perhaps there are additional steps to reproduce?

ericvl’s picture

Strange

- setup DDEV on Windows 10
- mkdir drupal11
- cd drupal11
- ddev config --docroot=web --project-type=drupal11
- ddev config --php-version=8.3
- ddev composer create-project drupal/recommended-project:11.3.5 .
- ddev launch

Run "standard" installation in Dutch and then follow the setup above.
Success

cilefen’s picture

Component: Olivero theme » theme system
Priority: Normal » Major
Status: Postponed (maintainer needs more info) » Active
Issue tags: -Needs steps to reproduce

Ah, I missed the detail about going to the Appearance > Settings page! This is reproducible in any language.

nicxvan’s picture

Interesting, the ThemeSettingsForm has this:

foreach ($theme_keys as $theme) {
        // Include the theme-settings.php file.
        $theme_path = $this->themeHandler->getTheme($theme)->getPath();
        $theme_settings_file = $theme_path . '/theme-settings.php';
        $theme_file = $theme_path . '/' . $theme . '.theme';
        $filenames = [$theme_settings_file, $theme_file];
        foreach ($filenames as $filename) {
          if (file_exists($filename)) {
            require_once $filename;

            // The file must be required for the cached form too.
            $files = $form_state->getBuildInfo()['files'];
            if (!in_array($filename, $files)) {
              $files[] = $filename;
            }
            $form_state->addBuildInfo('files', $files);
          }
        }
      }

before: $this->themeManager->alterForTheme($editing_active_theme, 'form_system_theme_settings', $form, $form_state);

cilefen’s picture

This bug released in 11.3.0-rc2.

shreyal999’s picture

Assigned: Unassigned » shreyal999

The same issue is still happening in 11.3.5

InvalidArgumentException: Class "olivero_form_system_theme_settings_alter" does not exist. in Drupal\Core\DependencyInjection\ClassResolver->getInstanceFromDefinition() (line 32 of core/lib/Drupal/Core/DependencyInjection/ClassResolver.php).

ericvl’s picture

@shreyal999
Any progress on this issue?

cilefen’s picture

Issue tags: +Needs tests

@shreyal999, this was reported for 11.3.5, and it hasn’t been fixed. What I said was that this bug released in 11.3.0-rc2, meaning that is when the bug appeared in a tag. Apologies if I wasn’t clear.

This obviously needs test coverage

cilefen’s picture

nicxvan’s picture

Yes we probably just need to move that include logic somewhere higher.

nicxvan’s picture

I pushed up a fix, we can probably narrow down to the current theme and I have not added tests yet.

ericvl’s picture

@nixvan
I've patched locally my 11.3.5 version with the contents of your merge request and I can say that the patch works. Ofcourse I don't know if this is the way to go because I don't have a clear view on the code. Thanks for your input.

ericvl’s picture

@nixvan
Another constatation after applying your path.
I do not get the PHP-error anymore but the form is different from the one created under core version 10.
Under version 11.5.3 I'm getting some extra settings from the Olivero theme although I'm selecting the general settings with .../admin/appearance/settings.
Under version 10 I'm only getting the general settings Page-element display, Logo and Favicon, wich is correct.
Greetings

jwilson3’s picture

Until this gets fixed in core, one way to guard against this without the patch from this issue it is to move your theme settings form alter into *.theme so that it is always registered. This may or may not be a good idea.

My diff looks something like this:

diff --git a/mytheme.theme b/mytheme.theme
index 33b60b3..a8df440 100644
--- a/mytheme.theme
+++ b/mytheme.theme
@@ -696,6 +696,20 @@ function mytheme_theme_suggestions_form_alter(array &$suggestions, array $variable
   }
 }
 
+/**
+ * Implements hook_form_system_theme_settings_alter().
+ */
+function mytheme_form_system_theme_settings_alter(&$form, FormStateInterface &$form_state, $form_id = NULL) {
+  // Avoid altering the global settings form when MyTheme is the admin theme.
+  // @see https://www.drupal.org/i/3581226
+  // @see https://www.drupal.org/i/943212
+  if (isset($form_id)) {
+    return;
+  }
+
+  require_once __DIR__ . '/theme-settings.php';
+  _mytheme_form_system_theme_settings_alter($form, $form_state);
+}
+
 /**
  * Implements hook_form_FORM_ID_alter() for the content moderation form.
  */
diff --git a/theme-settings.php b/theme-settings.php
index f509830..261d730 100644
--- a/theme-settings.php
+++ b/theme-settings.php
@@ -8,14 +8,9 @@
 use Drupal\Core\Form\FormStateInterface;
 
 /**
- * Implements hook_form_system_theme_settings_alter().
+ * Adds MyTheme-specific settings to the theme settings form.
  */
-function mytheme_form_system_theme_settings_alter(&$form, FormStateInterface &$form_state, $form_id = NULL) {
-  // Work-around for a core bug affecting admin themes. See issue #943212.
-  if (isset($form_id)) {
-    return;
-  }
-
+function _mytheme_form_system_theme_settings_alter(&$form, FormStateInterface &$form_state) {
   // Comments
   // Comments
   // Comments
nicxvan’s picture

This may or may not be a good idea.

That is a valid way to fix this.

The actual recommendation is to convert the hook to oop which is supported since 11.3.

Separate theme.settings does were deprecated in 11.4 and removed in 12.

Under version 11.5.3 I'm getting some extra settings from the Olivero theme although I'm selecting the general settings with

That's interesting, I probably included too many themes in the loop.

Converting the hook should fix this and then you don't need the patch.

jwilson3’s picture

The actual recommendation is to convert the hook to oop which is supported since 11.3.

Thanks for that. I was seeing this as a Drupal 12 compatibility addition, but might make sense to go ahead and add that. The D12-compatibility issue for my theme doesn't (yet) have an automated rector fix for converting theme-settings.php to oop.

Under version 11.5.3 I'm getting some extra settings from the Olivero theme although I'm selecting the general settings

The extra settings might be appearing due to #943212: hook_form_system_theme_settings_alter() vs. THEME_form_system_theme_settings_alter() name-clash, which I guard for using the $form_id parameter, if $form_id is empty, then its the global settings form, and I therefore skip my custom theme additions. Sadly that issue was closed. I might be totally wrong about this.

function mytheme_form_system_theme_settings_alter(&$form, FormStateInterface &$form_state, $form_id = NULL) {
  // Avoid altering the global settings form when MyTheme is the admin theme.
  // @see https://www.drupal.org/i/3581226
  // @see https://www.drupal.org/i/943212
  if (isset($form_id)) {
    return;
  }