You are here

function disqus_element_post_render in Disqus 7

Post render function of the Disqus element to inject the Disqus JavaScript.

1 string reference to 'disqus_element_post_render'
disqus_element_info in ./disqus.module
Implements hook_element_info().

File

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

Code

function disqus_element_post_render($children, &$element) {

  // Construct the settings to be passed in for Disqus.
  $disqus = array(
    'domain' => $element['#disqus']['domain'],
    'url' => $element['#disqus']['url'],
    'title' => $element['#disqus']['title'],
    'identifier' => $element['#disqus']['identifier'],
  );
  if (isset($element['#disqus']['developer']) && $element['#disqus']['developer']) {
    $disqus['developer'] = 1;
  }

  // If the user is logged in, we can inject the username and email for Disqus.
  global $user;
  if (variable_get('disqus_inherit_login', TRUE) && $user->uid > 0) {
    $disqus['name'] = $user->name;
    $disqus['email'] = $user->mail;
  }

  // Provide alternate language support if desired.
  if (variable_get('disqus_localization', FALSE)) {
    global $language;
    $disqus['language'] = $language->language;
  }

  // Check if we are to provide Single Sign-On access.
  if (variable_get('disqus_sso', FALSE)) {
    $disqus += disqus_sso_disqus_settings($user);
  }

  // Check if we want to track new comments in Google Analytics.
  if (variable_get('disqus_track_newcomment_ga', FALSE)) {

    // Add a callback when a new comment is posted.
    $disqus['callbacks']['onNewComment'][] = 'Drupal.disqus.disqusTrackNewComment';

    // Attach the js with the callback implementation.
    $element['#attached']['js'][] = drupal_get_path('module', 'disqus') . '/js/disqus_ga.js';
  }

  /**
   * Pass callbacks on if needed. Callbacks array is two dimensional array
   * with callback type as key on first level and array of JS callbacks on the
   * second level.
   *
   * Example:
   * @code
   * $element['#disqus']['callbacks'] = array(
   *   'onNewComment' => array(
   *     'myCallbackThatFiresOnCommentPost',
   *     'Drupal.mymodule.anotherCallbInsideDrupalObj',
   *   ),
   * );
   * @endcode
   */
  if (!empty($element['#disqus']['callbacks'])) {
    $disqus['callbacks'] = $element['#disqus']['callbacks'];
  }

  // Add the disqus.js and all the settings to process the JavaScript and load Disqus.
  $element['#attached']['js'][] = drupal_get_path('module', 'disqus') . '/js/disqus.js';
  $element['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => array(
      'disqus' => $disqus,
    ),
  );
  return $children;
}