Option 1 (recommended): custom entity reference selection handler. Further options down below.
After implementing that plugin, you need to activate it on the entity form display for the field you want it.
Option 1 (recommended): custom entity reference selection handler. Further options down below.
<?php
namespace Drupal\my_module\Plugin\EntityReferenceSelection;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\UserSelction;
use Drupal\Core\StringTranslation\TranslatableMarkup;
#[EntityReferenceSelection(
id: "default:active_user",
label: new TranslatableMarkup("User selection (only active users)"),
entity_types: ["user"],
group: "default",
weight: 1
)]
class ActiveUserSelection extends UserSelection {
/**
* {@inheritdoc}
*/
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityQuery($match, $match_operator);
$query->condition('status', 1);
return $query;
}
}After implementing that plugin, you need to activate it on the entity form display for the field you want it.
You can add headlines to a webform using the code below. To get to the source section, navigate to the form you want to alter, click on build and then on the source tab like in the screenshot.
This is just a short example on how to add a headline.
personal_data:
'#type': html_tag
'#tag': h3
'#value': 'Personal data'It will look something like this:
If this does not work...
$form['nodes'] = [
'#title' => $this->t('Select articles'),
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#tags' => TRUE,
'#selection_handler' => 'default',
'#selection_settings' => ['target_bundles' => ['article']],
'#default_value' => $default_nodes,
];...try adding an increased maxlength value:
$form['nodes'] = [
'#title' => $this->t('Select articles'),
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#maxlength' => 1024,
'#tags' => TRUE,
'#selection_handler' => 'default',
'#selection_settings' => ['target_bundles' => ['article']],
'#default_value' => $default_nodes,
];my_module.edit:
options:
# Enable the admin theme for this route.
_admin_route: TRUE<?php
namespace Drupal\scp_base\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
// Make sure profile pages are not admin pages, in order to load the right
// translation.
if ($route = $collection->get('profile.user_page.single')) {
$route->setOption('_admin_route', FALSE);
}
}
}
You want to use the Gin edit form for all content entities, including custom entities.
The Gin Everywhere module was developed to solve this specific issue.