0

I newish to Drupal and very new to Drupal 8, as this comes out in a couple of weeks it seems to make sense to grab the bull by the horns and start any new project with this now! Anyway I was following pretty good tutorial: http://www.drupalwoo.com/content/blog/my-first-drupal-8-module on creating a Drupal 8 module but it looks to be a year or two old and things have changed. I am getting the following error:

Fatal error: Declaration of Drupal\bd_contact\AddForm::getFormId() must be compatible with Drupal\Core\Form\FormBuilderInterface::getFormId($form_arg, Drupal\Core\Form\FormStateInterface &$form_state) in modules\custom\bd_contact\src\AddForm.php on line 7

This is the file in question: https://github.com/nlisgo/bd_contact/blob/master/src/AddForm.php could anyone tell me what would be the correct way? I have googled it quite a bit and found similar issues but I can't see how I'd integrate those changes into mine.

2

In my project it works as public function without arguments since drupal beta 1 until today (i've updated today to rc3):

 public function getFormId() {

You only have to add public to your function.

Otherwise your error suggest, that there are the two arguments form and form_state missing, what is also described in the api.drupal.org:

public function FormBuilder::getFormId

But I don't think, that you need these arguments in the definition of the getFormId(). This is probably a false alarm. You only need these arguments, when using the function in the context of formBuilder, when you don't provide the form through your class, for example like this:

this->formBuilder->getFormId($form_arg, $form_state)

Edit: Berdir has the right clue, you have to change this:

wrong: class AddForm implements FormInterface {
right: class AddForm extends FormBase {

and the use statements for this are:

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

This is a complete example:

<?php
/**
 * @file
 * Contains \Drupal\mymodule\Form\OrderForm
 */

namespace Drupal\mymodule\Form;

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

/**
 * Implements an form.
 */
class OrderForm extends FormBase {

  /**
   * {@inheritdoc}.
   */
  public function getFormId() {
    return 'mymodule_order_form';
  }

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

  }


   /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

  }


   /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

  }
  • Hi, thanks for your help. I am now getting: Fatal error: Declaration of Drupal\bd_contact\AddForm::validateForm() must be compatible with Drupal\Core\Form\FormInterface::validateForm(array &$form, Drupal\Core\Form\FormStateInterface $form_state) in C:\xampp\htdocs\drupal\modules\custom\bd_contact\src\AddForm.php on line 48 – karl Nov 5 '15 at 15:15
  • That's the same problem now in validate. I put a complete example in the answer. – 4k4 Nov 5 '15 at 16:46
0

I think you are implementing the wrong interface (always include example code, if it's long, post it somewhere and link to it).

Drupal\Core\Form\FormBuilderInterface is the API that one calls when displaying a specific form. It's basically what drupal_get_form() and other functions from form.inc used to be.

What you want to implement in a class that represents a form is Drupal\Core\Form\FormInterface.

  • The class does implement FormInterface. I think this issue is particular to a module relying on older Drupal 8 code. Someone already @simonventuri already created a pull request to fix it. – mradcliffe Nov 5 '15 at 13:57

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.