2

I'm trying to understand how to port the following D7 code to Drupal 8.

function x_menu() {
    $menu['x/form/test/%/%'] = [
        'page callback' => 'drupal_get_form',
        'page arguments' => ['x_form_test', 3, 4],
        'type' => MENU_CALLBACK,
        'title' => 'Test Form',
    ];
    return $menu;
}


function x_form_test(&$form, $form_state, $arg1, $arg2) {
    return $form;
}

From what I can glean from the documentation of the Routing and Form API, the new way to connect a path to a form is to put this in x.routing.yml:

x.form_test:
    path: 'x/form/{arg1}/{arg2}',
    defaults:
        _form: '\Drupal\x\XFormTest'
        _title: 'Test Form'

and then create a form class XFormTest that ultimately descends from FormBase.

However, it's clearly impossible* to add extra arguments to an inherited function signature like public FormInterface::buildForm(array $form, FormStateInterface $form_state)

How can I read $arg1 and $arg2 in XFormTest::buildForm(), preferably while still pointing the route at the form rather than putting a Controller around it?

(Routing API: https://www.drupal.org/node/2092643)

*Update: Maybe it's not so much impossible as that I'm not doing it correctly.

My function declaration

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class XBBCodeHandlerForm extends FormBase {
  public function buildForm(array $form, FormStateInterface &$form_state, $format = XBBCODE_GLOBAL) {
  }
}

generates the following fatal error:

PHP Fatal error:  Declaration of Drupal\xbbcode\Form\XBBCodeHandlerForm::buildForm() 
must be compatible with 
Drupal\Core\Form\FormInterface::buildForm(array $form, 
Drupal\Core\Form\FormStateInterface $form_state)
 in /home/arancaytar/software/web/drupal/modules/xbbcode/src/Form/XBBCodeHandlerForm.php
on line 161

Edit: I misunderstood the fatal error; it wasn't caused by the extra argument but by changing the value argument $form_state to a reference argument. PHP does allow adding optional arguments to overriding functions, apparently.

closed as off-topic by kiamlaluno Apr 22 '15 at 10:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

1

it's clearly impossible to add extra arguments to an inherited function signature

Not true - adding extra arguments to a function signature is one of the fundamental reasons that inheritance exists in OO.

You literally just need to add your arguments to the function signature to make this work:

public function buildForm(array $form, FormStateInterface $form_state, $arg1, $arg2) {
    // You have $arg1 and $arg2 available
}

You could do something more fancy and get typed data by using parameter upcasting, otherwise you'll get the args as strings.

Examples here.

  • I must be doing it wrong somehow, because I'm getting this error: Declaration of Drupal\xbbcode\Form\XBBCodeHandlerForm::buildForm() must be compatible with Drupal\Core\Form\FormInterface::buildForm(array $form, Drupal\Core\Form\FormStateInterface $form_state) in /home/arancaytar/software/web/drupal/modules/xbbcode/src/Form/XBBCodeHandlerForm.php on line 161 My function declaration is: public function buildForm(array $form, FormStateInterface &$form_state, $format = XBBCODE_GLOBAL) – Christoph Burschka Apr 14 '15 at 15:50
  • What is your form class extending? (it should be FormBase or a derivative, not an interface) – Clive Apr 14 '15 at 15:53
  • I'm extending FormBase (question updated) – Christoph Burschka Apr 14 '15 at 15:57
  • Just in the middle of something sorry, will take a look in a bit – Clive Apr 14 '15 at 15:58
  • Oops, I just noticed that I'm taking the form_state as a reference for some reason. If I delete the '&', I don't get the same error anymore. – Christoph Burschka Apr 14 '15 at 16:04

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