node.php

  1. drupal
    1. 4.6 developer/hooks/node.php
    2. 4.7 developer/hooks/node.php
    3. 5 developer/hooks/node.php
    4. 6 developer/hooks/node.php
    5. 8 core/modules/node/lib/Drupal/node/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().

Functions & methods

NameDescription
hook_accessDefine access restrictions.
hook_deleteRespond to node deletion.
hook_formDisplay a node editing form.
hook_insertRespond to node insertion.
hook_loadLoad node-type-specific information.
hook_node_infoDefine the human-readable name of a node type.
hook_prepareThis is a hook used by node modules. It is called after load but before the node is shown on the add/edit form.
hook_submitThis is a hook used by node modules. It is called after validation has succeeded and before insert/update. It is used to for actions which must happen only if the node is to be saved. Usually, $node is changed in some way and then the actual saving of…
hook_updateRespond to node updating.
hook_validateVerify a node editing form.
hook_viewDisplay a node.

File

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 the human-readable name of a node type.
  18. *
  19. * This is a hook used by node modules. This hook is required of modules
  20. * that define a node type. It is called to determine the names of the module's
  21. * nodes.
  22. *
  23. * @return
  24. * An array of information on the module's nodes. The array contains a
  25. * sub-array for each node with the node name as the key. Each sub-array has
  26. * two elements, 'name' and 'base'.
  27. *
  28. * The 'name' value is a human-readable name for the node and while the 'base'
  29. * value tells Drupal how a module's functions map to hooks (i.e. if the base
  30. * is example_foo then example_foo_insert will be called when inserting the
  31. * node).
  32. *
  33. * To prevent namespace conflicts, each node type defined by a module
  34. * should be prefixed by the name of the module and an underscore.
  35. *
  36. * For a detailed usage example, see node_example.module.
  37. */
  38. function hook_node_info() {
  39. return array(
  40. 'project_project' => array('name' => t('project'), 'base' => 'project_project'),
  41. 'project_issue' => array('name' => t('issue'), 'base' => 'project_issue')
  42. );
  43. }
  44. /**
  45. * Define access restrictions.
  46. *
  47. * This hook allows node modules to limit access to the node types they
  48. * define.
  49. *
  50. * @param $op
  51. * The operation to be performed. Possible values:
  52. * - "create"
  53. * - "delete"
  54. * - "update"
  55. * - "view"
  56. * @param $node
  57. * The node on which the operation is to be performed, or, if it does
  58. * not yet exist, the type of node to be created.
  59. * @return
  60. * TRUE if the operation may be performed; FALSE if the operation may not be
  61. * returned; NULL to not override the settings in the node_access table.
  62. *
  63. * The administrative account (user ID #1) always passes any access check,
  64. * so this hook is not called in that case. If this hook is not defined for
  65. * a node type, all access checks will fail, so only the administrator will
  66. * be able to see content of that type. However, users with the "administer
  67. * nodes" permission may always view and edit content through the
  68. * administrative interface.
  69. *
  70. * For a detailed usage example, see node_example.module.
  71. *
  72. * @ingroup node_access
  73. */
  74. function hook_access($op, $node) {
  75. global $user;
  76. if ($op == 'create') {
  77. return user_access('create stories');
  78. }
  79. if ($op == 'update' || $op == 'delete') {
  80. if (user_access('edit own stories') && ($user->uid == $node->uid)) {
  81. return TRUE;
  82. }
  83. }
  84. }
  85. /**
  86. * Respond to node deletion.
  87. *
  88. * This is a hook used by node modules. It is called to allow the module
  89. * to take action when a node is being deleted from the database by, for
  90. * example, deleting information from related tables.
  91. *
  92. * @param &$node
  93. * The node being deleted.
  94. * @return
  95. * None.
  96. *
  97. * To take action when nodes of any type are deleted (not just nodes of
  98. * the type defined by this module), use hook_nodeapi() instead.
  99. *
  100. * For a detailed usage example, see node_example.module.
  101. */
  102. function hook_delete(&$node) {
  103. db_query('DELETE FROM {mytable} WHERE nid = %d', $node->nid);
  104. }
  105. /**
  106. * This is a hook used by node modules. It is called after validation has succeeded and before insert/update.
  107. * It is used to for actions which must happen only if the node is to be saved. Usually, $node is
  108. * changed in some way and then the actual saving of that change is left for the insert/update hooks.
  109. *
  110. * @param &$node
  111. * The node being saved.
  112. * @return
  113. * None.
  114. *
  115. * For a detailed usage example, see fileupload.module.
  116. */
  117. function hook_submit(&$node) {
  118. // if a file was uploaded, move it to the files directory
  119. if ($file = file_check_upload('file')) {
  120. $node->file = file_save_upload($file, file_directory_path(), false);
  121. }
  122. }
  123. /**
  124. * This is a hook used by node modules. It is called after load but before the node is shown on the add/edit form.
  125. *
  126. * @param &$node
  127. * The node being saved.
  128. * @return
  129. * None.
  130. *
  131. * For a usage example, see image.module.
  132. */
  133. function hook_prepare(&$node) {
  134. if ($file = file_check_upload($field_name)) {
  135. $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE));
  136. if ($file) {
  137. if (!image_get_info($file->filepath)) {
  138. form_set_error($field_name, t('Uploaded file is not a valid image'));
  139. return;
  140. }
  141. }
  142. else {
  143. return;
  144. }
  145. $node->images['_original'] = $file->filepath;
  146. _image_build_derivatives($node, true);
  147. $node->new_file = TRUE;
  148. }
  149. }
  150. /**
  151. * Display a node editing form.
  152. *
  153. * This hook, implemented by node modules, is called to retrieve the form
  154. * that is displayed when one attempts to "create/edit" an item. This form is
  155. * displayed at the URI http://www.example.com/?q=node/<add|edit>/nodetype.
  156. *
  157. * @param &$node
  158. * The node being added or edited.
  159. * @return
  160. * An array containing the form elements to be displayed in the node
  161. * edit form.
  162. *
  163. * The submit and preview buttons, taxonomy controls, and administrative
  164. * accoutrements are displayed automatically by node.module. This hook
  165. * needs to return the node title, the body text area, and fields
  166. * specific to the node type.
  167. *
  168. * For a detailed usage example, see node_example.module.
  169. */
  170. function hook_form(&$node) {
  171. $form['title'] = array(
  172. '#type'=> 'textfield',
  173. '#title' => t('Title'),
  174. '#required' => TRUE,
  175. );
  176. $form['body'] = array(
  177. '#type' => 'textarea',
  178. '#title' => t('Description'),
  179. '#rows' => 20,
  180. '#required' => TRUE,
  181. );
  182. $form['field1'] = array(
  183. '#type' => 'textfield',
  184. '#title' => t('Custom field'),
  185. '#default_value' => $node->field1,
  186. '#maxlength' => 127,
  187. );
  188. $form['selectbox'] = array(
  189. '#type' => 'select',
  190. '#title' => t('Select box'),
  191. '#default_value' => $node->selectbox,
  192. '#options' => array(
  193. 1 => 'Option A',
  194. 2 => 'Option B',
  195. 3 => 'Option C',
  196. ),
  197. '#description' => t('Please choose an option.'),
  198. );
  199. return $form;
  200. }
  201. /**
  202. * Respond to node insertion.
  203. *
  204. * This is a hook used by node modules. It is called to allow the module
  205. * to take action when a new node is being inserted in the database by,
  206. * for example, inserting information into related tables.
  207. *
  208. * @param $node
  209. * The node being inserted.
  210. * @return
  211. * None.
  212. *
  213. * To take action when nodes of any type are inserted (not just nodes of
  214. * the type(s) defined by this module), use hook_nodeapi() instead.
  215. *
  216. * For a detailed usage example, see node_example.module.
  217. */
  218. function hook_insert($node) {
  219. db_query("INSERT INTO {mytable} (nid, extra)
  220. VALUES (%d, '%s')", $node->nid, $node->extra);
  221. }
  222. /**
  223. * Load node-type-specific information.
  224. *
  225. * This is a hook used by node modules. It is called to allow the module
  226. * a chance to load extra information that it stores about a node, or
  227. * possibly replace already loaded information - which can be dangerous.
  228. *
  229. * @param $node
  230. * The node being loaded. At call time, node.module has already loaded
  231. * the basic information about the node, such as its node ID (nid),
  232. * title, and body.
  233. * @return
  234. * An object containing properties of the node being loaded. This will
  235. * be merged with the passed-in $node to result in an object containing
  236. * a set of properties resulting from adding the extra properties to
  237. * the passed-in ones, and overwriting the passed-in ones with the
  238. * extra properties if they have the same name as passed-in properties.
  239. *
  240. * For a detailed usage example, see node_example.module.
  241. */
  242. function hook_load($node) {
  243. $additions = db_fetch_object(db_query('SELECT * FROM {mytable} WHERE vid = %d', $node->vid));
  244. return $additions;
  245. }
  246. /**
  247. * Respond to node updating.
  248. *
  249. * This is a hook used by node modules. It is called to allow the module
  250. * to take action when an edited node is being updated in the database by,
  251. * for example, updating information in related tables.
  252. *
  253. * @param $node
  254. * The node being updated.
  255. * @return
  256. * None.
  257. *
  258. * To take action when nodes of any type are updated (not just nodes of
  259. * the type(s) defined by this module), use hook_nodeapi() instead.
  260. *
  261. * For a detailed usage example, see node_example.module.
  262. */
  263. function hook_update($node) {
  264. db_query("UPDATE {mytable} SET extra = '%s' WHERE nid = %d",
  265. $node->extra, $node->nid);
  266. }
  267. /**
  268. * Verify a node editing form.
  269. *
  270. * This is a hook used by node modules. It is called to allow the module
  271. * to verify that the node is in a format valid to post to the site.
  272. * Errors should be set with form_set_error().
  273. *
  274. * @param $node
  275. * The node to be validated.
  276. * @return
  277. * None.
  278. *
  279. * To validate nodes of all types (not just nodes of the type(s) defined by
  280. * this module), use hook_nodeapi() instead.
  281. *
  282. * Changes made to the $node object within a hook_validate() function will
  283. * have no effect. The preferred method to change a node's content is to use
  284. * hook_submit() or hook_nodeapi($op='submit') instead. If it is really
  285. * necessary to change the node at the validate stage, you can use function
  286. * form_set_value().
  287. *
  288. * For a detailed usage example, see node_example.module.
  289. */
  290. function hook_validate($node) {
  291. if (isset($node->end) && isset($node->start)) {
  292. if ($node->start > $node->end) {
  293. form_set_error('time', t('An event may not end before it starts.'));
  294. }
  295. }
  296. }
  297. /**
  298. * Display a node.
  299. *
  300. * This is a hook used by node modules. It allows a module to define a
  301. * custom method of displaying its nodes, usually by displaying extra
  302. * information particular to that node type.
  303. *
  304. * @param &$node
  305. * The node to be displayed.
  306. * @param $teaser
  307. * Whether we are to generate a "teaser" or summary of the node, rather than
  308. * display the whole thing.
  309. * @param $page
  310. * Whether the node is being displayed as a standalone page. If this is
  311. * TRUE, the node title should not be displayed, as it will be printed
  312. * automatically by the theme system. Also, the module may choose to alter
  313. * the default breadcrumb trail in this case.
  314. * @return
  315. * None. The passed-by-reference $node parameter should be modified as
  316. * necessary so it can be properly presented by theme('node', $node). This
  317. * means, for instance, that content should be passed through the filter
  318. * system by calling check_output() on appropriate fields or by sending the
  319. * node through node_prepare().
  320. *
  321. * For a detailed usage example, see node_example.module.
  322. */
  323. function hook_view(&$node, $teaser = FALSE, $page = FALSE) {
  324. if ($page) {
  325. $breadcrumb = array();
  326. $breadcrumb[] = array('path' => 'example', 'title' => t('example'));
  327. $breadcrumb[] = array('path' => 'example/'. $node->field1,
  328. 'title' => t('%category', array('%category' => $node->field1)));
  329. $breadcrumb[] = array('path' => 'node/'. $node->nid);
  330. menu_set_location($breadcrumb);
  331. }
  332. $node = node_prepare($node, $teaser);
  333. }
  334. /**
  335. * @} End of "addtogroup hooks".
  336. */
Login or register to post comments