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?