6

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?
10

Try Contact block module, which provides the contact form in a block.

From module documentation,

The Contact Block module provides contact forms in a block. It uses the contact forms that can be created using the Drupal 8 core Contact module.

5

Try https://www.drupal.org/project/entityform_block project, which allows you to create add form blocks for any entity type.

  • Berdir, I really think some contact-form block functionality should be in core... An option to place contact-forms as blocks... – JohnDoea Jul 25 '16 at 11:07
3

Create a new block type with an ER field that points to contact forms, enable contact_storage, and create a block, no coding required

  • Oh, i thouth i had it solved but when i made the block, added a field i had to reference the contactform, but in my fields i can only choose between general, number, text and reference . So i picked reference and "others", it gives me a list of content i can reference comment, contact message, content, custom block and a few others that doesn't make sense. Then the other options is beneath "configuration", and that one has contact form, but that field will link to my forms configuration page and not the real contact form. How do i make contact form appear? – DenLilleMand Apr 6 '16 at 10:07
  • You need the contributed contact_storage module, and to use the 'rendered entity' formatter – larowlan Apr 7 '16 at 10:25
3

There is also a module FormBlock, which looks more general.

Enables the presentation of user registration, site wide contact, or node creation forms in blocks. This is particularly useful for including forms on panels.

I found an issue, to merge the FormBlock and the ContactBlock module together.

3

The simplest way is to use contact block module that is made specifically for this need. It will provide blocks for each of your contact form.

1

Since this applies to a few of the answers I am posting a separate response, hope that is okay...

For both contact_block and formblock I have tested both a custom theme, and then (to be sure) Bartik (8.6.1 for 8.6.x site on pantheon, if it matters). For both, the module "fails" to render the block. However, it does create "space" as if it was rendered. That is, if you put it in sidebar-first and that is the only thing for sidebar-first in the Block-Layout then there is a Fat Margin As-If it was rendering the sidebar with the Contact Form.

On a hunch, I uninstalled BigPipe, and presto! it works.

(Knowing that the order of module implementation sometimes matters, I did try re-installing BigPipe and the non-rendering behavior returned.)

I had a hunch to post as an Issue, but I am not certain how BigPipe fits in with all of this so I didn't. Maybe someone that knows more about BigPipe and why it would have this effect can respond to this post, and perhaps submit it as an issue.

1

Simple solution of your problem:

public function build() {
  $message = \Drupal\contact\Entity\Message::create(['contact_form' => 'feedback']);

  return \Drupal::service('entity.form_builder')->getForm($message);
}

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.