node.php

These hooks are defined by node modules, modules that define a new kind of node.

If you don't need to make a new node type but rather extend the existing ones, you should instead investigate using hook_nodeapi().

Node hooks are typically called by node.module using node_invoke().

File

documentation-6.x/developer/hooks/node.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * These hooks are defined by node modules, modules that define a new kind
  5. * of node.
  6. *
  7. * If you don't need to make a new node type but rather extend the existing
  8. * ones, you should instead investigate using hook_nodeapi().
  9. *
  10. * Node hooks are typically called by node.module using node_invoke().
  11. */
  12. /**
  13. * @addtogroup hooks
  14. * @{
  15. */
  16. /**
  17. * Define module-provided node types.
  18. *
  19. * This is a hook used by node modules. This hook is required for modules to
  20. * define one or more node types. It is called to determine the names and the
  21. * attributes of a module's node types.
  22. *
  23. * Only module-provided node types should be defined through this hook. User-
  24. * provided (or 'custom') node types should be defined only in the 'node_type'
  25. * database table, and should be maintained by using the node_type_save() and
  26. * node_type_delete() functions.
  27. *
  28. * @return
  29. * An array of information on the module's node types. The array contains a
  30. * sub-array for each node type, with the machine-readable type name as the
  31. * key. Each sub-array has up to 10 attributes. Possible attributes:
  32. * - "name": the human-readable name of the node type. Required.
  33. * - "module": a string telling Drupal how a module's functions map to hooks
  34. * (i.e. if module is defined as example_foo, then example_foo_insert will
  35. * be called when inserting a node of that type). This string is usually
  36. * the name of the module in question, but not always. Required.
  37. * - "description": a brief description of the node type. Required.
  38. * - "help": text that will be displayed at the top of the submission form for
  39. * this content type. Optional (defaults to '').
  40. * - "has_title": boolean indicating whether or not this node type has a title
  41. * field. Optional (defaults to TRUE).
  42. * - "title_label": the label for the title field of this content type.
  43. * Optional (defaults to 'Title').
  44. * - "has_body": boolean indicating whether or not this node type has a body
  45. * field. Optional (defaults to TRUE).
  46. * - "body_label": the label for the body field of this content type. Optional
  47. * (defaults to 'Body').
  48. * - "min_word_count": the minimum number of words for the body field to be
  49. * considered valid for this content type. Optional (defaults to 0).
  50. * - "locked": boolean indicating whether the machine-readable name of this
  51. * content type can (FALSE) or cannot (TRUE) be edited by a site
  52. * administrator. Optional (defaults to TRUE).
  53. *
  54. * The machine-readable name of a node type should contain only letters,
  55. * numbers, and underscores. Underscores will be converted into hyphens for the
  56. * purpose of constructing URLs.
  57. *
  58. * All attributes of a node type that are defined through this hook (except for
  59. * 'locked') can be edited by a site administrator. This includes the
  60. * machine-readable name of a node type, if 'locked' is set to FALSE.
  61. *
  62. * For a detailed usage example, see node_example.module.
  63. */
  64. function hook_node_info() {
  65. return array(
  66. 'book' => array(
  67. 'name' => t('book page'),
  68. 'module' => 'book',
  69. 'description' => t("A book is a collaborative writing effort: users can collaborate writing the pages of the book, positioning the pages in the right order, and reviewing or modifying pages previously written. So when you have some information to share or when you read a page of the book and you didn't like it, or if you think a certain page could have been written better, you can do something about it."),
  70. )
  71. );
  72. }
  73. /**
  74. * Act on node type changes.
  75. *
  76. * This hook allows modules to take action when a node type is modified.
  77. *
  78. * @param $op
  79. * What is being done to $info. Possible values:
  80. * - "delete"
  81. * - "insert"
  82. * - "update"
  83. * Most modules will not need the update or insert operations, because form
  84. * fields you add to the node type editing form will be automatically saved as
  85. * variables. For instance, if you add a field called 'my_module_foo' to
  86. * content type 'machine_name', its value will be saved as variable
  87. * 'my_module_foo_machine_name' (and if the machine name is updated, the
  88. * variables will be updated appropriately).
  89. * @param $info
  90. * The node type object on which $op is being performed.
  91. */
  92. function hook_node_type($op, $info) {
  93. switch ($op){
  94. case 'delete':
  95. // Example from comment.module.
  96. variable_del('comment_'. $info->type);
  97. break;
  98. case 'update':
  99. // Here is an example where you do need an update operation (from the
  100. // book module), because the simple default case doesn't cover what needs
  101. // to be done.
  102. if (!empty($info->old_type) && $info->old_type != $info->type) {
  103. // Update the list of node types that are allowed to be added to books.
  104. $allowed_types = variable_get('book_allowed_types', array('book'));
  105. $key = array_search($info->old_type, $allowed_types);
  106. if ($key !== FALSE) {
  107. $allowed_types[$info->type] = $allowed_types[$key] ? $info->type : 0;
  108. unset($allowed_types[$key]);
  109. variable_set('book_allowed_types', $allowed_types);
  110. }
  111. // Update the setting for the "Add child page" link.
  112. if (variable_get('book_child_type', 'book') == $info->old_type) {
  113. variable_set('book_child_type', $info->type);
  114. }
  115. }
  116. break;
  117. }
  118. }
  119. /**
  120. * Define access restrictions.
  121. *
  122. * This hook allows node modules to limit access to the node types they
  123. * define.
  124. *
  125. * @param $op
  126. * The operation to be performed. Possible values:
  127. * - "create"
  128. * - "delete"
  129. * - "update"
  130. * - "view"
  131. * @param $node
  132. * Either a node object or the machine name of the content type on which to
  133. * perform the access check.
  134. * @param $account
  135. * The user object to perform the access check operation on.
  136. *
  137. * @return
  138. * - TRUE if the operation is to be allowed.
  139. * - FALSE if the operation is to be denied.
  140. * - NULL to not override the settings in the node_access table, or access
  141. * control modules.
  142. *
  143. * The administrative account (user ID #1) always passes any access check,
  144. * so this hook is not called in that case. If this hook is not defined for
  145. * a node type, all access checks will fail, so only the administrator will
  146. * be able to see content of that type. However, users with the "administer
  147. * nodes" permission may always view and edit content through the
  148. * administrative interface.
  149. *
  150. * For a detailed usage example, see node_example.module.
  151. *
  152. * @ingroup node_access
  153. */
  154. function hook_access($op, $node, $account) {
  155. if ($op == 'create') {
  156. return user_access('create stories', $account);
  157. }
  158. if ($op == 'update' || $op == 'delete') {
  159. if (user_access('edit own stories', $account) && ($account->uid == $node->uid)) {
  160. return TRUE;
  161. }
  162. }
  163. }
  164. /**
  165. * Respond to node deletion.
  166. *
  167. * This is a hook used by node modules. It is called to allow the module
  168. * to take action when a node is being deleted from the database by, for
  169. * example, deleting information from related tables.
  170. *
  171. * @param &$node
  172. * The node being deleted.
  173. * @return
  174. * None.
  175. *
  176. * To take action when nodes of any type are deleted (not just nodes of
  177. * the type defined by this module), use hook_nodeapi() instead.
  178. *
  179. * For a detailed usage example, see node_example.module.
  180. */
  181. function hook_delete(&$node) {
  182. db_query('DELETE FROM {mytable} WHERE nid = %d', $node->nid);
  183. }
  184. /**
  185. * This is a hook used by node modules. It is called after load but before the
  186. * node is shown on the add/edit form.
  187. *
  188. * @param &$node
  189. * The node being saved.
  190. * @return
  191. * None.
  192. *
  193. * For a usage example, see image.module.
  194. */
  195. function hook_prepare(&$node) {
  196. if ($file = file_check_upload($field_name)) {
  197. $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE));
  198. if ($file) {
  199. if (!image_get_info($file->filepath)) {
  200. form_set_error($field_name, t('Uploaded file is not a valid image'));
  201. return;
  202. }
  203. }
  204. else {
  205. return;
  206. }
  207. $node->images['_original'] = $file->filepath;
  208. _image_build_derivatives($node, true);
  209. $node->new_file = TRUE;
  210. }
  211. }
  212. /**
  213. * Display a node editing form.
  214. *
  215. * This hook, implemented by node modules, is called to retrieve the form
  216. * that is displayed when one attempts to "create/edit" an item. This form is
  217. * displayed at the URI http://www.example.com/?q=node/<add|edit>/nodetype.
  218. *
  219. * @param &$node
  220. * The node being added or edited.
  221. * @param $form_state
  222. * The form state array. Changes made to this variable will have no effect.
  223. * @return
  224. * An array containing the form elements to be displayed in the node
  225. * edit form.
  226. *
  227. * The submit and preview buttons, taxonomy controls, and administrative
  228. * accoutrements are displayed automatically by node.module. This hook
  229. * needs to return the node title, the body text area, and fields
  230. * specific to the node type.
  231. *
  232. * For a detailed usage example, see node_example.module.
  233. */
  234. function hook_form(&$node, $form_state) {
  235. $type = node_get_types('type', $node);
  236. $form['title'] = array(
  237. '#type'=> 'textfield',
  238. '#title' => check_plain($type->title_label),
  239. '#required' => TRUE,
  240. '#default_value' => $node->title,
  241. );
  242. $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  243. $form['field1'] = array(
  244. '#type' => 'textfield',
  245. '#title' => t('Custom field'),
  246. '#default_value' => $node->field1,
  247. '#maxlength' => 127,
  248. );
  249. $form['selectbox'] = array(
  250. '#type' => 'select',
  251. '#title' => t('Select box'),
  252. '#default_value' => $node->selectbox,
  253. '#options' => array(
  254. 1 => 'Option A',
  255. 2 => 'Option B',
  256. 3 => 'Option C',
  257. ),
  258. '#description' => t('Please choose an option.'),
  259. );
  260. return $form;
  261. }
  262. /**
  263. * Respond to node insertion.
  264. *
  265. * This is a hook used by node modules. It is called to allow the module
  266. * to take action when a new node is being inserted in the database by,
  267. * for example, inserting information into related tables.
  268. *
  269. * @param $node
  270. * The node being inserted.
  271. * @return
  272. * None.
  273. *
  274. * To take action when nodes of any type are inserted (not just nodes of
  275. * the type(s) defined by this module), use hook_nodeapi() instead.
  276. *
  277. * For a detailed usage example, see node_example.module.
  278. */
  279. function hook_insert($node) {
  280. db_query("INSERT INTO {mytable} (nid, extra)
  281. VALUES (%d, '%s')", $node->nid, $node->extra);
  282. }
  283. /**
  284. * Load node-type-specific information.
  285. *
  286. * This is a hook used by node modules. It is called to allow the module
  287. * a chance to load extra information that it stores about a node, or
  288. * possibly replace already loaded information - which can be dangerous.
  289. *
  290. * @param $node
  291. * The node being loaded. At call time, node.module has already loaded
  292. * the basic information about the node, such as its node ID (nid),
  293. * title, and body.
  294. * @return
  295. * An object containing properties of the node being loaded. This will
  296. * be merged with the passed-in $node to result in an object containing
  297. * a set of properties resulting from adding the extra properties to
  298. * the passed-in ones, and overwriting the passed-in ones with the
  299. * extra properties if they have the same name as passed-in properties.
  300. *
  301. * For a detailed usage example, see node_example.module.
  302. */
  303. function hook_load($node) {
  304. $additions = db_fetch_object(db_query('SELECT * FROM {mytable} WHERE vid = %d', $node->vid));
  305. return $additions;
  306. }
  307. /**
  308. * Respond to node updating.
  309. *
  310. * This is a hook used by node modules. It is called to allow the module
  311. * to take action when an edited node is being updated in the database by,
  312. * for example, updating information in related tables.
  313. *
  314. * @param $node
  315. * The node being updated.
  316. * @return
  317. * None.
  318. *
  319. * To take action when nodes of any type are updated (not just nodes of
  320. * the type(s) defined by this module), use hook_nodeapi() instead.
  321. *
  322. * For a detailed usage example, see node_example.module.
  323. */
  324. function hook_update($node) {
  325. db_query("UPDATE {mytable} SET extra = '%s' WHERE nid = %d",
  326. $node->extra, $node->nid);
  327. }
  328. /**
  329. * Verify a node editing form.
  330. *
  331. * This is a hook used by node modules. It is called to allow the module
  332. * to verify that the node is in a format valid to post to the site.
  333. * Errors should be set with form_set_error().
  334. *
  335. * @param $node
  336. * The node to be validated.
  337. * @param $form
  338. * The node edit form array.
  339. * @return
  340. * None.
  341. *
  342. * To validate nodes of all types (not just nodes of the type(s) defined by
  343. * this module), use hook_nodeapi() instead.
  344. *
  345. * Changes made to the $node object within a hook_validate() function will
  346. * have no effect. The preferred method to change a node's content is to use
  347. * hook_nodeapi($op='presave') instead. If it is really necessary to change
  348. * the node at the validate stage, you can use function form_set_value().
  349. *
  350. * For a detailed usage example, see node_example.module.
  351. */
  352. function hook_validate($node, &$form) {
  353. if (isset($node->end) && isset($node->start)) {
  354. if ($node->start > $node->end) {
  355. form_set_error('time', t('An event may not end before it starts.'));
  356. }
  357. }
  358. }
  359. /**
  360. * Display a node.
  361. *
  362. * This is a hook used by node modules. It allows a module to define a
  363. * custom method of displaying its nodes, usually by displaying extra
  364. * information particular to that node type.
  365. *
  366. * @param $node
  367. * The node to be displayed.
  368. * @param $teaser
  369. * Whether we are to generate a "teaser" or summary of the node, rather than
  370. * display the whole thing.
  371. * @param $page
  372. * Whether the node is being displayed as a standalone page. If this is
  373. * TRUE, the node title should not be displayed, as it will be printed
  374. * automatically by the theme system. Also, the module may choose to alter
  375. * the default breadcrumb trail in this case.
  376. * @return
  377. * $node. The passed $node parameter should be modified as necessary and
  378. * returned so it can be properly presented. Nodes are prepared for display
  379. * by assembling a structured array in $node->content, rather than directly
  380. * manipulating $node->body and $node->teaser. The format of this array is
  381. * the same used by the Forms API. As with FormAPI arrays, the #weight
  382. * property can be used to control the relative positions of added elements.
  383. * If for some reason you need to change the body or teaser returned by
  384. * node_prepare(), you can modify $node->content['body']['#value']. Note
  385. * that this will be the un-rendered content. To modify the rendered output,
  386. * see hook_nodeapi($op = 'alter').
  387. *
  388. * For a detailed usage example, see node_example.module.
  389. */
  390. function hook_view($node, $teaser = FALSE, $page = FALSE) {
  391. if ($page) {
  392. $breadcrumb = array();
  393. $breadcrumb[] = l(t('Home'), NULL);
  394. $breadcrumb[] = l(t('Example'), 'example');
  395. $breadcrumb[] = l($node->field1, 'example/' . $node->field1);
  396. drupal_set_breadcrumb($breadcrumb);
  397. }
  398. $node = node_prepare($node, $teaser);
  399. $node->content['myfield'] = array(
  400. '#value' => theme('mymodule_myfield', $node->myfield),
  401. '#weight' => 1,
  402. );
  403. return $node;
  404. }
  405. /**
  406. * @} End of "addtogroup hooks".
  407. */