Commit 3ce68eb3 authored by Croitor Alexandru's avatar Croitor Alexandru
Browse files

Added hook_node_convert_change, to allow node modules perform needed changes...

Added hook_node_convert_change, to allow node modules perform needed changes when converting. Fixed a bug on bulk convert when not all nodes are chosen.
parent 76e35f53
......@@ -127,6 +127,12 @@ function node_convert_conversion_form($form_state, $node) {
$form['current_field_value_'. $i] = array('#type' => 'markup', '#value' => '<div>'. t("Current value is:") ." <b>". $temp_value .'</b></div>');
}
}
$hook_options = module_invoke_all('node_convert_change', array('dest_node_type' => $form_state['storage']['destination_type']), 'options');
if (!empty($hook_options)) {
$form['hook_options'] = $hook_options;
array_unshift($form['hook_options'], array('#value' => '<strong>'. t("Also the following parameters are available:") .'</strong>'));
$form['hook_options']['#tree'] = TRUE;
}
}
$form['submit'] = array('#type' => 'submit', '#value' => t("Submit"));
......@@ -155,7 +161,12 @@ function node_convert_conversion_form_submit($form, &$form_state) {
if (preg_match("/dest_field_[0-9]+?/", $key) == 1) $dest_fields[] = $value; // Destination fields
}
}
node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag);
if (!empty($form['hook_options'])) {
$hook_options = $form_state['values']['hook_options'];
} else {
$hook_options = NULL;
}
node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag, $hook_options);
drupal_set_message("The current node has been converted succesufuly");
// We clear the storage so redirect works
......@@ -223,7 +234,7 @@ function node_convert_bulk($form_state) {
elseif ($op == 'choose_fields') {
$info = $form_state['values']['info'];
$nodes = $form_state['values']['nodes'];
$info['nodes'] = $nodes;
$info['nodes'] = array_filter($nodes);
$source_fields = content_types($info['source_type']);
$source_fields = $source_fields['fields']; // Get the cck fields of the source type
......@@ -246,6 +257,12 @@ function node_convert_bulk($form_state) {
// The select populated with possible destination cck fields for each source field
$form['dest_field_'. $i] = array('#type' => 'select', '#options' => $options, '#title' => $field['field_name'] ." ". t("should be inserted into"));
}
$hook_options = module_invoke_all('node_convert_change', array('dest_node_type' => $info['dest_type']), 'options');
if (!empty($hook_options)) {
$form['hook_options'] = $hook_options;
array_unshift($form['hook_options'], array('#value' => '<strong>'. t("Also the following parameters are available:") .'</strong>'));
$form['hook_options']['#tree'] = TRUE;
}
$form['info'] = array('#type' => 'value', '#value' => $info);
}
......@@ -286,8 +303,13 @@ function node_convert_bulk_submit($form, &$form_state) {
if (preg_match("/dest_field_[0-9]+?/", $key) == 1) $info['fields']['destination'][] = $value; // Destination fields
}
}
if (!empty($form['hook_options'])) {
$hook_options = $form_state['values']['hook_options'];
} else {
$hook_options = NULL;
}
foreach ($info['nodes'] as $nid) {
node_convert_node_convert($nid, $info['dest_type'], $info['fields']['source'], $info['fields']['destination'], $info['no_fields']);
node_convert_node_convert($nid, $info['dest_type'], $info['fields']['source'], $info['fields']['destination'], $info['no_fields'], $hook_options);
}
drupal_set_message("Nodes converted succesufuly.");
......@@ -348,7 +370,7 @@ function theme_node_convert_bulk($form) {
* @param $no_fields_flag
* A boolean containing if there are source fields that have to be converted.
*/
function node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag) {
function node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag, $hook_options = NULL) {
$node = node_load($nid);
$vid = $node->vid;
$source_node_type = $node->type;
......@@ -361,9 +383,21 @@ function node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_
node_convert_field_convert($nid, $field, $dest_fields[$key]);
}
}
// We collate date to send to the hook implementations
$data = array(
'node' => $node,
'dest_node_type' => $dest_node_type,
);
if (!empty($hook_options)) $data['hook_options'] = $hook_options;
// We make sure that all custom node modules do their changes at the appropriate steps
module_invoke_all('node_convert_change', $data, 'insert');
module_invoke_all('node_convert_change', $data, 'delete');
if (count($tables_info[$source_node_type]['tables']) != 0)
db_query("DELETE FROM {%s} WHERE nid = %d", "content_type_". $source_node_type, $nid); // We delete the source node_type info
db_query("DELETE FROM {cache_content} WHERE cid = '%s'", "content:". $nid .":". $vid); // We clear the cache
cache_clear_all('node:'. $nid, 'cache_menu', 'TRUE');
cache_clear_all('node/'. $nid, 'cache_menu', 'TRUE');
}
/**
......@@ -448,3 +482,58 @@ function node_convert_field_convert($nid, $source_field, $dest_field) {
}
}
}
/**
* Preforms actions when converting a module based on the node type.
*
* @param $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
* @param $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.
* @return
* Should return a FAPI array only when using the options operation.
*/
function node_convert_node_convert_change($data, $op) {
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']);
}
} 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']);
}
} 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'),
);
}
return $form;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment