2

I'm using :

public function build() {

    $form = $this->formBuilder->getForm('Drupal\mymodule\Form\Myformclass');
    return $form;
  }

to get form inside block. But Drupal community doesn't find it as an appropriate way to get form inside a block.

According to Drupal community module reviewing team, Do not hard-code class names in strings. I tried calling class directly but that is not serving my purpose, giving 505 error.

What's the right way to get a form inside a block?

1
  • That's how I do it. I'm curious as to their reasoning behind their comments. Did they give any more comments and/or examples?
    – Jaypan
    Commented Mar 23, 2017 at 11:54

2 Answers 2

6

You can get the fully qualified class name by using ClassName::class:

use Drupal\mymodule\Form\Myformclass;

  public function build() {

    $form = $this->formBuilder->getForm(Myformclass::class);
    return $form;
  }

If you want to avoid the full classname in code.

0

The following code will help to load the custom form SampleDataForm defined on the path Drupal\mymodule\src\Form\ SampleDataForm.php

namespace Drupal\mymodule\Form;


class MymoduleSearchBlockForm extends SampleDataForm 
{
  protected $formIdentifier;

  public function setFormIdentifier($formIdentifier)
  {
      $this->formIdentifier = $formIdentifier;
  }

  public function getFormId()
  {
    $form_id = 'sample_form';
    if($this->formIdentifier)
    {
        $form_id .= '-' . $this->formIdentifier;
    }
    return $form_id;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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