0

I have a simple custom form that works until I try to add a custom parameter for my module called "forms_admin".

These are the contents of my forms_admin.routing.yml

forms_admin.settings:
  path: 'admin/config/system/forms/{arg1}'
  defaults:
    _form: '\Drupal\forms_admin\Form\FormSettings'
    _title: 'Forms Settings'
  requirements:
    _permission: 'access content'

This is a relevant part of my FormSettings.php file (Currently do not have any custom Validation or Submit actions)

public function buildForm(array $form, FormStateInterface $form_state, $arg1 = NULL) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#required' => TRUE,
    );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );


return $form;
}

And my third file is: forms_admin.links.menu.yml

forms_admin.config:
  title: Forms
  route_name: forms_admin.settings
  parent: system.admin_config_system
  description: 'Forms settings'
  weight: 0

The moment that I add {arg1} to my forms_admin.routing.yml and , $arg1 = NULL to my FormSettings.php file, I am unable to acces the website.


I receive a white page with the error that states:

The website encountered an unexpected error. Please try again later.

After removing arg1 from my two files and going to the Recent Log messages, this is the error I receive:

Symfony\Component\Routing\Exception\MissingMandatoryParametersException: Some mandatory parameters are missing ("arg1") to generate a URL for route "forms_admin.settings". in Drupal\Core\Routing\UrlGenerator->doGenerate() (line 182 of /var/www/test_site/docroot/core/lib/Drupal/Core/Routing/UrlGenerator.php).


I have tried changing arg1 to other names, not making $arg1 = NULL, etc. and I receive an error every time.

What am I missing?

  • 1
    It looks good to me - the parameter is named the same and is optional. The error indicates something's trying to generate a URL from the route without a parameter (which obviously can't work). I'm not sure the error's coming directly from a mistake in the code you've posted (it just manifests when you have a required parameter). If you have for example a menu link tied to the route you should provide a default parameter so it can generate the URL IIUC. See optional parameters from the routing docs. – Andy Jul 27 '18 at 11:03
  • @Andy The menu link was the issue! Thank you for catching that! How do I modify it to provide a default parameter? – Andrew Webs Jul 27 '18 at 16:16
  • It's not the link itself that you need to modify, it's the route: see the link in my previous comment and look under optional parameters. For example in ban.routing.yml there are two routes with parameters, but only ban.admin_page specifies a default for its parameter. – Andy Jul 28 '18 at 9:58
0

The problem is that your FormSettings class is not the controller in this case. It's the controller method which is where the routing system expects $arg_1.

I think you need to define your own controller which accepts the request and passes the argument to the form. Extending the core Drupal\Core\Controller\FormController class is probably the simplest way forward.

  • I don't think you need a custom controller to pass on arguments. See eg. ban.delete in ban.routing.yml and Drupal\ban\Form\BanDelete::buildForm(). – Andy Jul 27 '18 at 10:52
  • I stand corrected. How odd that your code doesn't work, then. – Alfred Armstrong Jul 27 '18 at 10:57
  • 1
    Is there a reference to "forms_admin.settings" anywhere else? Seems like it's the default (without arg) that's being objected to. Maybe try adding a default entry for it in routing.yml? – Alfred Armstrong Jul 27 '18 at 11:01
  • Just posted the same, didn't notice your reply - that's my guess, yeah. – Andy Jul 27 '18 at 11:04
  • Thank you for all of your comments and thoughts! The issue ended up being in my .links.menu.yml file. – Andrew Webs Jul 27 '18 at 16:16

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.