You are here

function npop_ajax_load_node in Node pop-up 7

Ajax node loading callback.

Parameters

object $node: Node or false if url is incorrect.

string $mode: Query mode by default "nojs" automatically convert to "ajax" when core ajax.js is attached to project.

Return value

array Ajax interface commands for showing loaded node.

1 string reference to 'npop_ajax_load_node'
npop_menu in ./npop.module
Implements hook_menu().

File

./npop.module, line 60
Create popup nodes with ajax and Drupal core functionality.

Code

function npop_ajax_load_node($node, $mode) {
  $commands = array();

  // Check if $node is loaded correctly.
  if (!$node) {

    // Set watchdog message.
    watchdog('npop', 'The link is not correct', array(), WATCHDOG_WARNING);

    // If page is called without js show 404 not found page.
    if ($mode == 'nojs') {
      drupal_not_found();
      drupal_exit();
    }
    else {

      // Alert about incorrect link.
      $commands[] = ajax_command_alert(t('The link is not correct'));
      return array(
        '#type' => 'ajax',
        '#commands' => $commands,
      );
    }
  }

  // Generate ajax commands for showing loaded node.
  $nid = $node->nid;

  // If page is called without js command, then redirect to node page.
  if ($mode == 'nojs') {
    drupal_goto('node/' . $nid);
  }
  $classes = array(
    'npop-content',
    'npop-nid-' . $node->nid,
  );

  // Load selected animation class name and apply it.
  $animations_class_name = variable_get('npop_animations');
  if ($animations_class_name) {
    $classes[] = 'animated';
    $classes[] = $animations_class_name;
  }
  $view['overlay']['#markup'] = '<div class="npop-overlay"></div>';
  $view['node']['content'] = node_view($node, 'npop_ajax');
  $view['node']['#prefix'] = '<div class="' . implode(" ", $classes) . '">';
  $view['node']['#suffix'] = '</div>';

  // Generate parent url link for History API.
  // @todo - find another way for this feature.
  $url = url('node/' . $nid);
  $query = drupal_get_query_parameters();
  if (isset($query['parent'])) {
    $url_parent = check_plain($query['parent']);
    unset($_GET['parent']);
  }
  else {
    $url_parts = explode('/', $url);
    array_pop($url_parts);
    $url_parent = implode('/', $url_parts);
  }

  // Close button options.
  $close_btn_options = array(
    'html' => TRUE,
    'attributes' => array(
      'class' => array(
        'use-ajax',
        'npop-close-btn',
        'icons',
      ),
    ),
    'query' => array(
      'url' => $url_parent,
      'id' => $nid,
    ),
  );

  // Check overrided close link text.
  if (variable_get('npop_close_override', FALSE)) {
    $close_link_text = variable_get('npop_close_override_text');
  }
  else {
    $close_link_text = t('Close', array(), array(
      'context' => 'npop',
    ));
  }
  $view['node']['closebtn'] = array(
    '#theme' => 'link',
    '#text' => '<span class="npop-close-text">' . $close_link_text . '</span>',
    '#path' => 'ajax/npop/close',
    '#options' => $close_btn_options,
  );
  $wrapper_attributes = array(
    'id' => 'npop-' . $nid,
    'class' => array(
      'npop',
      'npop-node-type-' . $node->type,
    ),
  );
  $view['#prefix'] = '<div' . drupal_attributes($wrapper_attributes) . '>';
  $view['#suffix'] = '</div>';

  // Allow other modules to alter view data.
  drupal_alter('npop_view_data', $view, $node);
  $commands[] = ajax_command_append('body', render($view));
  $commands[] = ajax_command_invoke('body', 'addClass', array(
    'npop-over',
  ));
  if (npop_check_change_url($node->type)) {
    $commands[] = array(
      'command' => 'npop_change_url',
      'url' => $url,
    );
  }

  // Add an ability to alter commands before executing them.
  drupal_alter('npop_ajax', $commands, $node);
  drupal_add_css(drupal_get_path('module', 'npop') . '/css/npop.css');
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}