Problem/Motivation
I think there is an issue when trying to use multiple AND clauses with 'visible' #states. I think it is always using OR..
Steps to reproduce
I have 2 sets of radio buttons. I want a text field to only be visible if both correct values are set - in this case where the currency is set to 'gbp' and the frequency is set to 'single'.
(to make it work, you will probably have to change the 'name' bit in the state code to match how it appears for you).
It seems what is happening is that the text field is appearing if either 'gbp' OR 'single' are selected, rather than AND.
I've been using the examples on this page (where I'm sure I've had this working fine when using simple forms, rather than as a composite webform.
https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields β
<?php
namespace Drupal\my_module\Element;
use Drupal\webform\Element\WebformCompositeBase;
use Drupal\Core\Form\FormStateInterface;
/**
*
*
* @FormElement("my_module")
*/
class my_module extends WebformCompositeBase {
/**
* {@inheritdoc}
*/
public function getInfo() {
return parent::getInfo();
}
/**
* {@inheritdoc}
*/
public static function getCompositeElements(array $element) {
$elements = [];
$elements['currency'] = [
'#type' => 'radios',
'#title' => t('Currency'),
'#options' => [],
'#weight' => 0,
];
$elements['frequency'] = [
'#type' => 'radios',
'#title' => t('Frequency'),
'#options' => [],
'#weight' => 0,
];
return $elements;
}
/**
* {@inheritdoc}
*/
public static function processWebformComposite(&$element, FormStateInterface $form_state, &$complete_form) {
parent::processWebformComposite($element, $form_state, $complete_form);
$currency = [
'gbp' => 'Β£',
'eur' => 'β¬',
];
$frequency = [
'single' => 'single',
'regular' => 'regular',
];
$element['currency']['#options'] = $currency;
$element['frequency']['#options'] = $frequency;
$element['textfield'] = [
'#type' => 'textfield',
'#value' => 'gbp & single',
'#states' => [
'visible' => [
[':input[name="donate[currency]"]' => ['value' => 'gbp']],
'and',
[':input[name="donate[frequency]"]' => ['value' => 'single']],
],
],
];
return $element;
}
}