From bd2b607b44b825f369f0299671a61f183d6036ec Mon Sep 17 00:00:00 2001 From: berliner Date: Mon, 23 Dec 2024 16:20:31 +0100 Subject: [PATCH] HPC-9987: Change label logic for data point form elements --- .../AttachmentData.php | 34 +++++++++++++++++-- .../ConfigurationContainerItem/DataPoint.php | 15 ++++++++ .../ConfigurationContainerItemPluginBase.php | 8 +++-- .../src/Element/ConfigurationContainer.php | 2 +- .../src/Element/DataPoint.php | 14 ++++++++ 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/html/modules/custom/ghi_blocks/src/Plugin/ConfigurationContainerItem/AttachmentData.php b/html/modules/custom/ghi_blocks/src/Plugin/ConfigurationContainerItem/AttachmentData.php index b543aba74..f595367bf 100644 --- a/html/modules/custom/ghi_blocks/src/Plugin/ConfigurationContainerItem/AttachmentData.php +++ b/html/modules/custom/ghi_blocks/src/Plugin/ConfigurationContainerItem/AttachmentData.php @@ -134,8 +134,11 @@ public function buildForm($element, FormStateInterface $form_state) { ]; $element['label']['#access'] = !$attachment_select_mode; + if ($attachment) { - $element['label']['#weight'] = 4; + $element['label']['#access'] = FALSE; + $element['label']['#default_value'] = ''; + $element['label']['#value'] = ''; $element['data_point'] = [ '#type' => 'data_point', '#attachment' => $attachment, @@ -154,6 +157,33 @@ public function buildForm($element, FormStateInterface $form_state) { return $element; } + /** + * Get a default label. + * + * @return string|null + * A default label or NULL. + */ + public function getDefaultLabel() { + $attachment = $this->getAttachmentObject(); + $data_point_conf = $this->get('data_point'); + $data_point_index = $data_point_conf ? $data_point_conf['data_points'][0]['index'] : NULL; + if ($data_point_index === NULL) { + return NULL; + } + return $attachment->getPrototype()->getDefaultFieldLabel($data_point_index, $attachment->getPlanLanguage()); + } + + /** + * {@inheritdoc} + */ + public function getLabel() { + $data_point_conf = $this->get('data_point'); + if (array_key_exists('label', $data_point_conf) && !empty($data_point_conf['label'])) { + return trim($data_point_conf['label']); + } + return parent::getLabel(); + } + /** * {@inheritdoc} */ @@ -171,7 +201,7 @@ public function getRenderArray() { return NULL; } - $data_point_conf = $this->get(['data_point']); + $data_point_conf = $this->get('data_point'); $build = $attachment->formatValue($data_point_conf); $data_point_index = $data_point_conf['data_points'][0]['index']; diff --git a/html/modules/custom/ghi_blocks/src/Plugin/ConfigurationContainerItem/DataPoint.php b/html/modules/custom/ghi_blocks/src/Plugin/ConfigurationContainerItem/DataPoint.php index c5649094f..e17d8dfb4 100644 --- a/html/modules/custom/ghi_blocks/src/Plugin/ConfigurationContainerItem/DataPoint.php +++ b/html/modules/custom/ghi_blocks/src/Plugin/ConfigurationContainerItem/DataPoint.php @@ -45,6 +45,10 @@ public static function create(ContainerInterface $container, array $configuratio */ public function buildForm($element, FormStateInterface $form_state) { $element = parent::buildForm($element, $form_state); + $element['label']['#access'] = FALSE; + $element['label']['#default_value'] = ''; + $element['label']['#value'] = ''; + $attachment = $this->getContextValue('attachment'); $plan_object = $this->getContextValue('plan_object'); $configuration = $this->getPluginConfiguration(); @@ -90,6 +94,17 @@ public function getDefaultLabel() { return $attachment_prototype->getDefaultFieldLabel($data_point_index, $plan_object?->getPlanLanguage()); } + /** + * {@inheritdoc} + */ + public function getLabel() { + $data_point_conf = $this->get('data_point'); + if (array_key_exists('label', $data_point_conf) && !empty($data_point_conf['label'])) { + return trim($data_point_conf['label']); + } + return parent::getLabel(); + } + /** * {@inheritdoc} */ diff --git a/html/modules/custom/ghi_form_elements/src/ConfigurationContainerItemPluginBase.php b/html/modules/custom/ghi_form_elements/src/ConfigurationContainerItemPluginBase.php index 71b43f8be..8c472e84f 100644 --- a/html/modules/custom/ghi_form_elements/src/ConfigurationContainerItemPluginBase.php +++ b/html/modules/custom/ghi_form_elements/src/ConfigurationContainerItemPluginBase.php @@ -93,9 +93,11 @@ public function buildForm($element, FormStateInterface $form_state) { '#default_value' => $this->getSubmittedValue($element, $form_state, 'label'), ]; - if (method_exists($this, 'getDefaultLabel')) { - $element['label']['#description'] = $this->t('Leave empty to use a default label'); - $element['label']['#placeholder'] = $this->getDefaultLabel(); + if (method_exists($this, 'getDefaultLabel') && $label = $this->getDefaultLabel()) { + $element['label']['#description'] = $this->t('Leave empty to use the default label "%default_label".', [ + '%default_label' => $label, + ]); + $element['label']['#placeholder'] = $label; } return $element; diff --git a/html/modules/custom/ghi_form_elements/src/Element/ConfigurationContainer.php b/html/modules/custom/ghi_form_elements/src/Element/ConfigurationContainer.php index 3c15006bf..cf4e19edf 100644 --- a/html/modules/custom/ghi_form_elements/src/Element/ConfigurationContainer.php +++ b/html/modules/custom/ghi_form_elements/src/Element/ConfigurationContainer.php @@ -382,7 +382,7 @@ public static function elementSubmit(array &$element, FormStateInterface $form_s $id = self::get($element, $form_state, 'edit_item'); $index = self::getItemIndexById($items, $id); - $values = $form_state->getValue(array_merge($parents)); + $values = $form_state->getValue($parents); $values = $values['item_config'] ?? $values; if ($mode == 'add_item') { diff --git a/html/modules/custom/ghi_form_elements/src/Element/DataPoint.php b/html/modules/custom/ghi_form_elements/src/Element/DataPoint.php index e2bf3716d..6de0f3d8f 100644 --- a/html/modules/custom/ghi_form_elements/src/Element/DataPoint.php +++ b/html/modules/custom/ghi_form_elements/src/Element/DataPoint.php @@ -103,6 +103,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form */ public static function processDataPoint(array &$element, FormStateInterface $form_state) { $attachment = $element['#attachment']; + /** @var \Drupal\ghi_plans\Entity\Plan $plan_object */ $plan_object = $element['#plan_object'] ?? NULL; /** @var \Drupal\ghi_plans\ApiObjects\AttachmentPrototype\AttachmentPrototype $attachment_prototype */ $attachment_prototype = $attachment ? $attachment->prototype : $element['#attachment_prototype']; @@ -126,6 +127,7 @@ public static function processDataPoint(array &$element, FormStateInterface $for ], 1 => array_key_exists('data_points', $values) && array_key_exists(1, $values['data_points']) ? $values['data_points'][1] : NULL, ], + 'label' => !empty($values['label']) ? $values['label'] : '', 'formatting' => !empty($values['formatting']) ? $values['formatting'] : array_key_first(DataAttachment::getFormattingOptions()), 'widget' => !empty($values['widget']) ? $values['widget'] : 'none', ]; @@ -323,6 +325,18 @@ public static function processDataPoint(array &$element, FormStateInterface $for ]; } + $data_point_index = $defaults['data_points'][0]['index'] ?? NULL; + $default_label = $data_point_index !== NULL ? $attachment_prototype->getDefaultFieldLabel($data_point_index, $plan_object->getPlanLanguage()) : NULL; + $element['label'] = [ + '#type' => 'textfield', + '#title' => t('Label'), + '#default_value' => $defaults['label'], + '#description' => t('Leave empty to use the default label "%default_label".', [ + '%default_label' => $default_label, + ]), + '#placeholder' => $default_label, + ]; + $element['formatting'] = [ '#type' => 'select', '#title' => t('Formatting'),