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);
}
}
?>