protected function PullForm::buildSelectElement in Entity Share 8.3
Builds a required select element, disabled if only one option exists.
Parameters
\Drupal\Core\Form\FormStateInterface $form_state: The form state object.
string $field: The form field key.
Return value
array The Drupal form element array, or an empty array if field is invalid.
2 calls to PullForm::buildSelectElement()
- PullForm::buildChannelSelect in modules/
entity_share_client/ src/ Form/ PullForm.php - Helper function to generate channel select.
- PullForm::buildForm in modules/
entity_share_client/ src/ Form/ PullForm.php - Form constructor.
File
- modules/
entity_share_client/ src/ Form/ PullForm.php, line 371
Class
- PullForm
- Form controller to pull entities.
Namespace
Drupal\entity_share_client\FormCode
protected function buildSelectElement(FormStateInterface $form_state, string $field) {
// Get all available options for this field.
$options = $this
->getSelectOptions($field);
// Sanity check for a valid $field parameter.
if (!$options) {
return [];
}
$disabled = FALSE;
$default_value = $this->query
->get($field);
// If only one option, pre-select it and disable the select.
if (count($options) == 1) {
$disabled = TRUE;
$default_value = key($options);
$form_state
->setValue($field, $default_value);
}
return [
'#type' => 'select',
'#options' => $options,
'#default_value' => $default_value,
'#empty_value' => '',
'#required' => TRUE,
'#disabled' => $disabled,
];
}