I am looking for a way to hide/show some fields depending on what users select on another field. Users can select between four wheeler or two wheeler. If users select four wheeler from a drop-down, they see different set of fields than the ones show when two wheeler is selected. As there are many common fields I am looking to use the same content type for both cases.
What I have tried so far is using hook_form_alter()
or hook_form_FORM_ID_alter()
in the .theme file. Neither of them seems to work for me.
This is the code I tried. (Please do note I am using themename instead of the machine name of the theme; also the field names has been changed.)
use Drupal\Core\Form\FormStateInterface; // at top of themename.theme
function themename_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
$form['field_album_type_name']['#states'] = [
'visible' => [
'select[name="field_album_type"]' => ['value' => 'custom']
]
];
}