You are here

function book_node_import_values_alter in Node import 6

Implementation of hook_node_import_values_alter().

File

supported/book.inc, line 136
Support file for the core book module.

Code

function book_node_import_values_alter(&$values, $type, $defaults, $options, $fields, $preview) {
  static $virtual_book = 0;
  static $virtual_book_page = 0;
  if (($node_type = node_import_type_is_node($type)) !== FALSE && book_type_is_allowed($node_type) && (user_access('administer book outlines') || user_access('add content to books'))) {
    $book = $values['book:book'];
    $parent = $values['book:parent'];
    $weight = $values['book:weight'];
    unset($values['book:book']);
    unset($values['book:parent']);
    unset($values['book:weight']);

    // Not in a book.
    if (drupal_strlen($book) == 0) {
      $values['book'] = array(
        'bid' => 0,
        'weight' => $weight,
      );
      return;
    }

    // A new top-level book.
    if ($book == 'new') {
      if ($preview) {
        drupal_set_message(t('A new book %title will be created.', array(
          '%title' => $values['title'],
        )));
        $virtual_book--;
        node_import_set_object('book', $values['title'], $virtual_book);
        node_import_set_object('book:title', $virtual_book, $values['title']);
      }
      else {

        // When a new book is created, the form will no longer validate
        // as book_get_books() is cached.
        global $node_import_can_continue;
        $node_import_can_continue = FALSE;
      }
      $values['book'] = array(
        'bid' => $book,
        'weight' => $weight,
      );
      return;
    }

    // A new page in an existing book.
    $plid = node_import_get_object('book:' . $book, $parent);
    if ($plid === NULL && $book > 0 && drupal_strlen($parent) > 0) {
      if ($plid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE menu_name = '%s' AND module = 'book' AND (LOWER(link_title) = '%s' OR link_path = '%s')", book_menu_name($book), drupal_strtolower($parent), 'node/' . (is_numeric($parent) && intval($parent) > 0 ? $parent : '')))) {
        node_import_set_object('book:' . $book, $parent, $plid);
      }
    }
    if ($book > 0 && isset($plid) && $plid > 0) {
      $values['book'] = array(
        'plid' => $plid,
        'weight' => $weight,
        'bid' => $book,
      );
      return;
    }
    else {
      if ($book > 0 && drupal_strlen($parent) == 0) {
        $values['book'] = array(
          'bid' => $book,
          'weight' => $weight,
        );
        return;
      }
    }
    if ($preview) {
      $virtual_book_page--;
      node_import_set_object('book:' . $book, $values['title'], $virtual_book_page);
      node_import_set_object('book:' . $book . ':title', $virtual_book_page, $values['title']);
      if ($book < 0) {
        drupal_set_message(t('This will be a page in %title.', array(
          '%title' => node_import_get_object('book:title', $book),
        )));
      }
      else {
        $values['book'] = array(
          'bid' => $book,
          'weight' => $weight,
        );
      }
      if ($plid < 0) {
        drupal_set_message(t('This will be a child page of %title.', array(
          '%title' => node_import_get_object('book:' . $book . ':title', $plid),
        )));
        return;
      }
      else {
        if (drupal_strlen($parent) == 0) {
          drupal_set_message(t('This will be the top-level page in this book.'));
          return;
        }
      }
    }
    node_import_input_error(t('Input error: %value is not allowed for %name (not a book page in %book).', array(
      '%value' => $parent,
      '%name' => t('Parent item'),
      '%book' => node_import_get_object('book:title', $book),
    )));
  }
}