From 067ea5fa057bd46c073d710dca410b1807f8c843 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Mon, 15 Jan 2024 20:06:31 -0500 Subject: [PATCH] Allow modules to add entity form field groups. --- modules/core/ui/theme/farm_ui_theme.api.php | 21 +++++++++++++++++ .../ui/theme/src/Form/GinContentFormBase.php | 23 +++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/modules/core/ui/theme/farm_ui_theme.api.php b/modules/core/ui/theme/farm_ui_theme.api.php index c47ee4f9b..ffc043a45 100644 --- a/modules/core/ui/theme/farm_ui_theme.api.php +++ b/modules/core/ui/theme/farm_ui_theme.api.php @@ -14,6 +14,27 @@ * @{ */ +/** + * Add entity form field groups. + * + * @param string $entity_type + * The entity type. + * @param string $bundle + * The bundle. + */ +function hook_farm_ui_theme_field_groups(string $entity_type, string $bundle) { + if ($entity_type == 'asset' && $bundle == 'animal') { + return [ + 'custom' => [ + 'location' => 'main', + 'title' => t('Custom'), + 'weight' => 50, + ], + ]; + } + return []; +} + /** * Specify the field groups to place entity fields in. * diff --git a/modules/core/ui/theme/src/Form/GinContentFormBase.php b/modules/core/ui/theme/src/Form/GinContentFormBase.php index 95d6c7643..4e6ce0815 100644 --- a/modules/core/ui/theme/src/Form/GinContentFormBase.php +++ b/modules/core/ui/theme/src/Form/GinContentFormBase.php @@ -7,6 +7,7 @@ use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element\RenderCallbackInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -23,6 +24,13 @@ class GinContentFormBase extends ContentEntityForm implements RenderCallbackInte */ protected $dateFormatter; + /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + /** * Constructs a ContentEntityForm object. * @@ -34,10 +42,13 @@ class GinContentFormBase extends ContentEntityForm implements RenderCallbackInte * The time service. * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter * The date formatter service. + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. */ - public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, DateFormatterInterface $date_formatter) { + public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler) { parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->dateFormatter = $date_formatter; + $this->moduleHandler = $module_handler; } /** @@ -49,6 +60,7 @@ class GinContentFormBase extends ContentEntityForm implements RenderCallbackInte $container->get('entity_type.bundle.info'), $container->get('datetime.time'), $container->get('date.formatter'), + $container->get('module_handler'), ); } @@ -67,7 +79,9 @@ class GinContentFormBase extends ContentEntityForm implements RenderCallbackInte * provide a location, title and weight. */ protected function getFieldGroups() { - return [ + $entity_type = $this->entity->getEntityTypeId(); + $bundle = $this->entity->bundle(); + $field_groups = [ 'default' => [ 'location' => 'main', 'title' => 'Default', @@ -93,7 +107,8 @@ class GinContentFormBase extends ContentEntityForm implements RenderCallbackInte 'title' => $this->t('Revision information'), 'weight' => 200, ], - ]; + ] + $this->moduleHandler->invokeAll('farm_ui_theme_field_groups', [$entity_type, $bundle]); + return $field_groups; } /** @@ -116,7 +131,7 @@ class GinContentFormBase extends ContentEntityForm implements RenderCallbackInte // Attach content form styles. $form['#attached']['library'][] = 'farm_ui_theme/content_form'; - // Add field groups. + // Add field groups, and allow modules to alter them. $field_groups = $this->getFieldGroups(); if (!empty($field_groups)) {