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