Render fields in field groups
This commit is contained in:
parent
55e999af27
commit
c4d3f174d5
|
@ -0,0 +1,8 @@
|
|||
# Schema for form display third party settings.
|
||||
core.entity_form_display.*.*.*.third_party.farm_ui_theme:
|
||||
type: mapping
|
||||
label: 'Farm Theme Settings'
|
||||
mapping:
|
||||
use_field_group:
|
||||
type: boolean
|
||||
label: 'Use field group'
|
|
@ -86,8 +86,8 @@ function farm_ui_theme_farm_ui_theme_field_group_items(string $entity_type, stri
|
|||
|
||||
// Define base fields for asset, log, and plans on behalf of core modules.
|
||||
$fields = [
|
||||
'name' => 'entity',
|
||||
'status' => 'entity',
|
||||
'name' => 'default',
|
||||
'status' => 'meta',
|
||||
'flag' => 'meta',
|
||||
'file' => 'file',
|
||||
'image' => 'file',
|
||||
|
@ -103,11 +103,12 @@ function farm_ui_theme_farm_ui_theme_field_group_items(string $entity_type, stri
|
|||
break;
|
||||
|
||||
case 'log':
|
||||
$fields['timestamp'] = 'entity';
|
||||
$fields['timestamp'] = 'default';
|
||||
$fields['category'] = 'meta';
|
||||
$fields['owner'] = 'meta';
|
||||
$fields['geometry'] = 'location';
|
||||
$fields['location'] = 'location';
|
||||
$fields['is_movement'] = 'location';
|
||||
break;
|
||||
|
||||
case 'plan':
|
||||
|
|
|
@ -18,6 +18,138 @@ class GinContentFormBase extends ContentEntityForm implements RenderCallbackInte
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function that returns an array of tab definitions to add.
|
||||
*
|
||||
* @return array
|
||||
* Array of tab definitions keyed by tab id. Each definition should
|
||||
* provide a location, title and weight.
|
||||
*/
|
||||
protected function getFieldGroups() {
|
||||
return [
|
||||
'default' => [
|
||||
'location' => 'main',
|
||||
'title' => 'Default',
|
||||
'weight' => -50,
|
||||
],
|
||||
'meta' => [
|
||||
'location' => 'sidebar',
|
||||
'title' => $this->t('Meta'),
|
||||
'weight' => 0,
|
||||
],
|
||||
'location' => [
|
||||
'location' => 'main',
|
||||
'title' => $this->t('Location'),
|
||||
'weight' => 50,
|
||||
],
|
||||
'file' => [
|
||||
'location' => 'main',
|
||||
'title' => $this->t('Files'),
|
||||
'weight' => 150,
|
||||
],
|
||||
'revision' => [
|
||||
'location' => 'sidebar',
|
||||
'title' => $this->t('Revision information'),
|
||||
'weight' => 200,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function form(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::form($form, $form_state);
|
||||
|
||||
// Add process callback to alter form after GinContentFormHelper.
|
||||
$form['#process'][] = '::processContentForm';
|
||||
|
||||
// Only alter the form display if farm_ui_theme.use_field_group is TRUE
|
||||
// or if the form display is new and not saved.
|
||||
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
|
||||
$form_display = $form_state->get('form_display');
|
||||
if (!$form_display || !$form_display->getThirdPartySetting('farm_ui_theme', 'use_field_group', $form_display->isNew())) {
|
||||
return $form;
|
||||
}
|
||||
|
||||
// Add field groups.
|
||||
$field_groups = $this->getFieldGroups();
|
||||
if (!empty($field_groups)) {
|
||||
|
||||
// Disable HTML5 validation on the form element since it does not work
|
||||
// with vertical tabs.
|
||||
$form['#attributes']['novalidate'] = 'novalidate';
|
||||
|
||||
// Vary field group titles based on entity and bundle.
|
||||
if (isset($field_groups['default'])) {
|
||||
$field_groups['default']['title'] = $this->entity->type->entity->label();
|
||||
}
|
||||
|
||||
// Create parent for all tabs.
|
||||
$form['tabs'] = [
|
||||
'#type' => 'vertical_tabs',
|
||||
'#default_tab' => 'edit-setup',
|
||||
];
|
||||
|
||||
// Create tabs.
|
||||
foreach ($field_groups as $tab_id => $tab_info) {
|
||||
$tab_id = "{$tab_id}_field_group";
|
||||
$tab_group = $tab_info['location'] == 'sidebar' ? 'advanced' : 'tabs';
|
||||
$form[$tab_id] = [
|
||||
'#type' => 'details',
|
||||
'#title' => $tab_info['title'],
|
||||
'#group' => $tab_group,
|
||||
'#optional' => TRUE,
|
||||
'#weight' => $tab_info['weight'],
|
||||
'#open' => TRUE,
|
||||
];
|
||||
}
|
||||
|
||||
// Set field group for each display component.
|
||||
foreach ($form_display->getComponents() as $field_id => $options) {
|
||||
if (isset($form[$field_id]) && $render = $form_display->getRenderer($field_id)) {
|
||||
$tab_id = $render->getThirdPartySetting('farm_ui_theme', 'field_group', 'default');
|
||||
$form[$field_id]['#group'] = "{$tab_id}_field_group";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process function to update form after GinContentFormHelper.
|
||||
*
|
||||
* @param array $form
|
||||
* The form array to alter.
|
||||
*
|
||||
* @return array
|
||||
* The form array.
|
||||
*
|
||||
* @see \Drupal\gin\GinContentFormHelper
|
||||
*/
|
||||
public function processContentForm(array $form): array {
|
||||
|
||||
// Disable the default meta group provided by Gin.
|
||||
unset($form['meta']);
|
||||
|
||||
// Bail if the status field is not included.
|
||||
if (!isset($form['status'])) {
|
||||
return $form;
|
||||
}
|
||||
|
||||
// Assign correct status group after GinContentFormHelper.
|
||||
if (isset($form['meta_field_group'])) {
|
||||
$form['status']['#group'] = 'meta_field_group';
|
||||
}
|
||||
// Else unset the status group that is set by GinContentFormHelper.
|
||||
else {
|
||||
unset($form['status']['#group']);
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue