Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Sometimes it is desirable to trigger a form element state on the existence of Javascript, rather than on the state of another form element. For instance, I may have a form element that is hidden by default, but displays if Javascript is enabled on a page. Currently this is only possible through custom Javascript, or kludgy workarounds.
Proposed resolution
Allow #states to accept TRUE as a valid condition, as shown in this example:
$form['options']['published_date_override_container'] = array(
'#type' => 'container',
'#attributes' => array(
// Hide this container by default.
'class' => array('element-hidden'),
),
'#states' => array(
// Show this container if Javascript is enabled.
'visible' => TRUE,
),
);
Remaining tasks
Implement proposed resolution.
User interface changes
None.
API changes
TRUE will be a valid condition for the #states property on form elements.
Comments
Comment #1
tstoecklerI'm not going to directly mark this duplicate so as not to put you off, but I'm pretty sure this is a duplicate of #335185: Add js-show CSS class for hiding elements when JavaScript is disabled. Even though there has been no activity in over half a year over there it seems that one could actually go in in a pretty short amount of time if someone steps up to the plate (*nudge, nudge, ;-)*). I would love to RTBC that one, I've needed it quite a few times.
Comment #2
jstollerHmm. That issue would actually solve my immediate problem, but given the #states system can control more than just visibility, I think this issue may still have merit.
Comment #3
tstoecklerHmm... so you mean the feature request would be to mark a form element as required or disabled only when JS is available? I guess I have nothing against supporting that, although I totally don't see the use-case. I still think our efforts were better spent on the other issue...
Comment #4
jstollerI don't disagree that the other issue is probably a better place to spend my efforts right now, given its history. I'm just saying that we should keep this issue open as well. As it is, I have too many issues in the queue I'm already trying to work on, and I only pretend to be a developer, so in all likelihood I'm not getting to either of these soon anyway. :-(