-2

I have a custom form and I want to insert it into a views header. What is the Drupal 8 way to do it?

5
  • 3
    Create a custom area plugin. – No Sssweat Jan 26 at 13:34
  • @NoSssweat Great, thank you! – Елин Й. Jan 26 at 15:16
  • To the down and close voter: What is so unclear with my one-liner question? It is short and to the point! I think this question may help others to figure out how to extend views like this on Drupal 8. If you still think that question is not suitable for DA, I can delete it and save you the hassle of voting to close or down vote. I don't understand and don't like how Drupal community is getting unfriendlier. – Елин Й. Jan 27 at 11:37
  • 1
    @ЕлинЙ. Please don't fall into that trap - voting is not (and will never be) synonymous with friendliness. People vote based on the quality, research effort displayed, and perceived usefulness to the site. As there's no research effort shown here, the downvotes seem valid (within the context of this site I mean, I haven't personally voted here) – Clive Jan 27 at 13:27
  • @Clive I didn't mean that it is unfriendly to down vote and personally I do it as well if I see some questions or answers that are not useful or qualitative enough for DA. I meant doing it without a constructive comment. You are right, the question maybe too short to show any research effort, but actually I did and found nothing nearly related. Because my research was apparently too specific about inserting forms into views. I wouldn't come to the idea that I should look for creating custom area plugins. I'll also add this as answer with an implementation example, if the question survives. – Елин Й. Jan 28 at 7:51
1

Thanks to @NoSssweat pointing me to views area plugins, here's how you insert a custom form into a views header (or any other views area).

1. Create the form

my_module/src/Form/MyModuleCustomForm.php

namespace Drupal\my_module\Form;

use Drupal\Core\Form\FormBase;

class MyModuleCustomForm extends FormBase {
  // Skipping the class content here
  // @see https://api.drupal.org/api/drupal/core!core.api.php/group/form_api/
}

2. Create the views area plugin

my_module/src/Plugin/views/area/MyModuleCustomArea.php

namespace Drupal\my_module\Plugin\views\area;

use Drupal\Core\Form\FormBuilderInterface;
use Drupal\views\Plugin\views\area\AreaPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\my_module\Form\MyModuleCustomForm;

/**
 * My module's custom form area handler.
 *
 * @ingroup views_area_handlers
 *
 * @ViewsArea("my_module_custom_area")
 */
class MyModuleCustomArea extends AreaPluginBase {
  /**
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $formBuilder) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->formBuilder = $formBuilder;
  }

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container->get('form_builder'));
  }

  /**
   * @inheritDoc
   */
  public function render($empty = FALSE) {
    return $this->formBuilder->getForm(MyModuleCustomForm::class);
  }
}

Here it is important to have the @ViewsArea() annotation over the class name, and we'll describe the area id (my_module_custom_area in our example) in that annotation to Views in the next step.

3. Describe the views area plugin to Views.

my_module.views.inc

/**
 * Implements hook_views_data().
 */
function my_module_views_data() {
  $data['views']['my_module_custom_area'] = [
    'title' => 'My module custom form',
    'help' => 'Provides a custom form for my module.',
    'area' => [
      'id' => 'my_module_custom_area',
    ],
  ];

  return $data;
}

4. Clear the caches

And done.

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.