Skip to content

Commit

Permalink
Merge pull request #1279 from UN-OCHA/berliner/HPC-9987
Browse files Browse the repository at this point in the history
HPC-9987: Change label logic for data point form elements
  • Loading branch information
berliner authored Dec 23, 2024
2 parents 5aa486c + bd2b607 commit 20c24da
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}
*/
Expand All @@ -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'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
14 changes: 14 additions & 0 deletions html/modules/custom/ghi_form_elements/src/Element/DataPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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',
];
Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit 20c24da

Please sign in to comment.