0

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.

0

1 Answer 1

0

To use parent: system.admin_config for a menu link it needs a route that has _controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage' as a controller. And only now you can add children to that link.

parent: system.admin_config is for creating new thematic blocks. While you could use parent: system.admin_config_content, parent: system.admin_config_system etc. to add your link to an existing block.

So either switch parent: system.admin_config to parent: system.admin_config_content for example in your links Yaml and in your routing Yaml the path to path: '/admin/config/content/MYMODULE' to add your link to the "Content authoring" block.

Or adjust your MYMODULE.routing.yml and MYMDOULE.links.menu.yml to look like shown below instead to create a new thematic block and let your link be a child of that.

MYMODULE.routing.yml:

MYMODULE.settings:
  path: '/admin/config/MYMODULE'
  defaults:
    _controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage'
    _title: 'MyModule'
  requirements:
    _permission: 'access administration pages'

MYMODULE.example_thing:
  path: '/admin/config/MYMODULE/example-thing'
    _form: '\Drupal\MYMODULE\Form\myModuleSettingsForm'
    _title: 'Example thing'
  options:
    _admin_route: TRUE
  requirements:
    _permission: 'access administration pages'

MYMODULE.links.menu.yml

MYMODULE.settings:
  title: 'MyModule'
  route_name: MYMODULE.settings
  parent: system.admin_config
  description: 'Configure MyModule'
  weight: -10

MYMODULE.example_thing:
  title: 'Example thing'
  parent: MYMODULE.settings
  description: 'Example thing configuration'
  weight: 0
  route_name: MYMODULE.example_thing
4
  • I understand and appreciate this answer, thank you. However, I tested adding it to an existing block, like system, doing as you said, and it still doesn't appear
    – Joseph
    Mar 31 at 19:21
  • @Joseph – If you switched to parent: system.admin_config_system in your links Yaml, switch to path: '/admin/config/system/MYMODULE' in your routing Yaml. Flush cache, of course, and if this still doesn't work, simply take a look at Drupal's code base, especially all the modules. There are plenty of examples of how you correctly put a link on the /admin/config page. For example git.drupalcode.org/project/drupal/-/blob/9.3.9/core/modules/… and git.drupalcode.org/project/drupal/-/blob/9.3.9/core/modules/….
    – leymannx
    Mar 31 at 20:15
  • I edited my question to change the links and routing files to what they are now which are almost identical copies of how it is done in the examples you provided, and it still won't appear. Am I missing something, or is there some other thing potentially preventing it from working?
    – Joseph
    Apr 1 at 22:51
  • 1
    is there some other thing potentially preventing it from working? Can you reproduce module's issue on a fresh Drupal install, if not, then yeah something is preventing from working as it should @Joseph
    – No Sssweat
    Apr 4 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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