diff --git a/js/modal_forms_comment.js b/js/modal_forms_comment.js new file mode 100644 index 0000000..a0d0d30 --- /dev/null +++ b/js/modal_forms_comment.js @@ -0,0 +1,11 @@ +(function ($) { + +Drupal.behaviors.initModalFormsComment = { + attach: function (context, settings) { + $("a[href*='/comment/reply'], a[href*='?q=comment/reply']", context).once('init-modal-forms-comment', function () { + this.href = this.href.replace(/comment\/reply/,'modal_forms/nojs/comment/reply'); + }).addClass('ctools-use-modal ctools-modal-modal-popup-medium'); + } +}; + +})(jQuery); diff --git a/modal_forms.admin.inc b/modal_forms.admin.inc index 0b5e9d7..60c99b2 100644 --- a/modal_forms.admin.inc +++ b/modal_forms.admin.inc @@ -65,6 +65,19 @@ function modal_forms_admin_settings() { '#description' => t('Automatically activate Modal forms for links to contact.'), ); + if (module_exists('comment')) { + $form['modal_forms_comment_settings'] = array( + '#type' => 'fieldset', + '#title' => t('Comment links settings') + ); + $form['modal_forms_comment_settings']['modal_forms_comment'] = array( + '#type' => 'checkbox', + '#title' => t('Enable for comment links'), + '#default_value' => variable_get('modal_forms_comment', 0), + '#description' => t('Automatically activate Modal forms for links to comment.'), + ); + } + $form['modal_forms_advanced_settings'] = array( '#type' => 'fieldset', '#title' => t('Advanced settings'), diff --git a/modal_forms.module b/modal_forms.module index 3afdc01..ea805b0 100644 --- a/modal_forms.module +++ b/modal_forms.module @@ -59,6 +59,14 @@ function modal_forms_menu() { 'file' => 'modal_forms.pages.inc', 'type' => MENU_CALLBACK, ); + $items['modal_forms/%ctools_js/comment/reply/%node'] = array( + 'title' => 'Comment', + 'page callback' => 'modal_forms_comment_reply', + 'page arguments' => array(1, 4), + 'access arguments' => array('post comments'), + 'file' => 'modal_forms.pages.inc', + 'type' => MENU_CALLBACK, + ); return $items; } @@ -109,6 +117,22 @@ function modal_forms_form_alter(&$form, &$form_state, $form_id) { } /** + * Implements hook_form_BASE_FROM_ID_alter(). + */ +function modal_forms_form_comment_form_alter(&$form, &$form_state, $form_id) { + if (variable_get('modal_forms_comment', 0)) { + $comment = $form_state['comment']; + + // If not replying to a comment, use our dedicated page callback for new + // comments on nodes. + if (empty($comment->cid) && empty($comment->pid)) { + // Change the action to call our function. + $form['#action'] = url('modal_forms/nojs/comment/reply/' . $comment->nid); + } + } +} + +/** * Check if modal_forms should be active for the current URL. * * @return @@ -220,5 +244,11 @@ function _modal_forms_doheader() { drupal_add_js($path . '/js/modal_forms_contact.js', array('weight' => -20)); } + if (user_access('post comments')) { + if (variable_get('modal_forms_comment', 0)) { + drupal_add_js($path . '/js/modal_forms_comment.js', array('weight' => -20)); + } + } + $already_added = TRUE; } diff --git a/modal_forms.pages.inc b/modal_forms.pages.inc index 964ffd7..e6a145f 100644 --- a/modal_forms.pages.inc +++ b/modal_forms.pages.inc @@ -133,3 +133,69 @@ function modal_forms_contact($js = NULL) { print ajax_render($output); exit; } + +/** + * A modal comment callback. + */ +function modal_forms_comment_reply($js = NULL, $node, $pid = NULL) { + $build = array(); + $comment = array( + 'pid' => $pid, + 'nid' => $node->nid, + ); + + if (!$js) { + $form = drupal_get_form("comment_node_{$node->type}_form", (object) $comment); + // Remove output bellow the comment form. + unset($form['comment_output_below']); + return $form; + } + + ctools_include('modal'); + ctools_include('ajax'); + + $form_state = array( + 'build_info' => array( + 'args' => array( + (object) $comment, + ), + ), + 'title' => t('Comment'), + 'ajax' => TRUE, + 're_render' => FALSE, + 'no_redirect' => TRUE, + ); + + // Should we show the reply box? + if ($node->comment != COMMENT_NODE_OPEN) { + drupal_set_message(t("This discussion is closed: you can't post new comments."), 'error'); + drupal_goto("node/$node->nid"); + } + else { + $form = drupal_build_form("comment_node_{$node->type}_form", $form_state); + // Remove output bellow the comment. + unset($form['comment_output_below']); + } + + if (!$form_state['executed'] || $form_state['rebuild']) { + $form = ctools_modal_form_render($form_state, $form); + } + else { + // We'll just overwrite the form output if it was successful. + $form = array(); + // @todo: Uncomment once http://drupal.org/node/1587916 is fixed. + //if (is_array($form_state['redirect'])) { + // list($path, $options) = $form_state['redirect']; + // $form[] = ctools_ajax_command_redirect($path, 0, $options); + //} + if (isset($_GET['destination'])) { + $form[] = ctools_ajax_command_redirect($_GET['destination']); + } + else { + $form[] = ctools_ajax_command_reload(); + } + } + + print ajax_render($form); + exit; +}