You are here

function template_preprocess_i18n_book_navigation in Book translation 6.2

Same name and namespace in other branches
  1. 6 i18n_book_navigation.module \template_preprocess_i18n_book_navigation()

Preprocesses the i18n_book_navigation. Very similar to the book_navigation, the i18n_book_navigation uses the same template file and template variables. These variables will be translated and the urls adapted.

Parameters

array &$variables: The template variables

See also

template_preprocess_book_navigation()

File

./i18n_book_navigation.module, line 330
Defines the i18n book navigation module. Contains all necessary data and hooks

Code

function template_preprocess_i18n_book_navigation(&$variables) {

  // Get the current node
  $tnode = menu_get_object();

  // Get the translation
  $node = i18n_book_navigation_get_original_node($tnode);

  // Set the i18n node selection mode in the correct language
  i18n_selection_mode('node', $node->language);

  // We MUST call the menu_tree_all_data() function BEFORE the book module does.
  // This is ok for performance, as the result is stored in a static cache, so
  // the tree will not get build all over again later on
  menu_tree_all_data($node->book['menu_name'], $node->book);

  // Revert the i18n node selection mode
  i18n_selection_mode('node', i18n_get_lang());

  // Provide the original book link
  $variables['book_link'] = $node->book;

  // Call the book module preprocess function
  template_preprocess_book_navigation($variables);

  // Translate title and book url
  $data = db_fetch_array(db_query("SELECT v.title, n.nid FROM {node_revisions} v\n                                    LEFT JOIN {node} n ON n.nid = v.nid AND n.vid = v.vid\n                                  WHERE n.tnid = %d AND n.language = '%s' LIMIT 1", array(
    $variables['book__id'],
    i18n_get_lang(),
  )));
  $variables['book_title'] = check_plain($data['title']);
  $variables['book_url'] = 'node/' . $data['nid'];

  // Translate the sub tree
  if (strlen($variables['tree'])) {
    $variables['tree'] = i18n_book_navigation_children($node->book);
  }

  // Translate the book links
  if ($variables['has_links']) {

    // Translate the "prev" link
    if ($variables['prev_url']) {
      if ($link = i18n_book_navigation_prev($variables['book_link'])) {
        $href = url($link['href']);

        // This *should* override the default link, as it comes afterwards
        drupal_add_link(array(
          'rel' => 'prev',
          'href' => $href,
        ));
        $variables['prev_url'] = $href;
        $variables['prev_title'] = check_plain($link['title']);
      }
      else {

        // Unset the variables
        unset($variables['prev_url'], $variables['prev_title']);
      }
    }

    // Translate the "up" link
    if ($variables['parent_url']) {
      if ($link = i18n_book_navigation_link_load($variables['book_link']['plid'])) {
        $href = url($link['href']);

        // This *should* override the default link, as it comes afterwards
        drupal_add_link(array(
          'rel' => 'up',
          'href' => $href,
        ));
        $variables['parent_url'] = $href;
        $variables['parent_title'] = check_plain($link['title']);
      }
      else {
        unset($variables['parent_url'], $variables['parent_title']);
      }
    }

    // Translate the "next" link
    if ($variables['next_url']) {
      if ($link = i18n_book_navigation_next($variables['book_link'])) {
        $href = url($link['href']);

        // This *should* override the default link, as it comes afterwards
        drupal_add_link(array(
          'rel' => 'next',
          'href' => $href,
        ));
        $variables['next_url'] = $href;
        $variables['next_title'] = check_plain($link['title']);
      }
      else {
        unset($variables['next_url'], $variables['next_title']);
      }
    }

    // Reset the "has_links" flag if needed
    $links = array(
      'prev_url',
      'prev_title',
      'parent_url',
      'parent_title',
      'next_url',
      'next_title',
    );
    foreach ($links as $link) {
      if (isset($variables[$link])) {
        $variables['has_links'] = TRUE;
      }
      else {
        $variables[$link] = '';
      }
    }
  }
}