You are here

function hook_node_convert_change in Node Convert 6

Same name and namespace in other branches
  1. 7 node_convert.api.php \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 containg 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.

4 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 includes/book.node_convert.inc
Implementation of node_convert_change().
forum_node_convert_change in includes/forum.node_convert.inc
Implementation of node_convert_change().
panels_node_node_convert_change in includes/panels_node.node_convert.inc
Implementation of node_convert_change().
uc_product_node_convert_change in includes/uc_product.node_convert.inc
Implementation of node_convert_change().
5 invocations of hook_node_convert_change()
node_convert_add_template in ./node_convert.module
node_convert_add_template_validate in ./node_convert.module
node_convert_conversion_form in ./node_convert.module
node_convert_conversion_form_validate in ./node_convert.module
node_convert_node_convert in ./node_convert.module
Converts a node to another content type.

File

./node_convert.module, line 853
The node_convert module converts nodes from one type to another.

Code

function hook_node_convert_change($data, $op) {

  // All of this is just an example, there real data is being called from hook_init
  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;
      }
      db_query("INSERT INTO {book} (nid, mlid, bid) VALUES (%d, %d, %d)", $node->nid, $book['mlid'], $book['bid']);
    }
    if ($data['dest_node_type'] == 'forum') {
      db_query('INSERT INTO {forum} (tid, vid, nid) VALUES (%d, %d, %d)', $data['hook_options']['forum'], $data['node']->vid, $data['node']->nid);
      db_query('INSERT INTO {term_node} (tid, vid, nid) VALUES (%d, %d, %d)', $data['hook_options']['forum'], $data['node']->vid, $data['node']->nid);
    }
  }
  elseif ($op == 'delete') {
    if ($data['node']->type == 'book') {
      menu_link_delete($data['node']->book['mlid']);
      db_query('DELETE FROM {book} WHERE mlid = %d', $data['node']->book['mlid']);
    }
    if ($data['node']->type == 'forum') {
      db_query('DELETE FROM {forum} WHERE nid = %d', $data['node']->nid);
      db_query('DELETE FROM {term_node} WHERE nid = %d', $data['node']->nid);
    }
  }
  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_get_term($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,
        )));
      }
    }
  }
}