1

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);
  }
}
?>
7
  • 1
    Assuming your module’s machine name is weather, move the form class to module_path/src/Form/
    – Clive
    Commented Dec 8, 2021 at 19:11
  • @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: i.postimg.cc/KYqv3NYp/sc.png Commented Dec 8, 2021 at 19:23
  • 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.
    – Lambic
    Commented Dec 8, 2021 at 19:40
  • @Lambic On which version of drupal did you run the code? Commented Dec 8, 2021 at 19:49
  • We're on version 9.1
    – Lambic
    Commented Dec 8, 2021 at 19:58

1 Answer 1

4

Solution:

I just needed to restart the docker4drupal.

When I wrote the commands:

docker-compose stop
docker-compose up -d

The error is gone.

8
  • 1
    Did you not clear the cache after making changes? Restarting web servers is (extremely rare) for development.
    – Kevin
    Commented Dec 8, 2021 at 23:10
  • @Kevin yes, of course, I cleared cache after making changes. Commented Dec 8, 2021 at 23:54
  • 1
    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.
    – Kevin
    Commented Dec 9, 2021 at 1:00
  • Lost an half an hour with this :( Thank you a lot
    – Oumar
    Commented May 2, 2022 at 14:55
  • 1
    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.
    – COBadger
    Commented Oct 25, 2022 at 18:21

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