0

i'm working on my first custom module and really fumbling along :). thanks in advance for your advice!

goal i've added a checkbox to the comment form. if the user leaves it checked, then following comment insertion, i want to run some javascript on the following page.

possible approach? i've added the checkbox using hook_form_alter:

function wt_comments_form_comment_form_alter(&$form, $form_state, $form_id) {

//show the checkbox
  $form['wt_comments_share'] = array(
    '#type' => 'checkbox',
    '#title' => t('Post this comment on your Facebook wall too. Spread the word!'),
    '#default_value' => 1
  );
}

this works fine. however, i'm struggling to pass the variable from php to js. i read up on adding variables to Drupal.settings using drupal_add_js(), but i don't have a solid understanding of basic concepts. this post seems to add values to Drupal.settings using #attached, but i couldn't figure out how to add the value of a normal form field.

i tried to run drupal_add_js() within hook_comment_presave or hook_comment_insert, to no avail:

function wt_comments_comment_insert($comment) {
  dpm( get_defined_vars() );

  //nothing
  drupal_add_js( array ("wt_comments" => $comment), 'setting');

  // nothing  
  drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
}

So i added the variable to the global $_SESSION object:

function wt_comments_comment_presave($comment) {
  $_SESSION['wt_comments'] = $comment->wt_comments_share;  
}

then, within a hook that does run drupal_add_js, i add the new session variable to drupal settings as described here:

function wt_comments_page_alter(&$page) {

  drupal_add_js("jQuery(document).ready(function () { 

      if ( Drupal.settings.wt_comments.wt_comments == 1 ) {

        // dance around! jQuery('body').css('background-color','green');

      }

    });",
    'inline'
  );

  //reset session variable
  $_SESSION['wt_comments'] = '0';

}

note: i wonder if hook_page_alter is the best choice here...

is this the right approach? thanks again folks! -ryan

0

Adding the value of the new field wt_comments_share to $_SESSION['wt_comments'] is considered a good idea.

Because you're using session to pass values, you don't need to pass it to Drupal.settings unless if you want to add new JS file.

To add javascript code to node view you need to use hook_init or template_preprocess_node

function wt_comments_init() {
  // check if page is node and wt_comments is set
  if($node = menu_get_object() && isset($_SESSION['wt_comments'])) {
    // check users allowed to comment on this node
    if($node->comment) {
      // add javascript code
      drupal_add_js("jQuery(document).ready(function () { 
            if ( Drupal.settings.wt_comments.wt_comments == 1 ) {
              // dance around! jQuery('body').css('background-color','green');
            }
          });",
       'inline'
      ); 

      // unset session wt_comments
      unset($_SESSION['wt_comments']);
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.