Allow modules to add entity form field groups.

This commit is contained in:
Michael Stenta 2024-01-15 20:06:31 -05:00
parent bacf11f295
commit 067ea5fa05
2 changed files with 40 additions and 4 deletions

View File

@ -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.
*

View File

@ -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)) {