When I'm using #states
to set the visibility of a form field I'm checking an input element for its value. Is there then any way to grab the selected value the moment it's selected?
In the working sample below I'm displaying some markup when a select list's value != '_none'
. It works good so far. But how do I get the markup to display the value I selected?
Background is: it's a node reference select list and I'm trying to maybe load some fields from the selected node as preview inside the markup. The moment I'm writing this I think it's a stupid idea. I'm in Ajax with #states
, right? And there might be no way to call node_load()
or anything the like, right?
/**
* Implements hook_form_alter().
*/
function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'MY_FORM') {
$form['my_container'] = [
'#type' => 'container',
'#states' => [
'visible' => [
':input[name="my_field_name[und]"]' => ['!value' => '_none'],
],
],
];
$form['my_container']['markup'] = [
'#type' => 'markup',
'#markup' => /* selected value here */ ,
];
}
}
#ajax