node.api.php

  1. 7.x drupal/modules/node/node.api.php
  2. 8.x drupal/core/modules/node/node.api.php

Hooks provided by the Node module.

Functions

Namesort descending Description
hook_delete Respond to node deletion.
hook_form Display a node editing form.
hook_insert Respond to creation of a new node.
hook_load Act on nodes being loaded from the database.
hook_node_access Control access to a node.
hook_node_access_records Set permissions for a node to be written to the database.
hook_node_access_records_alter Alter permissions for a node before it is written to the database.
hook_node_delete Respond to node deletion.
hook_node_grants Inform the node access system what permissions the user has.
hook_node_grants_alter Alter user access rules when trying to view, edit or delete a node.
hook_node_info Define module-provided node types.
hook_node_insert Respond to creation of a new node.
hook_node_load Act on arbitrary nodes being loaded from the database.
hook_node_operations Add mass node operations.
hook_node_prepare Act on a node object about to be shown on the add/edit form.
hook_node_presave Act on a node being inserted or updated.
hook_node_revision_delete Respond to deletion of a node revision.
hook_node_search_result Act on a node being displayed as a search result.
hook_node_submit Act on a node after validated form values have been copied to it.
hook_node_type_delete Respond to node type deletion.
hook_node_type_insert Respond to node type creation.
hook_node_type_update Respond to node type updates.
hook_node_update Respond to updates to a node.
hook_node_update_index Act on a node being indexed for searching.
hook_node_validate Perform node validation before a node is created or updated.
hook_node_view Act on a node that is being assembled before rendering.
hook_node_view_alter Alter the results of node_view().
hook_prepare Act on a node object about to be shown on the add/edit form.
hook_ranking Provide additional methods of scoring for core search results for nodes.
hook_update Respond to updates to a node.
hook_validate Perform node validation before a node is created or updated.
hook_view Display a node.

File

