Score:-2

InvalidArgumentException: Class ... does not exist in custom configuration form

id flag

I create configuration form, so that I could dynamically set the API key for my custom weather module.

But when I write in the url address http://drupalsite/admin/config/services/weather/settings I get the error:

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

I checked the Class Name and File name and cleared the caches but the error did not go away. How to fix it?

weather.routing.yml

weather.weather_page:                               
  path: '/weather/{city}'
  defaults:                                                       
    _controller: '\Drupal\weather\Controller\WeatherPage::getWeather'                          
  requirements:                                        
     _permission: 'access content'
weather.settings:
  path: '/admin/config/services/weather/settings'
  defaults:
    _form: '\Drupal\weather\Form\WeatherSettingsForm'
    _title: 'Weather Settings form'
  requirements:
    _permission: 'administer site configuration'

WeatherSettingsForm.php

<?php

namespace Drupal\weather\Form;         

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Configure example settings for this site.
 */
class WeatherSettingsForm extends ConfigFormBase {      
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'weather_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'weather.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('weather.settings');

    $form['weather_api_key'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('API Key'),
      '#default_value' => $config->get('weather_api_key'),
    );

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory->getEditable('weather.settings')
      ->set('weather_api_key', $form_state->getValue('weather_api_key'))
      ->save();

    parent::submitForm($form, $form_state);
  }
}
?>
cn flag
Assuming your module’s machine name is `weather`, move the form class to `module_path/src/Form/`
bimsalrobit avatar
id flag
@Clive Excuse me, maybe I didn't understand you, but you say that I need to move the `WeatherSettingsForm.php` file which contains the `WeatherSettingsForm` class to a folder `modules/custom/weather/src/Form` ? But this file is there: https://i.postimg.cc/KYqv3NYp/sc.png
Lambic avatar
ph flag
I ran the code as posted and it works for me, so maybe you have a typo somewhere locally, or maybe a hidden character got in somewhere.
bimsalrobit avatar
id flag
@Lambic On which version of drupal did you run the code?
Lambic avatar
ph flag
We're on version 9.1
bimsalrobit avatar
id flag
@Lambic I found a solution to the problem, I just had to restart the docker4drupal.
Matoeil avatar
za flag
add $settings['class_loader_auto_detect'] = FALSE; to local.settings.php
Score:1
id flag

Solution:

I just needed to restart the docker4drupal.

When I wrote the commands:

docker-compose stop
docker-compose up -d

The error is gone.

Kevin avatar
in flag
Did you not clear the cache after making changes? Restarting web servers is (extremely rare) for development.
bimsalrobit avatar
id flag
@Kevin yes, of course, I cleared cache after making changes.
Kevin avatar
in flag
Something else is wrong or happened then, because a server restart to see changes is not normal, unless you enabled the module (registering the class paths) and then moved some files. That sometimes may require a restart to wipe the class path cache.
ve flag
Lost an half an hour with this :( Thank you a lot
ph flag
Hard to imagine, but the same approach worked for me in my Lando-based local dev: I did a 'lando restart' and the class resolver could find my class.
es flag
Wow, same thing here. It was driving me crazy because the code looked perfect! Required a lando restart.
Geat avatar
de flag
I never would have worked this out. I was getting the "The [entity] entity type did not specify a [action] form class" error, hacked EntityTypeManager to find the real reason was that the form class "didn't exist" - when it did, but I'd previously made a mistake in the entity definition that no amount of module reinstalls or cache clears would fix. Eventually a `lando restart` was the only thing that worked.
Ngatia Frankline avatar
mx flag
Also read: https://www.drupal.org/project/drupal/issues/3342669
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.