I have a custom module I'm working on and I'd like to make it configurable via the admin menu. I'm having difficulty getting a form to appear in the admin page.
Visting admin/config/MYMODULE does show the form, it's just not visible on the /admin/config page.
Here is my file structure so far (excluding irrelevant files):
*The form file is just an example form to try to get it to appear on the admin page.
MYMODULE.routing.yml
MYMODULE.settings:
path: '/admin/config/system/MYMODULE'
defaults:
_form: '\Drupal\MYMODULE\Form\myModuleSettingsForm'
_title: 'myModule Settings'
requirements:
_permission: 'administer site configuration'
MYMODULE.schema.yml
MYMODULE.settings:
type: config_object
label: 'myModule Settings'
mapping:
path_to_sound:
type: string
label: 'Path to Sound'
MYMODULE.links.menu.yml
MYMODULE.settings:
title: 'myModule Settings'
description: 'Alter myModule Settings'
route_name: MYMODULE.settings
parent: system.admin_config_system
MYMODULE.settings.yml
path:
path_to_sound: '/modules/custom/MYMODULE/myModule.mp3'
myModuleSettingsForm.php
<?php
namespace Drupal\MYMODULE\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure example settings for this site.
*/
class myModuleSettingsForm extends ConfigFormBase {
/**
* Config settings.
*
* @var string
*/
const SETTINGS = 'MYMODULE.settings';
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'MYMODULE_admin_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config(static::SETTINGS);
$form['example_thing'] = [
'#type' => 'textfield',
'#title' => $this->t('Things'),
'#default_value' => $config->get('example_thing'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Retrieve the configuration.
$this->configFactory->getEditable(static::SETTINGS)
// Set the submitted configuration setting.
->set('example_thing', $form_state->getValue('example_thing'))
// You can set multiple configurations at once by making
// multiple calls to set().
->save();
parent::submitForm($form, $form_state);
}
}
It's my understanding that I should now be able to see this form on the config page, but I do not, regardless of cache clearing, etc. I followed documentation to do this and even cross referenced other popular modules, so I'm really not sure what I'm doing wrong.