Score:0

html Form within a block

eg flag

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.

Kevin avatar
in flag
Honestly you'd be better off writing it with form api and calling that from the block.
Score:3
de flag

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.

Mira chalak avatar
eg flag
Thank Youuuu !!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.