I have to embed a contact form inside of a block, and I'm trying to use the entity.form_builder service, But I'm not sure which entity I should give it, I found Message and ContactForm entity, so what I'm thinking is to create a contact form entity and give it to the entityFormBuilder->getForm($contactFormEntity)
My current code is:
<?php
/**
* @file
* Contains Drupal\pid_contactform_block\Plugin\Block\TellUsYourStoryBlock
*/
namespace Drupal\pid_contactform_block\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class TellUsYourStoryBlock
* @package Drupal\pid_contactform_block\Plugin\Block
* @Block(
* id = "tell_us_your_story_block_id",
* admin_label = "Tell us your story block",
* module = "pid_contactform_block"
* )
*/
class TellUsYourStoryBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $entityFormBuilder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, $entityFormBuilder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityFormBuilder = $entityFormBuilder;
}
/**
* Builds and returns the renderable array for this block plugin.
*
* If a block should not be rendered because it has no content, then this
* method must also ensure to return no content: it must then only return an
* empty array, or an empty array with #cache set (with cacheability metadata
* indicating the circumstances for it being empty).
*
* @return array
* A renderable array representing the content of the block.
*
* @see \Drupal\block\BlockViewBuilder
*/
public function build()
{
return $this->entityFormBuilder->getForm();
}
/**
* Creates an instance of the plugin.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to pull out services used in the plugin.
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*
* @return static
* Returns an instance of this plugin.
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
{
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.form_builder')
);
}
}
My question is basically:
- Am i doing it the right way?
- How do I initialize the ContactForm entity correctly, if I am right, should I just import it or should I require it through a service or something?
- If I'm not right, do you have any idea how I should do it then?