0

I have a form within custom block in drupal 8 and I know that this is not the right method to write a form within a block but someone wrote it and I am trying to fix it without changing the method. In the return of the public function build() , he rendered the html of a form within the markup.

 return array(
      '#markup' => '<form>
//we have here many inputs
//then at the end:       
 <input  type="hidden" name="form_id" value="country_language_form">
  <div class="yellow"><a class="btn button button--primary js-form-submit form-submit" id="edit-submit--2">'.t("Submit").'</a> </div>
</form>',

I was wondering how does this code work (the form is actually submitted) when the user is anonymou s and it doesn't work (click on submit doesn't do anything) when the user is logged in as admin.

1
  • 2
    Honestly you'd be better off writing it with form api and calling that from the block.
    – Kevin
    Jun 10, 2021 at 11:27

1 Answer 1

3

My first guess would be an incorrect targeting of the submit button when the user is logged in (be aware that Drupal adds a lot of elements to the DOM when being logged in).

I'd highly recommend you to use the right method and build your form with the form API, though. Your block build code would look like this:

  public function build() {
    $form = \Drupal::formBuilder()->getForm('Drupal\your_module_name\Form\YourForm');
    $build['content'] = $form;
    return $build;
  }

And all your form-related code would be in the YourForm.php file.

1
  • Thank Youuuu !! Jun 11, 2021 at 6:49

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.