From abd6ee96467c37381bb22f87bd1ab2c6ebe78536 Mon Sep 17 00:00:00 2001 From: Paul Weidner Date: Thu, 7 Sep 2023 16:16:15 -0700 Subject: [PATCH] Add hook_farm_ui_theme_field_group_items to collect field group mappings --- modules/core/ui/theme/farm_ui_theme.api.php | 21 +++++++++++++++++ modules/core/ui/theme/farm_ui_theme.module | 26 +++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/modules/core/ui/theme/farm_ui_theme.api.php b/modules/core/ui/theme/farm_ui_theme.api.php index f94eddd13..c47ee4f9b 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 @@ * @{ */ +/** + * Specify the field groups to place entity fields in. + * + * @param string $entity_type + * The entity type. + * @param string $bundle + * The bundle. + * + * @return string[] + * An array keyed by field ID mapping to field group. + */ +function hook_farm_ui_theme_field_group_items(string $entity_type, string $bundle) { + if ($entity_type == 'asset' && $bundle == 'animal') { + return [ + 'nickname' => 'bundle', + 'sex' => 'bundle', + ]; + } + return []; +} + /** * Specify the regions that asset, log, and plan content items should be in. * diff --git a/modules/core/ui/theme/farm_ui_theme.module b/modules/core/ui/theme/farm_ui_theme.module index bd47c6b1b..2545ff450 100644 --- a/modules/core/ui/theme/farm_ui_theme.module +++ b/modules/core/ui/theme/farm_ui_theme.module @@ -6,6 +6,7 @@ */ use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Entity\Display\EntityFormDisplayInterface; use Drupal\Core\Form\FormStateInterface; /** @@ -52,6 +53,31 @@ function farm_ui_theme_theme_suggestions_menu_local_tasks(array $variables) { return ['menu_local_tasks__farm']; } +/** + * Implements hook_entity_form_display_alter(). + */ +function farm_ui_theme_entity_form_display_alter(EntityFormDisplayInterface $form_display, array $context) { + + // Only alter farm entity types. + $entity_types = ['asset', 'log', 'plan']; + if (!in_array($context['entity_type'], $entity_types)) { + return; + } + + // Ask modules for a list of field group items. + $field_map = \Drupal::moduleHandler()->invokeAll( + 'farm_ui_theme_field_group_items', + [$context['entity_type'], $context['bundle']], + ); + + // Apply the field group mapping if not already specified on the form display. + foreach ($field_map as $field_id => $field_group) { + if (($renderer = $form_display->getRenderer($field_id)) && !$renderer->getThirdPartySetting('farm_ui_theme', 'field_group', FALSE)) { + $renderer->setThirdPartySetting('farm_ui_theme', 'field_group', $field_group); + } + } +} + /** * Implements hook_gin_content_form_routes(). */