You are here

function template_preprocess_flippy in Flippy 7

Same name and namespace in other branches
  1. 8 flippy.module \template_preprocess_flippy()

Implements template_preprocess_hook()

File

./flippy.module, line 470
Allows administrators to add previous/next pagers to any node type.

Code

function template_preprocess_flippy(&$vars) {
  $links = array();
  drupal_add_css(drupal_get_path('module', 'flippy') . '/flippy.css');

  // for getting node type
  if ($node = menu_get_object('node')) {
    $vars['node'] = $node;
  }

  // Collect all of the node ids in the list
  $nids = array();
  foreach ($vars['list'] as $item) {
    if (isset($item['nid'])) {
      $nids[] = $item['nid'];
    }
  }

  // Load all of the nodes
  $list_nodes = node_load_multiple($nids);
  if ($nav = $vars['list']) {
    if (variable_get('flippy_firstlast_' . $vars['node']->type, NULL)) {
      $links['first'] = array(
        'title' => t(variable_get('flippy_first_label_' . $vars['node']->type, NULL)),
        'href' => empty($nav['first']) ? '' : drupal_get_path_alias('node/' . $nav['first']['nid'], 'und'),
      );
      $links['last'] = array(
        'title' => t(variable_get('flippy_last_label_' . $vars['node']->type, NULL)),
        'href' => empty($nav['last']) ? '' : drupal_get_path_alias('node/' . $nav['last']['nid'], 'und'),
      );
    }
    if (variable_get('flippy_random_' . $vars['node']->type, NULL)) {
      $links['random'] = array(
        'title' => t(variable_get('flippy_random_label_' . $vars['node']->type, NULL)),
        'href' => empty($nav['random']) ? '' : drupal_get_path_alias('node/' . $nav['random']['nid'], 'und'),
      );
    }
    $links['prev'] = array(
      'title' => t(variable_get('flippy_prev_label_' . $vars['node']->type, NULL)),
      'href' => empty($nav['prev']) ? '' : 'node/' . $nav['prev']['nid'],
    );
    $links['next'] = array(
      'title' => t(variable_get('flippy_next_label_' . $vars['node']->type, NULL)),
      'href' => empty($nav['next']) ? '' : 'node/' . $nav['next']['nid'],
    );
  }

  // Replace any tokens present in the links
  foreach ($links as $key => &$link) {
    if (isset($nav[$key]['nid']) && $nav[$key]['nid']) {
      if (isset($list_nodes[$nav[$key]['nid']])) {
        $link['title'] = token_replace($link['title'], array(
          'node' => $list_nodes[$nav[$key]['nid']],
        ));
      }
    }
  }

  // See if we need to truncate labels
  if ($truncate = variable_get('flippy_truncate_' . $vars['node']->type, NULL)) {
    if (is_numeric($truncate)) {
      foreach ($links as $key => &$link) {
        if (strlen($link['title']) > $truncate) {
          $link['title'] = mb_substr($link['title'], 0, $truncate) . variable_get('flippy_ellipse_' . $vars['node']->type, '...');
        }
      }
    }
  }

  // Set the order that we want the links to be in
  foreach (array(
    'first',
    'prev',
    'random',
    'next',
    'last',
  ) as $order) {
    if (isset($links[$order])) {
      $vars['links'][$order] = $links[$order];
    }
  }
  if (variable_get('flippy_loop_' . $vars['node']->type, NULL)) {
    if (!$vars['links']['next']['href']) {
      $vars['links']['next']['href'] = empty($nav['first']) ? '' : drupal_get_path_alias('node/' . $nav['first']['nid'], 'und');
      $vars['links']['next']['title'] = empty($nav['first']) || empty($vars['links']['next']['title']) ? '' : token_replace($vars['links']['next']['title'], array(
        'node' => $list_nodes[$nav['first']['nid']],
      ));
    }
    if (!$vars['links']['prev']['href']) {
      $vars['links']['prev']['href'] = empty($nav['last']) ? '' : drupal_get_path_alias('node/' . $nav['last']['nid'], 'und');
      $vars['links']['prev']['title'] = empty($nav['last']) || empty($vars['links']['prev']['title']) ? '' : token_replace($vars['links']['prev']['title'], array(
        'node' => $list_nodes[$nav['last']['nid']],
      ));
    }
  }
  $vars['show_empty'] = variable_get('flippy_show_empty_' . $vars['node']->type, TRUE);
  $vars = array_merge($vars, $vars['list']);
  unset($vars['list']);
  unset($vars['node']);
}