You are here

function disqus_form_alter in Disqus 7

Same name and namespace in other branches
  1. 6 disqus.module \disqus_form_alter()

Implementation of hook_form_alter().

File

./disqus.module, line 765
The Disqus Drupal module.

Code

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

  // Allow toggling the comments on or off per node from the node edit form.
  if (!empty($form['#node_edit_form'])) {
    $node = $form['#node'];

    // Only display the toggle Disqus comments setting if comments are available
    // for the given node type.
    $types = variable_get('disqus_nodetypes', array());
    if (isset($types[$node->type]) && !empty($types[$node->type])) {

      // Add a comment settings fieldset for users with "toggle disqus comments" permission
      // when Drupal core Comments module is disabled.
      if (!isset($form['comment_settings'])) {
        $form['comment_settings'] = array(
          '#type' => 'fieldset',
          '#access' => user_access('toggle disqus comments'),
          '#title' => t('Comment settings'),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#group' => 'additional_settings',
          '#weight' => 30,
          '#attached' => array(
            'js' => array(
              drupal_get_path('module', 'disqus') . '/js/disqus-node-form.js',
            ),
          ),
        );
      }
      else {
        if (isset($form['comment_settings']['comment'])) {

          // Ensure only core Comment administrators see Comment module settings
          $form['comment_settings']['comment']['#access'] = $form['comment_settings']['#access'];

          // But reveal parent comment settings fieldset if user has toggle permission
          $form['comment_settings']['#access'] = user_access('toggle disqus comments') || $form['comment_settings']['#access'];

          // Add the Javascript for tab text.
          $form['comment_settings']['#attached']['js'][] = drupal_get_path('module', 'disqus') . '/js/disqus-node-form.js';
        }
      }

      // Add the Disqus settings into the comment settings fieldset for users with toggle permission.
      $disqus_status = variable_get('disqus_nodetypes_default', _disqus_node_types_options());
      $disqus_default_status = !empty($disqus_status[$node->type]);
      $form['comment_settings']['disqus_status'] = array(
        '#type' => 'checkbox',
        '#title' => t('Disqus comments'),
        '#description' => t('Users can post comments using <a href="@disqus">Disqus</a>.', array(
          '@disqus' => 'http://disqus.com',
        )),
        '#default_value' => isset($node->disqus['status']) ? $node->disqus['status'] : $disqus_default_status,
        '#access' => user_access('toggle disqus comments'),
      );
    }
  }
}