0

I'm attempting to create a block plugin that displays a form that allows users to enter information and create a node with that information. I understand I can easily create a form plugin that accomplishes this task but I'd like this to be inside a block plugin so that I can place it on existing pages instead of it being on it's own dedicated page. I'm building the form like so

<?php

/**
 * @file
 * Contains \Drupal\my_plugin\Plugin\Block\EquipmentEntry.
 */

namespace Drupal\my_plugin\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * @Block(
 * id = "equipmententry",
 * admin_label = @Translation("Equipment Entry"),
 *  )
 */

class EquipmentEntry extends BlockBase {
    public function blockForm($form, FormStateInterface $form_state) {
        return $form;
    }
    public function blockSubmit($form, FormStateInterface $form_state) {

    }
    public function build(){
                $content = [
            'container'    => [
                '#type'       => 'container',
                '#attributes' => [
                    'class'      => ['equipment-entry'],
                ],
                'form'         => [
                    '#type'       => 'form',
                    '#attributes' => [
                        'name'       => 'equipment-entry',
                        'id'         => 'test',
                    ],
                    'title'   => [
                        '#type'  => 'html_tag',
                        '#tag'   => 'h1',
                        '#value' => $title,
                    ],
                    'contact'      => [
                        '#type'       => 'textfield',
                        '#title'      => t('Contact Person'),
                        '#attributes' => [
                            'name'       => 'contact',
                        ],
                    ],
                    'Telephone'    => [
                        '#type'       => 'textfield',
                        '#title'      => t('Telephone'),
                        '#attributes' => [
                            'name'       => 'phone',
                        ],
                    ],
                ],
             ];
          return $content;
    }
}

However I'm not sure how to perform any action upon submitting the form. I understand there's a blockSubmit() function but I believe that's only when you save a block.

I tried intercepting the form with a form_alter hook like so

function my_plugin_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
        die($form_id);
}

but the function never fired. Is there a way to programmatically create a node when someone submits a form that was created to be displayed inside a block plugin? Or is there a way to create a form plugin and embed it inside a block?

  • It's already embedded in a block. I'm trying to figure out how to decide what happens when I click the submit button for users that are visiting that page. – Matt Dec 19 '16 at 21:31
2

You are right, the blockSubmit() method is used when you configure the block. The config form is in blockForm(), not in build().

If you put form elements in build() they get rendered like any other render element, but they are not part of the form api and can't be submitted.

If you want to build a form, use a form class. Then you can put the form class in build() by using the formBuilder():

public function build() {

  $content = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\MyForm');

  return $content;
}

Better solution:

Instead of building your own form use the entity form for nodes, which you can place in a block with this module: https://www.drupal.org/project/entityform_block

| improve this answer | |
  • 1
    You beat me by 22 seconds :) – Berdir Dec 19 '16 at 22:16
1

That is not a form, that's just a render array that's made to look like a form. But it's just output, not actually doing any form processing.

See for \Drupal\search\Plugin\Block\SearchBlock for an example how a block that displays a form needs to look like. You need a second form class that you call from the block.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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