We're trying to figure out the approved way using the states api to detect when a form element is added to the form by an ajax callback.

We've tried all the different states that are mentioned here.

Our code was working with 7.13, but when we updated to 7.15 it stopped working.

Our code basically hides the comments form until the user votes on a poll. Once they voted, it hides the message, and shows the comment form.

Here is our code that worked for 7.13; (This works with the Ajax Poll module)

    
    // Provide an appropriate message about casting a vote when the voting form is visible
    $comment_form['message'] = array(
      '#type'          => 'fieldset',
      '#tree'          => FALSE,
      '#id'            => 'comment-message',
      '#attributes'    => array(),
      
      'content' => array(
        '#markup'  => t('To comment on this question, please first cast a vote for one of the options above.'),
      ),
      
      '#states'  => array(
        'invisible' => array(
          ".poll #edit-submit" => array('value' => 'Cancel your vote'),
        ),
        
        'visible' => array(
          ".poll #edit-vote" => array('value' => 'Vote'),
        ),
      ),
    );      
      
    // Provide the comment form when the results are visible.
    $comment_form['container'] = array(
      '#type'        => 'fieldset',
      '#tree'        => FALSE,
      '#id'          => 'comment-wrapper',
      '#attributes'  => array(),
        
      '#states'  => array(
        'invisible' => array(
          ".poll #edit-vote" => array('value' => 'Vote'),
        ),
          
        'visible' => array(
          ".poll #edit-submit" => array('value' => 'Cancel your vote'),
        ),
      ),
    );

Comments

guypaddock’s picture

Issue summary: View changes

Correct indentation.

nod_’s picture

tag

nod_’s picture

Issue summary: View changes

Typo correction.

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.