You are here

function hook_node_convert_change in Node Convert 7

Same name and namespace in other branches
  1. 6 node_convert.module \hook_node_convert_change()

This is an example implementation for the hook. Preforms actions when converting a node based on it's type.

Parameters

$data: An array containing information about the conversion process. The keys are

  • dest_node_type The destination type of the node
  • node The node object
  • Any other information passed by $op = 'options' or $op = 'options validate'

$op: A string containing the operation which should be executed. These are the possible values

  • insert Operations which should be run when the node is transferred to the new node type.

Usually for transferring and adding new node information into the database.

  • delete Operations which should be run after the node is transferred to the new node type.

Usually for deleting unneeded information from the database after the transfer.

  • options Configuration elements shown on the conversion form. Should return a FAPI array.
  • options validate Validation check on the options elements.

Return value

Should return a FAPI array only when using the options operation.

7 functions implement hook_node_convert_change()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_node_convert_change in modules/book.node_convert_behavior.inc
Implementation of node_convert_change().
comment_node_convert_change in modules/comment.node_convert_behavior.inc
Implementation of node_convert_change().
file_node_convert_change in modules/file.node_convert_behavior.inc
Implementation of node_convert_change().
forum_node_convert_change in modules/forum.node_convert_behavior.inc
Implementation of node_convert_change().
panels_node_node_convert_change in modules/panels_node.node_convert_behavior.inc
Implementation of node_convert_change().

... See full list

File

./node_convert.api.php, line 50
API documentation for the node_convert module.

Code

function hook_node_convert_change($data, $op) {

  // All of this is just an example.
  if ($op == 'insert') {
    if ($data['dest_node_type'] == 'book') {
      $book = array();
      $node = $data['node'];
      $book['link_path'] = 'node/' . $node->nid;
      $book['link_title'] = $node->title;
      $book['plid'] = 0;
      $book['menu_name'] = book_menu_name($node->nid);
      $mlid = menu_link_save($book);
      $book['bid'] = $data['hook_options']['bid'];
      if ($book['bid'] == 'self') {
        $book['bid'] = $node->nid;
      }
      $id = db_insert('book')
        ->fields(array(
        'nid' => $node->nid,
        'mlid' => $book['mlid'],
        'bid' => $book['bid'],
      ))
        ->execute();
    }
    if ($data['dest_node_type'] == 'forum') {
      $id = db_insert('forum')
        ->fields(array(
        'tid' => $data['hook_options']['forum'],
        'vid' => $data['node']->vid,
        'nid' => $data['node']->nid,
      ))
        ->execute();
      $id = db_insert('taxonomy_term_node')
        ->fields(array(
        'tid' => $data['hook_options']['forum'],
        'vid' => $data['node']->vid,
        'nid' => $data['node']->nid,
      ))
        ->execute();
    }
  }
  elseif ($op == 'delete') {
    if ($data['node']->type == 'book') {
      menu_link_delete($data['node']->book['mlid']);
      db_delete('book')
        ->condition('mlid', $data['node']->book['mlid'])
        ->execute();
    }
    if ($data['node']->type == 'forum') {
      db_delete('forum')
        ->condition('nid', $data['node']->nid)
        ->execute();
      db_delete('taxonomy_term_node')
        ->condition('nid', $data['node']->nid)
        ->execute();
    }
  }
  elseif ($op == 'options') {
    $form = array();
    if ($data['dest_node_type'] == 'book') {
      foreach (book_get_books() as $book) {
        $options[$book['nid']] = $book['title'];
      }
      $options = array(
        'self' => '<' . t('create a new book') . '>',
      ) + $options;
      $form['bid'] = array(
        '#type' => 'select',
        '#title' => t('Book'),
        '#options' => $options,
        '#description' => t('Your page will be a part of the selected book.'),
        '#attributes' => array(
          'class' => 'book-title-select',
        ),
      );
    }
    if ($data['dest_node_type'] == 'forum') {
      $vid = variable_get('forum_nav_vocabulary', '');
      $form['forum'] = taxonomy_form($vid);
      $form['forum']['#weight'] = 7;
      $form['forum']['#required'] = TRUE;
      $form['forum']['#options'][''] = t('- Please choose -');
    }
    return $form;
  }
  elseif ($op == 'options validate') {
    $form_state = $data['form_state'];
    if ($data['dest_node_type'] == 'forum') {
      $containers = variable_get('forum_containers', array());
      $term = $form_state['values']['hook_options']['forum'];
      if (in_array($term, $containers)) {
        $term = taxonomy_term_load($term);
        form_set_error('hook_options][forum', t('The item %forum is only a container for forums. Please select one of the forums below it.', array(
          '%forum' => $term->name,
        )));
      }
    }
  }
}