mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Use allowed values in ID tag widget to create a select list. If a saved value is not in list, use a text field.
This commit is contained in:
parent
d8cf2f5c29
commit
27c68df776
2 changed files with 20 additions and 3 deletions
|
@ -7,8 +7,11 @@
|
|||
|
||||
/**
|
||||
* Allowed values callback function for the ID tag type field.
|
||||
*
|
||||
* @return array
|
||||
* Returns an array of allowed values for use in form select options.
|
||||
*/
|
||||
function farm_id_tag_type_field_allowed_values() {
|
||||
function farm_id_tag_type_allowed_values() {
|
||||
/** @var \Drupal\farm_id_tag\Entity\FarmIDTagTypeInterface[] $types */
|
||||
$types = \Drupal::entityTypeManager()->getStorage('tag_type')->loadMultiple();
|
||||
$allowed_values = [];
|
||||
|
|
|
@ -32,12 +32,26 @@ class IdTagWidget extends WidgetBase {
|
|||
'#default_value' => isset($items[$delta]->id) ? $items[$delta]->id : NULL,
|
||||
];
|
||||
|
||||
// Load the saved tag type, if any.
|
||||
$tag_type = isset($items[$delta]->type) ? $items[$delta]->type : NULL;
|
||||
|
||||
// Load allowed tag types.
|
||||
$tag_types = farm_id_tag_type_allowed_values();
|
||||
|
||||
$element['type'] = [
|
||||
'#type' => 'textfield',
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Tag type'),
|
||||
'#default_value' => isset($items[$delta]->type) ? $items[$delta]->type : NULL,
|
||||
'#options' => [NULL => ''] + $tag_types,
|
||||
'#default_value' => $tag_type,
|
||||
];
|
||||
|
||||
// If the tag type is not in the list of allowed values, change to a
|
||||
// text field so that it is still editable.
|
||||
if (!empty($tag_type) && !array_key_exists($tag_type, $tag_types)) {
|
||||
$element['type']['#type'] = 'select';
|
||||
unset($element['type']['#options']);
|
||||
}
|
||||
|
||||
$element['location'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Tag location'),
|
||||
|
|
Loading…
Reference in a new issue