-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_convert.api.php
132 lines (128 loc) · 4.53 KB
/
node_convert.api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* @file
* API documentation for the node_convert module.
*/
/**
* This is an example implementation for the hook. Preforms actions when converting a node based on it's 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
* - Any other information passed by $op = 'options' or $op = 'options validate'
* @param $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
* Should return a FAPI array only when using the options operation.
*/
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)));
}
}
}
}
/**
* Allow modifying a node during conversion but before the final save.
*
* @param stdClass $node
* The node object.
* @param array $hook_options
* Additional options passed to node_convert_node_convert().
*/
function hook_node_convert_presave($node, $hook_options = array()) {
// Set the author to user 1.
$node->uid = 1;
}