drupal/modules/node/node.api.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Node module.
  5. */
  6. /**
  7. * @defgroup node_api_hooks Node API Hooks
  8. * @{
  9. * Functions to define and modify content types.
  10. *
  11. * Each content type is maintained by a primary module, which is either
  12. * node.module (for content types created in the user interface) or the module
  13. * that implements hook_node_info() to define the content type.
  14. *
  15. * During node operations (create, update, view, delete, etc.), there are
  16. * several sets of hooks that get invoked to allow modules to modify the base
  17. * node operation:
  18. * - Node-type-specific hooks: When defining a node type, hook_node_info()
  19. * returns a 'base' component. Node-type-specific hooks are named
  20. * base_hookname() instead of mymodule_hookname() (in a module called
  21. * 'mymodule' for example). Only the node type's corresponding implementation
  22. * is invoked. For example, poll_node_info() in poll.module defines the base
  23. * for the 'poll' node type as 'poll'. So when a poll node is created,
  24. * hook_insert() is invoked on poll_insert() only.
  25. * Hooks that are node-type-specific are noted below.
  26. * - All-module hooks: This set of hooks is invoked on all implementing modules,
  27. * to allow other modules to modify what the primary node module is doing. For
  28. * example, hook_node_insert() is invoked on all modules when creating a poll
  29. * node.
  30. * - Field hooks: Hooks related to the fields attached to the node. These are
  31. * invoked from the field operations functions described below, and can be
  32. * either field-type-specific or all-module hooks.
  33. * - Entity hooks: Generic hooks for "entity" operations. These are always
  34. * invoked on all modules.
  35. *
  36. * Here is a list of the node and entity hooks that are invoked, field
  37. * operations, and other steps that take place during node operations:
  38. * - Creating a new node (calling node_save() on a new node):
  39. * - field_attach_presave()
  40. * - hook_node_presave() (all)
  41. * - hook_entity_presave() (all)
  42. * - Node and revision records are written to the database
  43. * - hook_insert() (node-type-specific)
  44. * - field_attach_insert()
  45. * - hook_node_insert() (all)
  46. * - hook_entity_insert() (all)
  47. * - hook_node_access_records() (all)
  48. * - hook_node_access_records_alter() (all)
  49. * - Updating an existing node (calling node_save() on an existing node):
  50. * - field_attach_presave()
  51. * - hook_node_presave() (all)
  52. * - hook_entity_presave() (all)
  53. * - Node and revision records are written to the database
  54. * - hook_update() (node-type-specific)
  55. * - field_attach_update()
  56. * - hook_node_update() (all)
  57. * - hook_entity_update() (all)
  58. * - hook_node_access_records() (all)
  59. * - hook_node_access_records_alter() (all)
  60. * - Loading a node (calling node_load(), node_load_multiple() or entity_load()
  61. * with $entity_type of 'node'):
  62. * - Node and revision information is read from database.
  63. * - hook_load() (node-type-specific)
  64. * - field_attach_load_revision() and field_attach_load()
  65. * - hook_entity_load() (all)
  66. * - hook_node_load() (all)
  67. * - Viewing a single node (calling node_view() - note that the input to
  68. * node_view() is a loaded node, so the Loading steps above are already done):
  69. * - hook_view() (node-type-specific)
  70. * - field_attach_prepare_view()
  71. * - hook_entity_prepare_view() (all)
  72. * - field_attach_view()
  73. * - hook_node_view() (all)
  74. * - hook_entity_view() (all)
  75. * - hook_node_view_alter() (all)
  76. * - hook_entity_view_alter() (all)
  77. * - Viewing multiple nodes (calling node_view_multiple() - note that the input
  78. * to node_view_multiple() is a set of loaded nodes, so the Loading steps
  79. * above are already done):
  80. * - field_attach_prepare_view()
  81. * - hook_entity_prepare_view() (all)
  82. * - hook_view() (node-type-specific)
  83. * - field_attach_view()
  84. * - hook_node_view() (all)
  85. * - hook_entity_view() (all)
  86. * - hook_node_view_alter() (all)
  87. * - hook_entity_view_alter() (all)
  88. * - Deleting a node (calling node_delete() or node_delete_multiple()):
  89. * - Node is loaded (see Loading section above)
  90. * - hook_delete() (node-type-specific)
  91. * - hook_node_delete() (all)
  92. * - hook_entity_delete() (all)
  93. * - field_attach_delete()
  94. * - Node and revision information are deleted from database
  95. * - Deleting a node revision (calling node_revision_delete()):
  96. * - Node is loaded (see Loading section above)
  97. * - Revision information is deleted from database
  98. * - hook_node_revision_delete() (all)
  99. * - field_attach_delete_revision()
  100. * - Preparing a node for editing (calling node_form() - note that if it is an
  101. * existing node, it will already be loaded; see the Loading section above):
  102. * - hook_prepare() (node-type-specific)
  103. * - hook_node_prepare() (all)
  104. * - hook_form() (node-type-specific)
  105. * - field_attach_form()
  106. * - Validating a node during editing form submit (calling
  107. * node_form_validate()):
  108. * - hook_validate() (node-type-specific)
  109. * - hook_node_validate() (all)
  110. * - field_attach_form_validate()
  111. * - Searching (calling node_search_execute()):
  112. * - hook_ranking() (all)
  113. * - Query is executed to find matching nodes
  114. * - Resulting node is loaded (see Loading section above)
  115. * - Resulting node is prepared for viewing (see Viewing a single node above)
  116. * - comment_node_update_index() is called.
  117. * - hook_node_search_result() (all)
  118. * - Search indexing (calling node_update_index()):
  119. * - Node is loaded (see Loading section above)
  120. * - Node is prepared for viewing (see Viewing a single node above)
  121. * - hook_node_update_index() (all)
  122. * @}
  123. */
  124. /**
  125. * @addtogroup hooks
  126. * @{
  127. */
  128. /**
  129. * Inform the node access system what permissions the user has.
  130. *
  131. * This hook is for implementation by node access modules. In this hook,
  132. * the module grants a user different "grant IDs" within one or more
  133. * "realms". In hook_node_access_records(), the realms and grant IDs are
  134. * associated with permission to view, edit, and delete individual nodes.
  135. *
  136. * The realms and grant IDs can be arbitrarily defined by your node access
  137. * module; it is common to use role IDs as grant IDs, but that is not required.
  138. * Your module could instead maintain its own list of users, where each list has
  139. * an ID. In that case, the return value of this hook would be an array of the
  140. * list IDs that this user is a member of.
  141. *
  142. * A node access module may implement as many realms as necessary to properly
  143. * define the access privileges for the nodes. Note that the system makes no
  144. * distinction between published and unpublished nodes. It is the module's
  145. * responsibility to provide appropriate realms to limit access to unpublished
  146. * content.
  147. *
  148. * Node access records are stored in the {node_access} table and define which
  149. * grants are required to access a node. There is a special case for the view
  150. * operation -- a record with node ID 0 corresponds to a "view all" grant for
  151. * the realm and grant ID of that record. If there are no node access modules
  152. * enabled, the core node module adds a node ID 0 record for realm 'all'. Node
  153. * access modules can also grant "view all" permission on their custom realms;
  154. * for example, a module could create a record in {node_access} with:
  155. * @code
  156. * $record = array(
  157. * 'nid' => 0,
  158. * 'gid' => 888,
  159. * 'realm' => 'example_realm',
  160. * 'grant_view' => 1,
  161. * 'grant_update' => 0,
  162. * 'grant_delete' => 0,
  163. * );
  164. * drupal_write_record('node_access', $record);
  165. * @endcode
  166. * And then in its hook_node_grants() implementation, it would need to return:
  167. * @code
  168. * if ($op == 'view') {
  169. * $grants['example_realm'] = array(888);
  170. * }
  171. * @endcode
  172. * If you decide to do this, be aware that the node_access_rebuild() function
  173. * will erase any node ID 0 entry when it is called, so you will need to make
  174. * sure to restore your {node_access} record after node_access_rebuild() is
  175. * called.
  176. *
  177. * @see node_access_view_all_nodes()
  178. * @see node_access_rebuild()
  179. *
  180. * @param $account
  181. * The user object whose grants are requested.
  182. * @param $op
  183. * The node operation to be performed, such as 'view', 'update', or 'delete'.
  184. *
  185. * @return
  186. * An array whose keys are "realms" of grants, and whose values are arrays of
  187. * the grant IDs within this realm that this user is being granted.
  188. *
  189. * For a detailed example, see node_access_example.module.
  190. *
  191. * @ingroup node_access
  192. */
  193. function hook_node_grants($account, $op) {
  194. if (user_access('access private content', $account)) {
  195. $grants['example'] = array(1);
  196. }
  197. $grants['example_author'] = array($account->uid);
  198. return $grants;
  199. }
  200. /**
  201. * Set permissions for a node to be written to the database.
  202. *
  203. * When a node is saved, a module implementing hook_node_access_records() will
  204. * be asked if it is interested in the access permissions for a node. If it is
  205. * interested, it must respond with an array of permissions arrays for that
  206. * node.
  207. *
  208. * Node access grants apply regardless of the published or unpublished status
  209. * of the node. Implementations must make sure not to grant access to
  210. * unpublished nodes if they don't want to change the standard access control
  211. * behavior. Your module may need to create a separate access realm to handle
  212. * access to unpublished nodes.
  213. *
  214. * Note that the grant values in the return value from your hook must be
  215. * integers and not boolean TRUE and FALSE.
  216. *
  217. * Each permissions item in the array is an array with the following elements:
  218. * - 'realm': The name of a realm that the module has defined in
  219. * hook_node_grants().
  220. * - 'gid': A 'grant ID' from hook_node_grants().
  221. * - 'grant_view': If set to 1 a user that has been identified as a member
  222. * of this gid within this realm can view this node. This should usually be
  223. * set to $node->status. Failure to do so may expose unpublished content
  224. * to some users.
  225. * - 'grant_update': If set to 1 a user that has been identified as a member
  226. * of this gid within this realm can edit this node.
  227. * - 'grant_delete': If set to 1 a user that has been identified as a member
  228. * of this gid within this realm can delete this node.
  229. * - 'priority': If multiple modules seek to set permissions on a node, the
  230. * realms that have the highest priority will win out, and realms with a lower
  231. * priority will not be written. If there is any doubt, it is best to
  232. * leave this 0.
  233. *
  234. *
  235. * When an implementation is interested in a node but want to deny access to
  236. * everyone, it may return a "deny all" grant:
  237. *
  238. * @code
  239. * $grants[] = array(
  240. * 'realm' => 'all',
  241. * 'gid' => 0,
  242. * 'grant_view' => 0,
  243. * 'grant_update' => 0,
  244. * 'grant_delete' => 0,
  245. * 'priority' => 1,
  246. * );
  247. * @endcode
  248. *
  249. * Setting the priority should cancel out other grants. In the case of a
  250. * conflict between modules, it is safer to use hook_node_access_records_alter()
  251. * to return only the deny grant.
  252. *
  253. * Note: a deny all grant is not written to the database; denies are implicit.
  254. *
  255. * @see node_access_write_grants()
  256. *
  257. * @param $node
  258. * The node that has just been saved.
  259. *
  260. * @return
  261. * An array of grants as defined above.
  262. *
  263. * @see hook_node_access_records_alter()
  264. * @ingroup node_access
  265. */
  266. function hook_node_access_records($node) {
  267. // We only care about the node if it has been marked private. If not, it is
  268. // treated just like any other node and we completely ignore it.
  269. if ($node->private) {
  270. $grants = array();
  271. // Only published nodes should be viewable to all users. If we allow access
  272. // blindly here, then all users could view an unpublished node.
  273. if ($node->status) {
  274. $grants[] = array(
  275. 'realm' => 'example',
  276. 'gid' => 1,
  277. 'grant_view' => 1,
  278. 'grant_update' => 0,
  279. 'grant_delete' => 0,
  280. 'priority' => 0,
  281. );
  282. }
  283. // For the example_author array, the GID is equivalent to a UID, which
  284. // means there are many groups of just 1 user.
  285. // Note that an author can always view his or her nodes, even if they
  286. // have status unpublished.
  287. $grants[] = array(
  288. 'realm' => 'example_author',
  289. 'gid' => $node->uid,
  290. 'grant_view' => 1,
  291. 'grant_update' => 1,
  292. 'grant_delete' => 1,
  293. 'priority' => 0,
  294. );
  295. return $grants;
  296. }
  297. }
  298. /**
  299. * Alter permissions for a node before it is written to the database.
  300. *
  301. * Node access modules establish rules for user access to content. Node access
  302. * records are stored in the {node_access} table and define which permissions
  303. * are required to access a node. This hook is invoked after node access modules
  304. * returned their requirements via hook_node_access_records(); doing so allows
  305. * modules to modify the $grants array by reference before it is stored, so
  306. * custom or advanced business logic can be applied.
  307. *
  308. * @see hook_node_access_records()
  309. *
  310. * Upon viewing, editing or deleting a node, hook_node_grants() builds a
  311. * permissions array that is compared against the stored access records. The
  312. * user must have one or more matching permissions in order to complete the
  313. * requested operation.
  314. *
  315. * A module may deny all access to a node by setting $grants to an empty array.
  316. *
  317. * @see hook_node_grants()
  318. * @see hook_node_grants_alter()
  319. *
  320. * @param $grants
  321. * The $grants array returned by hook_node_access_records().
  322. * @param $node
  323. * The node for which the grants were acquired.
  324. *
  325. * The preferred use of this hook is in a module that bridges multiple node
  326. * access modules with a configurable behavior, as shown in the example with the
  327. * 'is_preview' field.
  328. *
  329. * @ingroup node_access
  330. */
  331. function hook_node_access_records_alter(&$grants, $node) {
  332. // Our module allows editors to mark specific articles with the 'is_preview'
  333. // field. If the node being saved has a TRUE value for that field, then only
  334. // our grants are retained, and other grants are removed. Doing so ensures
  335. // that our rules are enforced no matter what priority other grants are given.
  336. if ($node->is_preview) {
  337. // Our module grants are set in $grants['example'].
  338. $temp = $grants['example'];
  339. // Now remove all module grants but our own.
  340. $grants = array('example' => $temp);
  341. }
  342. }
  343. /**
  344. * Alter user access rules when trying to view, edit or delete a node.
  345. *
  346. * Node access modules establish rules for user access to content.
  347. * hook_node_grants() defines permissions for a user to view, edit or delete
  348. * nodes by building a $grants array that indicates the permissions assigned to
  349. * the user by each node access module. This hook is called to allow modules to
  350. * modify the $grants array by reference, so the interaction of multiple node
  351. * access modules can be altered or advanced business logic can be applied.
  352. *
  353. * @see hook_node_grants()
  354. *
  355. * The resulting grants are then checked against the records stored in the
  356. * {node_access} table to determine if the operation may be completed.
  357. *
  358. * A module may deny all access to a user by setting $grants to an empty array.
  359. *
  360. * @see hook_node_access_records()
  361. * @see hook_node_access_records_alter()
  362. *
  363. * @param $grants
  364. * The $grants array returned by hook_node_grants().
  365. * @param $account
  366. * The user account requesting access to content.
  367. * @param $op
  368. * The operation being performed, 'view', 'update' or 'delete'.
  369. *
  370. * Developers may use this hook to either add additional grants to a user or to
  371. * remove existing grants. These rules are typically based on either the
  372. * permissions assigned to a user role, or specific attributes of a user
  373. * account.
  374. *
  375. * @ingroup node_access
  376. */
  377. function hook_node_grants_alter(&$grants, $account, $op) {
  378. // Our sample module never allows certain roles to edit or delete
  379. // content. Since some other node access modules might allow this
  380. // permission, we expressly remove it by returning an empty $grants
  381. // array for roles specified in our variable setting.
  382. // Get our list of banned roles.
  383. $restricted = variable_get('example_restricted_roles', array());
  384. if ($op != 'view' && !empty($restricted)) {
  385. // Now check the roles for this account against the restrictions.
  386. foreach ($restricted as $role_id) {
  387. if (isset($account->roles[$role_id])) {
  388. $grants = array();
  389. }
  390. }
  391. }
  392. }
  393. /**
  394. * Add mass node operations.
  395. *
  396. * This hook enables modules to inject custom operations into the mass
  397. * operations dropdown found at admin/content, by associating a callback
  398. * function with the operation, which is called when the form is submitted. The
  399. * callback function receives one initial argument, which is an array of the
  400. * checked nodes.
  401. *
  402. * @return
  403. * An array of operations. Each operation is an associative array that may
  404. * contain the following key-value pairs:
  405. * - label: (required) The label for the operation, displayed in the dropdown
  406. * menu.
  407. * - callback: (required) The function to call for the operation.
  408. * - callback arguments: (optional) An array of additional arguments to pass
  409. * to the callback function.
  410. */
  411. function hook_node_operations() {
  412. $operations = array(
  413. 'publish' => array(
  414. 'label' => t('Publish selected content'),
  415. 'callback' => 'node_mass_update',
  416. 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED)),
  417. ),
  418. 'unpublish' => array(
  419. 'label' => t('Unpublish selected content'),
  420. 'callback' => 'node_mass_update',
  421. 'callback arguments' => array('updates' => array('status' => NODE_NOT_PUBLISHED)),
  422. ),
  423. 'promote' => array(
  424. 'label' => t('Promote selected content to front page'),
  425. 'callback' => 'node_mass_update',
  426. 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED, 'promote' => NODE_PROMOTED)),
  427. ),
  428. 'demote' => array(
  429. 'label' => t('Demote selected content from front page'),
  430. 'callback' => 'node_mass_update',
  431. 'callback arguments' => array('updates' => array('promote' => NODE_NOT_PROMOTED)),
  432. ),
  433. 'sticky' => array(
  434. 'label' => t('Make selected content sticky'),
  435. 'callback' => 'node_mass_update',
  436. 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED, 'sticky' => NODE_STICKY)),
  437. ),
  438. 'unsticky' => array(
  439. 'label' => t('Make selected content not sticky'),
  440. 'callback' => 'node_mass_update',
  441. 'callback arguments' => array('updates' => array('sticky' => NODE_NOT_STICKY)),
  442. ),
  443. 'delete' => array(
  444. 'label' => t('Delete selected content'),
  445. 'callback' => NULL,
  446. ),
  447. );
  448. return $operations;
  449. }
  450. /**
  451. * Respond to node deletion.
  452. *
  453. * This hook is invoked from node_delete_multiple() after the type-specific
  454. * hook_delete() has been invoked, but before hook_entity_delete and
  455. * field_attach_delete() are called, and before the node is removed from the
  456. * node table in the database.
  457. *
  458. * @param $node
  459. * The node that is being deleted.
  460. *
  461. * @ingroup node_api_hooks
  462. */
  463. function hook_node_delete($node) {
  464. db_delete('mytable')
  465. ->condition('nid', $node->nid)
  466. ->execute();
  467. }
  468. /**
  469. * Respond to deletion of a node revision.
  470. *
  471. * This hook is invoked from node_revision_delete() after the revision has been
  472. * removed from the node_revision table, and before
  473. * field_attach_delete_revision() is called.
  474. *
  475. * @param $node
  476. * The node revision (node object) that is being deleted.
  477. *
  478. * @ingroup node_api_hooks
  479. */
  480. function hook_node_revision_delete($node) {
  481. db_delete('mytable')
  482. ->condition('vid', $node->vid)
  483. ->execute();
  484. }
  485. /**
  486. * Respond to creation of a new node.
  487. *
  488. * This hook is invoked from node_save() after the database query that will
  489. * insert the node into the node table is scheduled for execution, after the
  490. * type-specific hook_insert() is invoked, and after field_attach_insert() is
  491. * called.
  492. *
  493. * Note that when this hook is invoked, the changes have not yet been written to
  494. * the database, because a database transaction is still in progress. The
  495. * transaction is not finalized until the save operation is entirely completed
  496. * and node_save() goes out of scope. You should not rely on data in the
  497. * database at this time as it is not updated yet. You should also note that any
  498. * write/update database queries executed from this hook are also not committed
  499. * immediately. Check node_save() and db_transaction() for more info.
  500. *
  501. * @param $node
  502. * The node that is being created.
  503. *
  504. * @ingroup node_api_hooks
  505. */
  506. function hook_node_insert($node) {
  507. db_insert('mytable')
  508. ->fields(array(
  509. 'nid' => $node->nid,
  510. 'extra' => $node->extra,
  511. ))
  512. ->execute();
  513. }
  514. /**
  515. * Act on arbitrary nodes being loaded from the database.
  516. *
  517. * This hook should be used to add information that is not in the node or node
  518. * revisions table, not to replace information that is in these tables (which
  519. * could interfere with the entity cache). For performance reasons, information
  520. * for all available nodes should be loaded in a single query where possible.
  521. *
  522. * This hook is invoked during node loading, which is handled by entity_load(),
  523. * via classes NodeController and DrupalDefaultEntityController. After the node
  524. * information is read from the database or the entity cache, hook_load() is
  525. * invoked on the node's content type module, then field_attach_load_revision()
  526. * or field_attach_load() is called, then hook_entity_load() is invoked on all
  527. * implementing modules, and finally hook_node_load() is invoked on all
  528. * implementing modules.
  529. *
  530. * @param $nodes
  531. * An array of the nodes being loaded, keyed by nid.
  532. * @param $types
  533. * An array containing the node types present in $nodes. Allows for an early
  534. * return for modules that only support certain node types. However, if your
  535. * module defines a content type, you can use hook_load() to respond to
  536. * loading of just that content type.
  537. *
  538. * For a detailed usage example, see nodeapi_example.module.
  539. *
  540. * @ingroup node_api_hooks
  541. */
  542. function hook_node_load($nodes, $types) {
  543. // Decide whether any of $types are relevant to our purposes.
  544. if (count(array_intersect($types_we_want_to_process, $types))) {
  545. // Gather our extra data for each of these nodes.
  546. $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
  547. // Add our extra data to the node objects.
  548. foreach ($result as $record) {
  549. $nodes[$record->nid]->foo = $record->foo;
  550. }
  551. }
  552. }
  553. /**
  554. * Control access to a node.
  555. *
  556. * Modules may implement this hook if they want to have a say in whether or not
  557. * a given user has access to perform a given operation on a node.
  558. *
  559. * The administrative account (user ID #1) always passes any access check, so
  560. * this hook is not called in that case. Users with the "bypass node access"
  561. * permission may always view and edit content through the administrative
  562. * interface.
  563. *
  564. * Note that not all modules will want to influence access on all node types. If
  565. * your module does not want to actively grant or block access, return
  566. * NODE_ACCESS_IGNORE or simply return nothing. Blindly returning FALSE will
  567. * break other node access modules.
  568. *
  569. * Also note that this function isn't called for node listings (e.g., RSS feeds,
  570. * the default home page at path 'node', a recent content block, etc.) See
  571. * @link node_access Node access rights @endlink for a full explanation.
  572. *
  573. * @param $node
  574. * Either a node object or the machine name of the content type on which to
  575. * perform the access check.
  576. * @param $op
  577. * The operation to be performed. Possible values:
  578. * - "create"
  579. * - "delete"
  580. * - "update"
  581. * - "view"
  582. * @param $account
  583. * The user object to perform the access check operation on.
  584. *
  585. * @return
  586. * - NODE_ACCESS_ALLOW: if the operation is to be allowed.
  587. * - NODE_ACCESS_DENY: if the operation is to be denied.
  588. * - NODE_ACCESS_IGNORE: to not affect this operation at all.
  589. *
  590. * @ingroup node_access
  591. */
  592. function hook_node_access($node, $op, $account) {
  593. $type = is_string($node) ? $node : $node->type;
  594. if (in_array($type, node_permissions_get_configured_types())) {
  595. if ($op == 'create' && user_access('create ' . $type . ' content', $account)) {
  596. return NODE_ACCESS_ALLOW;
  597. }
  598. if ($op == 'update') {
  599. if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
  600. return NODE_ACCESS_ALLOW;
  601. }
  602. }
  603. if ($op == 'delete') {
  604. if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
  605. return NODE_ACCESS_ALLOW;
  606. }
  607. }
  608. }
  609. // Returning nothing from this function would have the same effect.
  610. return NODE_ACCESS_IGNORE;
  611. }
  612. /**
  613. * Act on a node object about to be shown on the add/edit form.
  614. *
  615. * This hook is invoked from node_object_prepare() after the type-specific
  616. * hook_prepare() is invoked.
  617. *
  618. * @param $node
  619. * The node that is about to be shown on the add/edit form.
  620. *
  621. * @ingroup node_api_hooks
  622. */
  623. function hook_node_prepare($node) {
  624. if (!isset($node->comment)) {
  625. $node->comment = variable_get("comment_$node->type", COMMENT_NODE_OPEN);
  626. }
  627. }
  628. /**
  629. * Act on a node being displayed as a search result.
  630. *
  631. * This hook is invoked from node_search_execute(), after node_load() and
  632. * node_view() have been called.
  633. *
  634. * @param $node
  635. * The node being displayed in a search result.
  636. *
  637. * @return array
  638. * Extra information to be displayed with search result. This information
  639. * should be presented as an associative array. It will be concatenated with
  640. * the post information (last updated, author) in the default search result
  641. * theming.
  642. *
  643. * @see template_preprocess_search_result()
  644. * @see search-result.tpl.php
  645. *
  646. * @ingroup node_api_hooks
  647. */
  648. function hook_node_search_result($node) {
  649. $comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField();
  650. return array('comment' => format_plural($comments, '1 comment', '@count comments'));
  651. }
  652. /**
  653. * Act on a node being inserted or updated.
  654. *
  655. * This hook is invoked from node_save() before the node is saved to the
  656. * database.
  657. *
  658. * @param $node
  659. * The node that is being inserted or updated.
  660. *
  661. * @ingroup node_api_hooks
  662. */
  663. function hook_node_presave($node) {
  664. if ($node->nid && $node->moderate) {
  665. // Reset votes when node is updated:
  666. $node->score = 0;
  667. $node->users = '';
  668. $node->votes = 0;
  669. }
  670. }
  671. /**
  672. * Respond to updates to a node.
  673. *
  674. * This hook is invoked from node_save() after the database query that will
  675. * update node in the node table is scheduled for execution, after the
  676. * type-specific hook_update() is invoked, and after field_attach_update() is
  677. * called.
  678. *
  679. * Note that when this hook is invoked, the changes have not yet been written to
  680. * the database, because a database transaction is still in progress. The
  681. * transaction is not finalized until the save operation is entirely completed
  682. * and node_save() goes out of scope. You should not rely on data in the
  683. * database at this time as it is not updated yet. You should also note that any
  684. * write/update database queries executed from this hook are also not committed
  685. * immediately. Check node_save() and db_transaction() for more info.
  686. *
  687. * @param $node
  688. * The node that is being updated.
  689. *
  690. * @ingroup node_api_hooks
  691. */
  692. function hook_node_update($node) {
  693. db_update('mytable')
  694. ->fields(array('extra' => $node->extra))
  695. ->condition('nid', $node->nid)
  696. ->execute();
  697. }
  698. /**
  699. * Act on a node being indexed for searching.
  700. *
  701. * This hook is invoked during search indexing, after node_load(), and after the
  702. * result of node_view() is added as $node->rendered to the node object.
  703. *
  704. * @param $node
  705. * The node being indexed.
  706. *
  707. * @return string
  708. * Additional node information to be indexed.
  709. *
  710. * @ingroup node_api_hooks
  711. */
  712. function hook_node_update_index($node) {
  713. $text = '';
  714. $comments = db_query('SELECT subject, comment, format FROM {comment} WHERE nid = :nid AND status = :status', array(':nid' => $node->nid, ':status' => COMMENT_PUBLISHED));
  715. foreach ($comments as $comment) {
  716. $text .= '<h2>' . check_plain($comment->subject) . '</h2>' . check_markup($comment->comment, $comment->format, '', TRUE);
  717. }
  718. return $text;
  719. }
  720. /**
  721. * Perform node validation before a node is created or updated.
  722. *
  723. * This hook is invoked from node_validate(), after a user has has finished
  724. * editing the node and is previewing or submitting it. It is invoked at the
  725. * end of all the standard validation steps, and after the type-specific
  726. * hook_validate() is invoked.
  727. *
  728. * To indicate a validation error, use form_set_error().
  729. *
  730. * Note: Changes made to the $node object within your hook implementation will
  731. * have no effect. The preferred method to change a node's content is to use
  732. * hook_node_presave() instead. If it is really necessary to change the node at
  733. * the validate stage, you can use form_set_value().
  734. *
  735. * @param $node
  736. * The node being validated.
  737. * @param $form
  738. * The form being used to edit the node.
  739. * @param $form_state
  740. * The form state array.
  741. *
  742. * @ingroup node_api_hooks
  743. */
  744. function hook_node_validate($node, $form, &$form_state) {
  745. if (isset($node->end) && isset($node->start)) {
  746. if ($node->start > $node->end) {
  747. form_set_error('time', t('An event may not end before it starts.'));
  748. }
  749. }
  750. }
  751. /**
  752. * Act on a node after validated form values have been copied to it.
  753. *
  754. * This hook is invoked when a node form is submitted with either the "Save" or
  755. * "Preview" button, after form values have been copied to the form state's node
  756. * object, but before the node is saved or previewed. It is a chance for modules
  757. * to adjust the node's properties from what they are simply after a copy from
  758. * $form_state['values']. This hook is intended for adjusting non-field-related
  759. * properties. See hook_field_attach_submit() for customizing field-related
  760. * properties.
  761. *
  762. * @param $node
  763. * The node object being updated in response to a form submission.
  764. * @param $form
  765. * The form being used to edit the node.
  766. * @param $form_state
  767. * The form state array.
  768. *
  769. * @ingroup node_api_hooks
  770. */
  771. function hook_node_submit($node, $form, &$form_state) {
  772. // Decompose the selected menu parent option into 'menu_name' and 'plid', if
  773. // the form used the default parent selection widget.
  774. if (!empty($form_state['values']['menu']['parent'])) {
  775. list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']);
  776. }
  777. }
  778. /**
  779. * Act on a node that is being assembled before rendering.
  780. *
  781. * The module may add elements to $node->content prior to rendering. This hook
  782. * will be called after hook_view(). The structure of $node->content is a
  783. * renderable array as expected by drupal_render().
  784. *
  785. * When $view_mode is 'rss', modules can also add extra RSS elements and
  786. * namespaces to $node->rss_elements and $node->rss_namespaces respectively for
  787. * the RSS item generated for this node.
  788. * For details on how this is used, see node_feed().
  789. *
  790. * @see blog_node_view()
  791. * @see forum_node_view()
  792. * @see comment_node_view()
  793. *
  794. * @param $node
  795. * The node that is being assembled for rendering.
  796. * @param $view_mode
  797. * The $view_mode parameter from node_view().
  798. * @param $langcode
  799. * The language code used for rendering.
  800. *
  801. * @see hook_entity_view()
  802. *
  803. * @ingroup node_api_hooks
  804. */
  805. function hook_node_view($node, $view_mode, $langcode) {
  806. $node->content['my_additional_field'] = array(
  807. '#markup' => $additional_field,
  808. '#weight' => 10,
  809. '#theme' => 'mymodule_my_additional_field',
  810. );
  811. }
  812. /**
  813. * Alter the results of node_view().
  814. *
  815. * This hook is called after the content has been assembled in a structured
  816. * array and may be used for doing processing which requires that the complete
  817. * node content structure has been built.
  818. *
  819. * If the module wishes to act on the rendered HTML of the node rather than the
  820. * structured content array, it may use this hook to add a #post_render
  821. * callback. Alternatively, it could also implement hook_preprocess_node(). See
  822. * drupal_render() and theme() documentation respectively for details.
  823. *
  824. * @param $build
  825. * A renderable array representing the node content.
  826. *
  827. * @see node_view()
  828. * @see hook_entity_view_alter()
  829. *
  830. * @ingroup node_api_hooks
  831. */
  832. function hook_node_view_alter(&$build) {
  833. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
  834. // Change its weight.
  835. $build['an_additional_field']['#weight'] = -10;
  836. }
  837. // Add a #post_render callback to act on the rendered HTML of the node.
  838. $build['#post_render'][] = 'my_module_node_post_render';
  839. }
  840. /**
  841. * Define module-provided node types.
  842. *
  843. * This hook allows a module to define one or more of its own node types. For
  844. * example, the blog module uses it to define a blog node-type named "Blog
  845. * entry." The name and attributes of each desired node type are specified in an
  846. * array returned by the hook.
  847. *
  848. * Only module-provided node types should be defined through this hook. User-
  849. * provided (or 'custom') node types should be defined only in the 'node_type'
  850. * database table, and should be maintained by using the node_type_save() and
  851. * node_type_delete() functions.
  852. *
  853. * @return
  854. * An array of information defining the module's node types. The array
  855. * contains a sub-array for each node type, with the machine-readable type
  856. * name as the key. Each sub-array has up to 10 attributes. Possible
  857. * attributes:
  858. * - name: (required) The human-readable name of the node type.
  859. * - base: (required) The base name for implementations of node-type-specific
  860. * hooks that respond to this node type. Base is usually the name of the
  861. * module or 'node_content', but not always. See
  862. * @link node_api_hooks Node API hooks @endlink for more information.
  863. * - description: (required) A brief description of the node type.
  864. * - help: (optional) Help information shown to the user when creating a node
  865. * of this type.
  866. * - has_title: (optional) A Boolean indicating whether or not this node type
  867. * has a title field.
  868. * - title_label: (optional) The label for the title field of this content
  869. * type.
  870. * - locked: (optional) A Boolean indicating whether the administrator can
  871. * change the machine name of this type. FALSE = changeable (not locked),
  872. * TRUE = unchangeable (locked).
  873. *
  874. * The machine name of a node type should contain only letters, numbers, and
  875. * underscores. Underscores will be converted into hyphens for the purpose of
  876. * constructing URLs.
  877. *
  878. * All attributes of a node type that are defined through this hook (except for
  879. * 'locked') can be edited by a site administrator. This includes the
  880. * machine-readable name of a node type, if 'locked' is set to FALSE.
  881. *
  882. * @ingroup node_api_hooks
  883. */
  884. function hook_node_info() {
  885. return array(
  886. 'blog' => array(
  887. 'name' => t('Blog entry'),
  888. 'base' => 'blog',
  889. 'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
  890. )
  891. );
  892. }
  893. /**
  894. * Provide additional methods of scoring for core search results for nodes.
  895. *
  896. * A node's search score is used to rank it among other nodes matched by the
  897. * search, with the highest-ranked nodes appearing first in the search listing.
  898. *
  899. * For example, a module allowing users to vote on content could expose an
  900. * option to allow search results' rankings to be influenced by the average
  901. * voting score of a node.
  902. *
  903. * All scoring mechanisms are provided as options to site administrators, and
  904. * may be tweaked based on individual sites or disabled altogether if they do
  905. * not make sense. Individual scoring mechanisms, if enabled, are assigned a
  906. * weight from 1 to 10. The weight represents the factor of magnification of
  907. * the ranking mechanism, with higher-weighted ranking mechanisms having more
  908. * influence. In order for the weight system to work, each scoring mechanism
  909. * must return a value between 0 and 1 for every node. That value is then
  910. * multiplied by the administrator-assigned weight for the ranking mechanism,
  911. * and then the weighted scores from all ranking mechanisms are added, which
  912. * brings about the same result as a weighted average.
  913. *
  914. * @return
  915. * An associative array of ranking data. The keys should be strings,
  916. * corresponding to the internal name of the ranking mechanism, such as
  917. * 'recent', or 'comments'. The values should be arrays themselves, with the
  918. * following keys available:
  919. * - title: (required) The human readable name of the ranking mechanism.
  920. * - join: (optional) An array with information to join any additional
  921. * necessary table. This is not necessary if the table required is already
  922. * joined to by the base query, such as for the {node} table. Other tables
  923. * should use the full table name as an alias to avoid naming collisions.
  924. * - score: (required) The part of a query string to calculate the score for
  925. * the ranking mechanism based on values in the database. This does not need
  926. * to be wrapped in parentheses, as it will be done automatically; it also
  927. * does not need to take the weighted system into account, as it will be
  928. * done automatically. It does, however, need to calculate a decimal between
  929. * 0 and 1; be careful not to cast the entire score to an integer by
  930. * inadvertently introducing a variable argument.
  931. * - arguments: (optional) If any arguments are required for the score, they
  932. * can be specified in an array here.
  933. *
  934. * @ingroup node_api_hooks
  935. */
  936. function hook_ranking() {
  937. // If voting is disabled, we can avoid returning the array, no hard feelings.
  938. if (variable_get('vote_node_enabled', TRUE)) {
  939. return array(
  940. 'vote_average' => array(
  941. 'title' => t('Average vote'),
  942. // Note that we use i.sid, the search index's search item id, rather than
  943. // n.nid.
  944. 'join' => array(
  945. 'type' => 'LEFT',
  946. 'table' => 'vote_node_data',
  947. 'alias' => 'vote_node_data',
  948. 'on' => 'vote_node_data.nid = i.sid',
  949. ),
  950. // The highest possible score should be 1, and the lowest possible score,
  951. // always 0, should be 0.
  952. 'score' => 'vote_node_data.average / CAST(%f AS DECIMAL)',
  953. // Pass in the highest possible voting score as a decimal argument.
  954. 'arguments' => array(variable_get('vote_score_max', 5)),
  955. ),
  956. );
  957. }
  958. }
  959. /**
  960. * Respond to node type creation.
  961. *
  962. * This hook is invoked from node_type_save() after the node type is added to
  963. * the database.
  964. *
  965. * @param $info
  966. * The node type object that is being created.
  967. */
  968. function hook_node_type_insert($info) {
  969. drupal_set_message(t('You have just created a content type with a machine name %type.', array('%type' => $info->type)));
  970. }
  971. /**
  972. * Respond to node type updates.
  973. *
  974. * This hook is invoked from node_type_save() after the node type is updated in
  975. * the database.
  976. *
  977. * @param $info
  978. * The node type object that is being updated.
  979. */
  980. function hook_node_type_update($info) {
  981. if (!empty($info->old_type) && $info->old_type != $info->type) {
  982. $setting = variable_get('comment_' . $info->old_type, COMMENT_NODE_OPEN);
  983. variable_del('comment_' . $info->old_type);
  984. variable_set('comment_' . $info->type, $setting);
  985. }
  986. }
  987. /**
  988. * Respond to node type deletion.
  989. *
  990. * This hook is invoked from node_type_delete() after the node type is removed
  991. * from the database.
  992. *
  993. * @param $info
  994. * The node type object that is being deleted.
  995. */
  996. function hook_node_type_delete($info) {
  997. variable_del('comment_' . $info->type);
  998. }
  999. /**
  1000. * Respond to node deletion.
  1001. *
  1002. * This is a node-type-specific hook, which is invoked only for the node type
  1003. * being affected. See
  1004. * @link node_api_hooks Node API hooks @endlink for more information.
  1005. *
  1006. * Use hook_node_delete() to respond to node deletion of all node types.
  1007. *
  1008. * This hook is invoked from node_delete_multiple() before hook_node_delete()
  1009. * is invoked and before field_attach_delete() is called.
  1010. *
  1011. * Note that when this hook is invoked, the changes have not yet been written
  1012. * to the database, because a database transaction is still in progress. The
  1013. * transaction is not finalized until the delete operation is entirely
  1014. * completed and node_delete_multiple() goes out of scope. You should not rely
  1015. * on data in the database at this time as it is not updated yet. You should
  1016. * also note that any write/update database queries executed from this hook are
  1017. * also not committed immediately. Check node_delete_multiple() and
  1018. * db_transaction() for more info.
  1019. *
  1020. * @param $node
  1021. * The node that is being deleted.
  1022. *
  1023. * @ingroup node_api_hooks
  1024. */
  1025. function hook_delete($node) {
  1026. db_delete('mytable')
  1027. ->condition('nid', $node->nid)
  1028. ->execute();
  1029. }
  1030. /**
  1031. * Act on a node object about to be shown on the add/edit form.
  1032. *
  1033. * This is a node-type-specific hook, which is invoked only for the node type
  1034. * being affected. See
  1035. * @link node_api_hooks Node API hooks @endlink for more information.
  1036. *
  1037. * Use hook_node_prepare() to respond to node preparation of all node types.
  1038. *
  1039. * This hook is invoked from node_object_prepare() before the general
  1040. * hook_node_prepare() is invoked.
  1041. *
  1042. * @param $node
  1043. * The node that is about to be shown on the add/edit form.
  1044. *
  1045. * @ingroup node_api_hooks
  1046. */
  1047. function hook_prepare($node) {
  1048. if (!isset($node->mymodule_value)) {
  1049. $node->mymodule_value = 'foo';
  1050. }
  1051. }
  1052. /**
  1053. * Display a node editing form.
  1054. *
  1055. * This is a node-type-specific hook, which is invoked only for the node type
  1056. * being affected. See
  1057. * @link node_api_hooks Node API hooks @endlink for more information.
  1058. *
  1059. * Use hook_form_BASE_FORM_ID_alter(), with base form ID 'node_form', to alter
  1060. * node forms for all node types.
  1061. *
  1062. * This hook, implemented by node modules, is called to retrieve the form
  1063. * that is displayed to create or edit a node. This form is displayed at path
  1064. * node/add/[node type] or node/[node ID]/edit.
  1065. *
  1066. * The submit and preview buttons, administrative and display controls, and
  1067. * sections added by other modules (such as path settings, menu settings,
  1068. * comment settings, and fields managed by the Field UI module) are
  1069. * displayed automatically by the node module. This hook just needs to
  1070. * return the node title and form editing fields specific to the node type.
  1071. *
  1072. * @param $node
  1073. * The node being added or edited.
  1074. * @param $form_state
  1075. * The form state array.
  1076. *
  1077. * @return
  1078. * An array containing the title and any custom form elements to be displayed
  1079. * in the node editing form.
  1080. *
  1081. * @ingroup node_api_hooks
  1082. */
  1083. function hook_form($node, &$form_state) {
  1084. $type = node_type_get_type($node);
  1085. $form['title'] = array(
  1086. '#type' => 'textfield',
  1087. '#title' => check_plain($type->title_label),
  1088. '#default_value' => !empty($node->title) ? $node->title : '',
  1089. '#required' => TRUE, '#weight' => -5
  1090. );
  1091. $form['field1'] = array(
  1092. '#type' => 'textfield',
  1093. '#title' => t('Custom field'),
  1094. '#default_value' => $node->field1,
  1095. '#maxlength' => 127,
  1096. );
  1097. $form['selectbox'] = array(
  1098. '#type' => 'select',
  1099. '#title' => t('Select box'),
  1100. '#default_value' => $node->selectbox,
  1101. '#options' => array(
  1102. 1 => 'Option A',
  1103. 2 => 'Option B',
  1104. 3 => 'Option C',
  1105. ),
  1106. '#description' => t('Choose an option.'),
  1107. );
  1108. return $form;
  1109. }
  1110. /**
  1111. * Respond to creation of a new node.
  1112. *
  1113. * This is a node-type-specific hook, which is invoked only for the node type
  1114. * being affected. See
  1115. * @link node_api_hooks Node API hooks @endlink for more information.
  1116. *
  1117. * Use hook_node_insert() to respond to node insertion of all node types.
  1118. *
  1119. * This hook is invoked from node_save() after the node is inserted into the
  1120. * node table in the database, before field_attach_insert() is called, and
  1121. * before hook_node_insert() is invoked.
  1122. *
  1123. * @param $node
  1124. * The node that is being created.
  1125. *
  1126. * @ingroup node_api_hooks
  1127. */
  1128. function hook_insert($node) {
  1129. db_insert('mytable')
  1130. ->fields(array(
  1131. 'nid' => $node->nid,
  1132. 'extra' => $node->extra,
  1133. ))
  1134. ->execute();
  1135. }
  1136. /**
  1137. * Act on nodes being loaded from the database.
  1138. *
  1139. * This is a node-type-specific hook, which is invoked only for the node type
  1140. * being affected. See
  1141. * @link node_api_hooks Node API hooks @endlink for more information.
  1142. *
  1143. * Use hook_node_load() to respond to node load of all node types.
  1144. *
  1145. * This hook is invoked during node loading, which is handled by entity_load(),
  1146. * via classes NodeController and DrupalDefaultEntityController. After the node
  1147. * information is read from the database or the entity cache, hook_load() is
  1148. * invoked on the node's content type module, then field_attach_node_revision()
  1149. * or field_attach_load() is called, then hook_entity_load() is invoked on all
  1150. * implementing modules, and finally hook_node_load() is invoked on all
  1151. * implementing modules.
  1152. *
  1153. * This hook should only be used to add information that is not in the node or
  1154. * node revisions table, not to replace information that is in these tables
  1155. * (which could interfere with the entity cache). For performance reasons,
  1156. * information for all available nodes should be loaded in a single query where
  1157. * possible.
  1158. *
  1159. * @param $nodes
  1160. * An array of the nodes being loaded, keyed by nid.
  1161. *
  1162. * For a detailed usage example, see node_example.module.
  1163. *
  1164. * @ingroup node_api_hooks
  1165. */
  1166. function hook_load($nodes) {
  1167. $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
  1168. foreach ($result as $record) {
  1169. $nodes[$record->nid]->foo = $record->foo;
  1170. }
  1171. }
  1172. /**
  1173. * Respond to updates to a node.
  1174. *
  1175. * This is a node-type-specific hook, which is invoked only for the node type
  1176. * being affected. See
  1177. * @link node_api_hooks Node API hooks @endlink for more information.
  1178. *
  1179. * Use hook_node_update() to respond to node update of all node types.
  1180. *
  1181. * This hook is invoked from node_save() after the node is updated in the
  1182. * node table in the database, before field_attach_update() is called, and
  1183. * before hook_node_update() is invoked.
  1184. *
  1185. * @param $node
  1186. * The node that is being updated.
  1187. *
  1188. * @ingroup node_api_hooks
  1189. */
  1190. function hook_update($node) {
  1191. db_update('mytable')
  1192. ->fields(array('extra' => $node->extra))
  1193. ->condition('nid', $node->nid)
  1194. ->execute();
  1195. }
  1196. /**
  1197. * Perform node validation before a node is created or updated.
  1198. *
  1199. * This is a node-type-specific hook, which is invoked only for the node type
  1200. * being affected. See
  1201. * @link node_api_hooks Node API hooks @endlink for more information.
  1202. *
  1203. * Use hook_node_validate() to respond to node validation of all node types.
  1204. *
  1205. * This hook is invoked from node_validate(), after a user has finished
  1206. * editing the node and is previewing or submitting it. It is invoked at the end
  1207. * of all the standard validation steps, and before hook_node_validate() is
  1208. * invoked.
  1209. *
  1210. * To indicate a validation error, use form_set_error().
  1211. *
  1212. * Note: Changes made to the $node object within your hook implementation will
  1213. * have no effect. The preferred method to change a node's content is to use
  1214. * hook_node_presave() instead.
  1215. *
  1216. * @param $node
  1217. * The node being validated.
  1218. * @param $form
  1219. * The form being used to edit the node.
  1220. * @param $form_state
  1221. * The form state array.
  1222. *
  1223. * @ingroup node_api_hooks
  1224. */
  1225. function hook_validate($node, $form, &$form_state) {
  1226. if (isset($node->end) && isset($node->start)) {
  1227. if ($node->start > $node->end) {
  1228. form_set_error('time', t('An event may not end before it starts.'));
  1229. }
  1230. }
  1231. }
  1232. /**
  1233. * Display a node.
  1234. *
  1235. * This is a node-type-specific hook, which is invoked only for the node type
  1236. * being affected. See
  1237. * @link node_api_hooks Node API hooks @endlink for more information.
  1238. *
  1239. * Use hook_node_view() to respond to node view of all node types.
  1240. *
  1241. * This hook is invoked during node viewing after the node is fully loaded, so
  1242. * that the node type module can define a custom method for display, or add to
  1243. * the default display.
  1244. *
  1245. * @param $node
  1246. * The node to be displayed, as returned by node_load().
  1247. * @param $view_mode
  1248. * View mode, e.g. 'full', 'teaser', ...
  1249. * @param $langcode
  1250. * (optional) A language code to use for rendering. Defaults to the global
  1251. * content language of the current request.
  1252. *
  1253. * @return
  1254. * The passed $node parameter should be modified as necessary and returned so
  1255. * it can be properly presented. Nodes are prepared for display by assembling
  1256. * a structured array, formatted as in the Form API, in $node->content. As
  1257. * with Form API arrays, the #weight property can be used to control the
  1258. * relative positions of added elements. After this hook is invoked,
  1259. * node_view() calls field_attach_view() to add field views to $node->content,
  1260. * and then invokes hook_node_view() and hook_node_view_alter(), so if you
  1261. * want to affect the final view of the node, you might consider implementing
  1262. * one of these hooks instead.
  1263. *
  1264. * @ingroup node_api_hooks
  1265. */
  1266. function hook_view($node, $view_mode, $langcode = NULL) {
  1267. if ($view_mode == 'full' && node_is_page($node)) {
  1268. $breadcrumb = array();
  1269. $breadcrumb[] = l(t('Home'), NULL);
  1270. $breadcrumb[] = l(t('Example'), 'example');
  1271. $breadcrumb[] = l($node->field1, 'example/' . $node->field1);
  1272. drupal_set_breadcrumb($breadcrumb);
  1273. }
  1274. $node->content['myfield'] = array(
  1275. '#markup' => theme('mymodule_myfield', $node->myfield),
  1276. '#weight' => 1,
  1277. );
  1278. return $node;
  1279. }
  1280. /**
  1281. * @} End of "addtogroup hooks".
  1282. */