How to Programmatically Display Form in Custom Block in Drupal 8
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY vinod
8 months ago
Drupal-8
0
comments comment

Most often we may want to show our custom forms in blocks. Here are the simple steps to accomplish this.

In the below code we will create a form called “MymoduleExampleForm” and this form will be placed in a custom block called “MymoduleExampleBlock”.

Step 1 – Create a custom module say 'mymodule'. For details on how to create a custom module in Drupal 8 See here

Step 2 – Create a php file named MymoduleExampleForm.php in mymodule/src/Form and place the following code.


  <?php

    namespace Drupal\mymodule\Form;
    use Drupal\Core\Form\FormBase;
    use Drupal\Core\Form\FormStateInterface;
    /**
     * Class MymoduleExampleForm for demostration.
    */
    class MymoduleExampleForm extends FormBase {
      /**
       * {@inheritdoc}
      */
      public function getFormId() {
        return 'mymodule_example_form';
      }
      /**
       * {@inheritdoc}
      */
      public function buildForm(array $form, FormStateInterface $form_state) {
        $form['user_mail'] = [
          '#type' => 'email',
          '#title' => t('Email ID:'),
          '#required' => TRUE,
        ];
        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = [
         '#type' => 'submit',
         '#value' => $this->t('Subscribe'),
        ];
        return $form;
      }
      /**
       * {@inheritdoc}
      */
      public function validateForm(array &$form, FormStateInterface $form_state) {
        // Nothing.
      }
      /**
       * {@inheritdoc}
      */
      public function submitForm(array &$form, FormStateInterface $form_state) {
        drupal_set_message($this->t('@user_email ,Your email-id has been sent !', ['@user_email' => $form_state-  >getValue('user_mail')]));}
    }

 

Step 3 – Create a php file named MymoduleExampleBlock.php in mymodule/src/Plugin/Block and place the following code.


  <?php

    namespace Drupal\mymodule\Plugin\Block;

    use Drupal\Core\Block\BlockBase;

    /**
     * Provides a 'MymoduleExampleBlock' block.
     *
     * @Block(
     *   id = "mymodule_example_block",
     *   admin_label = @Translation("Example block"),
     *   category = @Translation("Custom example block")
     * )
    */
    class MymoduleExampleBlock extends BlockBase {

     /**
      * {@inheritdoc}
     */
     public function build() {

       $form = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\MymoduleExampleForm');

       return $form;
     }
   }

 


RELATED ARTICLE

more_horiz
close

on 21st May 2013 / by Anoop John
Drupal 6 will be maintained till the release of Drupal 8. Drupal 8 release is expected to happen towards the end of 2013. The current stable branch Drupal 7 will be maintained till the release of Drupal 9 (probably 2015). If you are running a production site on Drupal 6 then you will probably have to start looking at upgrading the site by the end of this year. Drupal Drupal 6 Drupal Upgrade Drupal 7 Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 26th December 2014 / by abhai.sasidharan
YAML is a data serialization language which is easily readable to both humans and computers. It is for this very reason that YAML is being used in Drupal 8. YAML stands for YAML Ain't Markup Language. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 29th December 2014 / by abhai.sasidharan
Drupal, up until now a PAC (Presentation Abstraction Control) architecture, has taken a step closer to MVC (Model View Controller) architecture. Although nowhere have they mentioned about MVC, the new Drupal 8 is strictly object oriented. Another feature in Drupal 8, that is different from the existing versions of Drupal is that it is not entirely Drupal. Drupal 8 uses many components in symfony like it's ClassLoader, YAML file structure, twig and so on. For a detailed description please visit the Drupal 8 home page. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
Leave a reply
Your email address will not be published. Required fields are marker *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.