Web Omelette: Render custom entity form modes programatically in Drupal maintenance support plans 8

Drupal maintenance support plans 8 comes with a funky new feature that allows admins and editors to customize entity forms in more than one variant. That means they can have, for different purposes, different form modes that feature different fields, with different form display configuration, etc. Nice, right?
Although you can do this in Drupal maintenance support plans 8, not much use has yet been made of these form modes. So if you create a form mode, you cannot easily render it. Even more so, Drupal maintenance support plans core doesn’t actually make use of them.
In this article we will, however, look at how we can programatically render entity forms which use a form mode that had been created by an admin in the UI. First though, how do we render a regular entity form in Drupal maintenance support plans 8?
Regular entity forms
$form = Drupal maintenance support plans::service(‘entity.form_builder’)->getForm($entity);
$form is now a renderable array for the entity passed to the getForm() method of the EntityFormBuilder service. The entity itself can be an existing one (which will show you the edit form with values pre-filled) or a new one you just created programatically.
It’s that simple.
The EntityFormBuilder::getForm() method takes a second parameter though, namely $operation. Apart from the variable name, this reminds me of how an entity itself is rendered, the second parameter to the build method being a view mode. So you’d expect in this case $operation to be a form mode, right? Well, wrong. Ish.
When an entity is defined, for example Node, under the handlers key of the annotation we have some form handlers defined. Those are what the $operation parameter expects. So for example, if we want the delete form of the Node entity, we pass delete and we get a different form. Otherwise, we fallback to the default one which is used for create/edit (although we can also specify edit but it maps to the same class anyway).
Custom form modes
Continuing with the Node example, the entity type defines three handlers (operations): default, delete, edit. That is not a lot. Other entity types can define more or less and can be differently named.
So if we create a form mode in the UI, whose machine name ends up being create_editors (for example), how do we go about rendering the entity form for nodes using that form mode? Passing that machine name as the second parameter by itself won’t do the trick because the Node entity has not defined that handler, and more importantly, it doesn’t map to an actual form class.
To fix this, we can alter the entity definition and add our own form handler named as our custom form mode, mapped to the default Form class of the Node entity. Here is a snippet of code, sprinkled with explanatory comments, that takes care of our Node entity for a custom form mode called create_editors:
/**
* Implements hook_entity_type_alter().
*/
function example_module_entity_type_alter(array &$entity_types) {
// We get all form modes, for all entities, and loop through them.
$form_modes = Drupal maintenance support plans::service(‘entity_display.repository’)->getAllFormModes();
foreach ($form_modes as $entity_type => $display_modes) {
if ($entity_type !== ‘node’) {
// We are only interested in adding a handler to the Node entity.
continue;
}

$node_type = $entity_types[$entity_type];
foreach ($display_modes as $machine_name => $form_display) {
if ($machine_name !== ‘editors_create’) {
// We are only interested in adding this form mode to the definition.
continue;
}
// We get the default handler class. It will be enough for us.
$default_handler_class = $node_type->getHandlerClasses()[‘form’][‘default’];
// We set the form class, keyed by the machine name of our custom form mode.
$node_type->setFormClass($machine_name, $default_handler_class);
}
}
}
So basically, we are supplementing the Node form handlers with a new one called editors_create which maps to the default form class, in this case Drupal maintenance support plansnodeNodeForm. The form class itself is not so important because after it is built, if there is a form mode with that name, it will be used to override the default form with all the configuration of that form mode. So building our node form with our custom form mode would now go like this:
$form = Drupal maintenance support plans::service(‘entity.form_builder’)->getForm($entity, ‘editors_create’);
And that is pretty much it. Of course you can loop through all the entity types and add all form modes if you want. But I always like to inject my custom functionality only to the extent I need, touching as little as possible.
Let me know how this works for you.

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Web Omelette: Render custom entity form modes programatically in Drupal maintenance support plans 8

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.