0

I'm trying to load a form inside a block plugin. However when I try to load it I get an error

The form argument information_request is not a valid form

'information_request' is the machine name of the form I'm attempting to load. This is an EForm I created, not a built in core form such as the user login form, or search form.

Here is the method I'm using that fails

$fb = \Drupal::formBuilder();
$inforequest = $fb->getForm('information_request');
$render['form'] = $inforequest;

return $render;

I've seen other examples for loading core forms like so

$form = \Drupal::formBuilder()->getForm('Drupal\search\Form\SearchBlockForm');

and I do see that a full address is being given in this example instead of just the machine id, but I don't know what address you could give for a custom eform. I've tried the following with no luck.

$inforequest = $fb->getForm('\Drupal\eform\information_request');
$inforequest = $fb->getForm('\Drupal\eform\Form\information_request');

How do you load an EForm programmatically to render inside a block plugin?

UPDATE: I made some more attempts to try to render it in another way. I attempted to load all entities like so

$content_entity_types = [];

foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type_definition) {
    if ($entity_type_definition->getGroup() == 'content') {
        $content_entity_types[$entity_type_id] = $entity_type_definition;
    }
}

$entities = array();
foreach ($content_entity_types as $type_key => $entity_type) {
    // Entities that do not declare a form class.
    // Exclude Comment entities as they have to be attached to another entity.

    // Get all bundles for current entity type.
    $entity_type_bundles = \Drupal::entityManager()->getBundleInfo($type_key);
    foreach ($entity_type_bundles as $bundle_key => $bundle_info) {
        // Personal contact form requires a user recipient to be specified.
        $entities[(string) $entity_type->getLabel()][$type_key.'.'.$bundle_key] = $bundle_info['label'];
    }
}

die('<pre>'.print_r($entities).'</pre>');

Doing so did actually show that $entities did contain the form I was looking for

Array
(
    [Custom block] => Array
        (
            [block_content.basic] => Basic block
        )

    [EForm Submission] => Array
        (
            [eform_submission.donate] => Donate
            [eform_submission.information_request] => Information Request
        )

    [File] => Array
        (
            [file.file] => Drupal\Core\StringTranslation\TranslatableMarkup Object
                (
                    [string:protected] => File
                    [translatedMarkup:protected] => 
                    [options:protected] => Array
                        (
                        )

                    [stringTranslation:protected] => 
                    [arguments:protected] => Array
                        (
                        )

                )

        )

    [Content] => Array
        (
            [node.book] => Book page
            [node.landing_page] => Landing Page
            [node.news] => News
            [node.newsletter] => Newsletter
            [node.page] => Basic page
        )

    [Shortcut link] => Array
        (
            [shortcut.default] => Default
        )

    [User] => Array
        (
            [user.user] => Drupal\Core\StringTranslation\TranslatableMarkup Object
                (
                    [string:protected] => User
                    [translatedMarkup:protected] => 
                    [options:protected] => Array
                        (
                        )

                    [stringTranslation:protected] => 
                    [arguments:protected] => Array
                        (
                        )

                )

        )

    [Custom menu link] => Array
        (
            [menu_link_content.menu_link_content] => Drupal\Core\StringTranslation\TranslatableMarkup Object
                (
                    [string:protected] => Custom menu link
                    [translatedMarkup:protected] => 
                    [options:protected] => Array
                        (
                        )

                    [stringTranslation:protected] => 
                    [arguments:protected] => Array
                        (
                        )

                )

        )

)

I see my form in $entities['EForm Submission']['eform_submission.information_request']; But I'm still not sure how to access or render it.

0

I did the exact same thing the other day. If you're having trouble setting up your classes, you can use Drupal console to generate a new block plugin. It can make it easier. Basically, the rendering happens inside the build method of your class. This method is inherited from BlockBase (which is a core Drupal class).

The process is to load the entity in one step and then use the form builder service to build the entity form of your entity. Then all you have to do is return the $form variable. It takes care of the rending for you.

Here's the code I used.

<?php

namespace Drupal\application_form\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a 'CallBackBlock' block.
 *
 * @Block(
 *  id = "call_back_block",
 *  admin_label = @Translation("Call back block"),
 * )
 */
class CallBackBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * Drupal\Core\Entity\EntityFormBuilder definition.
   *
   * @var \Drupal\Core\Entity\EntityFormBuilderInterface
   */
  protected $entity_form_builder;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entity_type_manager;

  /**
   * Construct.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param string $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   */
  public function __construct(
        array $configuration,
        $plugin_id,
        $plugin_definition,
        EntityFormBuilderInterface $entity_form_builder,
        EntityTypeManagerInterface $entityTypeManager
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entity_form_builder = $entity_form_builder;
    $this->entity_type_manager = $entityTypeManager;
  }
  /**
   * {@inheritdoc}
   */
  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'),
      $container->get('entity_type.manager')
    );
  }
  /**
   * {@inheritdoc}
   */
  public function build() {

    // Get the Call backs entity form and render it.
    $call_back_entity = $this->entity_type_manager->getStorage('call_backs')->create();
    $form = $this->entity_form_builder->getForm($call_back_entity);
    return $form;
  }

}
|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.