Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 367 Vote(s) - 3.63 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a form using block module in drupal 8?

#1
I want build a form using a block module in `Drupal 8`. I am aware of building the forms in `Drupal` 7 but the same seems to be different in Drupal 8.

Request anyone who has worked on drupal8 custom forms as block to help me.
Reply

#2
Here is a detailed summary of how to go about this:-

[To see links please register here]


Following the above guide, you would add the completed form to the block build function, e.g.

class DemoBlock extends BlockBase {

/**
* {@inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('Drupal\demo\Form\DemoForm');
return $form;
}

}

Some more useful docs if you are new to Drupal 8 or need to dust off your knowledge:

[To see links please register here]


[To see links please register here]


[To see links please register here]

Reply

#3
Your question is very vague, as I don't know how much you already know about modules, forms and blocks in Drupal 8. So here is a small guide what to do, further information on how to do stuff in detail would be overkill for this answer.

**1. Create a new module and enable it**

Look here: [Naming and placing your Drupal 8 module](

[To see links please register here]

).

Basically you create the module folder and the module info yml file to let Drupal know about the module. Then you enable it using drush or the admin area in Drupal.

**2. Create the form**

Look here: [Introduction to Form API](

[To see links please register here]

).

under `your_module/src/Form` you create the form. More details in the link above.

**3. Create the block and render the form**

Look here: [Create a custom block](

[To see links please register here]

).

under `your_module/src/Plugin/Block/` you create the block which will render the form.

The idea is basically (code updated with suggestion from Henrik):

$builtForm = \Drupal::formBuilder()->getForm('Drupal\your_module\Form\Your‌​Form');
$renderArray['form'] = $builtForm;

return $renderArray;

Note: You don't need to wrap the `$builtForm` with the `$renderArray`, you can return just the `$builtForm` and be fine. I just personally like to do it that way, because often times I need to add something else to the final render array like some markup, cache settings or a library etc.

**4. Place the block**

Place the block in the desired region(s). Done.
Reply

#4
To build a form using block module, you can easily use [Webform module][1] where you can add a form and display as a block.


----------


If you mean to create a form programatically in the custom block, you can achieve that by creating two files shown below:

Form file (`src/Form/DemoForm.php`):

<?php

/**
* @file
* Contains \Drupal\demo\Form\DemoForm.
*/

namespace Drupal\demo\Form;

use Drupal\Core\Form\FormBase;

class DemoForm extends FormBase {

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

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

$form['email'] = array(
'#type' => 'email',
'#title' => $this->t('Your .com email address.')
);
$form['show'] = array(
'#type' => 'submit',
'#value' => $this->t('Submit'),
);

return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
$values = $form_state->getValues();
if (strpos($values['email'], '.com') === FALSE ) {
$form_state->setErrorByName('email', t('This is not a .com email address.'));
}
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message($this->t('Your email address is @email', array('@email' => $form_state['values']['email'])));
}

}

<sup>Source: [Building a Drupal 8 Module: Blocks and Forms][2].</sup>

Block file (`src/Plugin/Block/HelloBlock.php`):

<?php

namespace Drupal\mymodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
* Provides a 'Hello' Block.
*
* @Block(
* id = "form_block",
* admin_label = @Translation("My form"),
* category = @Translation("My Category"),
* )
*/
class HelloBlock extends BlockBase {

/**
* {@inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('\Drupal\mymodule\Form\HelloBlock');
//$form['#attached']['js'][] = drupal_get_path('module', 'example') . '/js/example.js';
//$form['#markup'] = $this->t('Custom text');
return $form;
}

}

<sup>Source: [Create a custom block][3].</sup>


----------

To add a form to the *Block Configuration*, see: [Add a Form to the Block Configuration][4].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through