node.api.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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
  13. * module 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: These hooks are only invoked on the primary
  19. * module, using the "base" return component of hook_node_info() as the
  20. * function prefix. For example, poll.module defines the base for the Poll
  21. * content type as "poll", so during creation of a poll node, hook_insert() is
  22. * only invoked by calling poll_insert().
  23. * - All-module hooks: This set of hooks is invoked on all implementing
  24. * modules, to allow other modules to modify what the primary node module is
  25. * doing. For example, hook_node_insert() is invoked on all modules when
  26. * creating a poll node.
  27. * - Field hooks: Hooks related to the fields attached to the node. These are
  28. * invoked from the field operations functions described below, and can be
  29. * either field-type-specific or all-module hooks.
  30. * - Entity hooks: Generic hooks for "entity" operations. These are always
  31. * invoked on all modules.
  32. *
  33. * Here is a list of the node and entity hooks that are invoked, field
  34. * operations, and other steps that take place during node operations:
  35. * - Creating a new node (calling node_save() on a new node):
  36. * - field_attach_presave()
  37. * - hook_node_presave() (all)
  38. * - hook_entity_presave() (all)
  39. * - Node and revision records are written to the database
  40. * - hook_insert() (node-type-specific)
  41. * - field_attach_insert()
  42. * - hook_node_insert() (all)
  43. * - hook_entity_insert() (all)
  44. * - hook_node_access_records() (all)
  45. * - hook_node_access_records_alter() (all)
  46. * - Updating an existing node (calling node_save() on an existing node):
  47. * - field_attach_presave()
  48. * - hook_node_presave() (all)
  49. * - hook_entity_presave() (all)
  50. * - Node and revision records are written to the database
  51. * - hook_update() (node-type-specific)
  52. * - field_attach_update()
  53. * - hook_node_update() (all)
  54. * - hook_entity_update() (all)
  55. * - hook_node_access_records() (all)
  56. * - hook_node_access_records_alter() (all)
  57. * - Loading a node (calling node_load(), node_load_multiple(), or
  58. * entity_load() with $entity_type of 'node'):
  59. * - Node and revision information is read from database.
  60. * - hook_load() (node-type-specific)
  61. * - field_attach_load_revision() and field_attach_load()
  62. * - hook_entity_load() (all)
  63. * - hook_node_load() (all)
  64. * - Viewing a single node (calling node_view() - note that the input to
  65. * node_view() is a loaded node, so the Loading steps above are already
  66. * done):
  67. * - hook_view() (node-type-specific)
  68. * - field_attach_prepare_view()
  69. * - hook_entity_prepare_view() (all)
  70. * - field_attach_view()
  71. * - hook_node_view() (all)
  72. * - hook_entity_view() (all)
  73. * - hook_node_view_alter() (all)
  74. * - hook_entity_view_alter() (all)
  75. * - Viewing multiple nodes (calling node_view_multiple() - note that the input
  76. * to node_view_multiple() is a set of loaded nodes, so the Loading steps
  77. * above are already done):
  78. * - field_attach_prepare_view()
  79. * - hook_entity_prepare_view() (all)
  80. * - hook_view() (node-type-specific)
  81. * - field_attach_view()
  82. * - hook_node_view() (all)
  83. * - hook_entity_view() (all)
  84. * - hook_node_view_alter() (all)
  85. * - hook_entity_view_alter() (all)
  86. * - Deleting a node (calling node_delete() or node_delete_multiple()):
  87. * - Node is loaded (see Loading section above)
  88. * - hook_delete() (node-type-specific)
  89. * - hook_node_delete() (all)
  90. * - hook_entity_delete() (all)
  91. * - field_attach_delete()
  92. * - Node and revision information are deleted from database
  93. * - Deleting a node revision (calling node_revision_delete()):
  94. * - Node is loaded (see Loading section above)
  95. * - Revision information is deleted from database
  96. * - hook_node_revision_delete() (all)
  97. * - field_attach_delete_revision()
  98. * - Preparing a node for editing (calling node_form() - note that if it's
  99. * an existing node, it will already be loaded; see the Loading section
  100. * above):
  101. * - hook_prepare() (node-type-specific)
  102. * - hook_node_prepare() (all)
  103. * - hook_form() (node-type-specific)
  104. * - field_attach_form()
  105. * - Validating a node during editing form submit (calling
  106. * node_form_validate()):
  107. * - hook_validate() (node-type-specific)
  108. * - hook_node_validate() (all)
  109. * - field_attach_form_validate()
  110. * - Searching (calling node_search_execute()):
  111. * - hook_ranking() (all)
  112. * - Query is executed to find matching nodes
  113. * - Resulting node is loaded (see Loading section above)
  114. * - Resulting node is prepared for viewing (see Viewing a single node above)
  115. * - comment_node_update_index() is called.
  116. * - hook_node_search_result() (all)
  117. * - Search indexing (calling node_update_index()):
  118. * - Node is loaded (see Loading section above)
  119. * - Node is prepared for viewing (see Viewing a single node above)
  120. * - hook_node_update_index() (all)
  121. * @}
  122. */
  123. /**
  124. * @addtogroup hooks
  125. * @{
  126. */
  127. /**
  128. * Inform the node access system what permissions the user has.
  129. *
  130. * This hook is for implementation by node access modules. In this hook,
  131. * the module grants a user different "grant IDs" within one or more
  132. * "realms". In hook_node_access_records(), the realms and grant IDs are
  133. * associated with permission to view, edit, and delete individual nodes.
  134. *
  135. * The realms and grant IDs can be arbitrarily defined by your node access
  136. * module; it is common to use role IDs as grant IDs, but that is not
  137. * required. Your module could instead maintain its own list of users, where
  138. * each list has an ID. In that case, the return value of this hook would be
  139. * an array of the list IDs that this user is a member of.
  140. *
  141. * A node access module may implement as many realms as necessary to
  142. * properly define the access privileges for the nodes. Note that the system
  143. * makes no distinction between published and unpublished nodes. It is the
  144. * module's responsibility to provide appropriate realms to limit access to
  145. * unpublished content.
  146. *
  147. * Node access records are stored in the {node_access} table and define which
  148. * grants are required to access a node. There is a special case for the view
  149. * operation -- a record with node ID 0 corresponds to a "view all" grant for
  150. * the realm and grant ID of that record. If there are no node access modules
  151. * enabled, the core node module adds a node ID 0 record for realm 'all'. Node
  152. * access modules can also grant "view all" permission on their custom realms;
  153. * for example, a module could create a record in {node_access} with:
  154. * @code
  155. * $record = array(
  156. * 'nid' => 0,
  157. * 'gid' => 888,
  158. * 'realm' => 'example_realm',
  159. * 'grant_view' => 1,
  160. * 'grant_update' => 0,
  161. * 'grant_delete' => 0,
  162. * );
  163. * drupal_write_record('node_access', $record);
  164. * @endcode
  165. * And then in its hook_node_grants() implementation, it would need to return:
  166. * @code
  167. * if ($op == 'view') {
  168. * $grants['example_realm'] = array(888);
  169. * }
  170. * @endcode
  171. * If you decide to do this, be aware that the node_access_rebuild() function
  172. * will erase any node ID 0 entry when it is called, so you will need to make
  173. * sure to restore your {node_access} record after node_access_rebuild() is
  174. * called.
  175. *
  176. * @see node_access_view_all_nodes()
  177. * @see node_access_rebuild()
  178. *
  179. * @param $account
  180. * The user object whose grants are requested.
  181. * @param $op
  182. * The node operation to be performed, such as "view", "update", or "delete".
  183. *
  184. * @return
  185. * An array whose keys are "realms" of grants, and whose values are arrays of
  186. * the grant IDs within this realm that this user is being granted.
  187. *
  188. * For a detailed example, see node_access_example.module.
  189. *
  190. * @ingroup node_access
  191. */
  192. function hook_node_grants($account, $op) {
  193. if (user_access('access private content', $account)) {
  194. $grants['example'] = array(1);
  195. }
  196. $grants['example_owner'] = array($account->uid);
  197. return $grants;
  198. }
  199. /**
  200. * Set permissions for a node to be written to the database.
  201. *
  202. * When a node is saved, a module implementing hook_node_access_records() will
  203. * be asked if it is interested in the access permissions for a node. If it is
  204. * interested, it must respond with an array of permissions arrays for that
  205. * node.
  206. *
  207. * Node access grants apply regardless of the published or unpublished status
  208. * of the node. Implementations must make sure not to grant access to
  209. * unpublished nodes if they don't want to change the standard access control
  210. * behavior. Your module may need to create a separate access realm to handle
  211. * access to unpublished nodes.
  212. *
  213. * Note that the grant values in the return value from your hook must be
  214. * integers and not boolean TRUE and FALSE.
  215. *
  216. * Each permissions item in the array is an array with the following elements:
  217. * - 'realm': The name of a realm that the module has defined in
  218. * hook_node_grants().
  219. * - 'gid': A 'grant ID' from hook_node_grants().
  220. * - 'grant_view': If set to 1 a user that has been identified as a member
  221. * of this gid within this realm can view this node. This should usually be
  222. * set to $node->status. Failure to do so may expose unpublished content
  223. * to some users.
  224. * - 'grant_update': If set to 1 a user that has been identified as a member
  225. * of this gid within this realm can edit this node.
  226. * - 'grant_delete': If set to 1 a user that has been identified as a member
  227. * of this gid within this realm can delete this node.
  228. * - 'priority': If multiple modules seek to set permissions on a node, the
  229. * realms that have the highest priority will win out, and realms with a lower
  230. * priority will not be written. If there is any doubt, it is best to
  231. * leave this 0.
  232. *
  233. *
  234. * When an implementation is interested in a node but want to deny access to
  235. * everyone, it may return a "deny all" grant:
  236. *
  237. * @code
  238. * $grants[] = array(
  239. * 'realm' => 'all',
  240. * 'gid' => 0,
  241. * 'grant_view' => 0,
  242. * 'grant_update' => 0,
  243. * 'grant_delete' => 0,
  244. * 'priority' => 1,
  245. * );
  246. * @endcode
  247. *
  248. * Setting the priority should cancel out other grants. In the case of a
  249. * conflict between modules, it is safer to use hook_node_access_records_alter()
  250. * to return only the deny grant.
  251. *
  252. * Note: a deny all grant is not written to the database; denies are implicit.
  253. *
  254. * @see node_access_write_grants()
  255. *
  256. * @param $node
  257. * The node that has just been saved.
  258. *
  259. * @return
  260. * An array of grants as defined above.
  261. *
  262. * @ingroup node_access
  263. */
  264. function hook_node_access_records($node) {
  265. // We only care about the node if it has been marked private. If not, it is
  266. // treated just like any other node and we completely ignore it.
  267. if ($node->private) {
  268. $grants = array();
  269. // Only published nodes should be viewable to all users. If we allow access
  270. // blindly here, then all users could view an unpublished node.
  271. if ($node->status) {
  272. $grants[] = array(
  273. 'realm' => 'example',
  274. 'gid' => 1,
  275. 'grant_view' => 1,
  276. 'grant_update' => 0,
  277. 'grant_delete' => 0,
  278. 'priority' => 0,
  279. );
  280. }
  281. // For the example_author array, the GID is equivalent to a UID, which
  282. // means there are many groups of just 1 user.
  283. // Note that an author can always view his or her nodes, even if they
  284. // have status unpublished.
  285. $grants[] = array(
  286. 'realm' => 'example_author',
  287. 'gid' => $node->uid,
  288. 'grant_view' => 1,
  289. 'grant_update' => 1,
  290. 'grant_delete' => 1,
  291. 'priority' => 0,
  292. );
  293. return $grants;
  294. }
  295. }
  296. /**
  297. * Alter permissions for a node before it is written to the database.
  298. *
  299. * Node access modules establish rules for user access to content. Node access
  300. * records are stored in the {node_access} table and define which permissions
  301. * are required to access a node. This hook is invoked after node access modules
  302. * returned their requirements via hook_node_access_records(); doing so allows
  303. * modules to modify the $grants array by reference before it is stored, so
  304. * custom or advanced business logic can be applied.
  305. *
  306. * @see hook_node_access_records()
  307. *
  308. * Upon viewing, editing or deleting a node, hook_node_grants() builds a
  309. * permissions array that is compared against the stored access records. The
  310. * user must have one or more matching permissions in order to complete the
  311. * requested operation.
  312. *
  313. * A module may deny all access to a node by setting $grants to an empty array.
  314. *
  315. * @see hook_node_grants()
  316. * @see hook_node_grants_alter()
  317. *
  318. * @param $grants
  319. * The $grants array returned by hook_node_access_records().
  320. * @param $node
  321. * The node for which the grants were acquired.
  322. *
  323. * The preferred use of this hook is in a module that bridges multiple node
  324. * access modules with a configurable behavior, as shown in the example with the
  325. * 'is_preview' field.
  326. *
  327. * @ingroup node_access
  328. */
  329. function hook_node_access_records_alter(&$grants, $node) {
  330. // Our module allows editors to mark specific articles with the 'is_preview'
  331. // field. If the node being saved has a TRUE value for that field, then only
  332. // our grants are retained, and other grants are removed. Doing so ensures
  333. // that our rules are enforced no matter what priority other grants are given.
  334. if ($node->is_preview) {
  335. // Our module grants are set in $grants['example'].
  336. $temp = $grants['example'];
  337. // Now remove all module grants but our own.
  338. $grants = array('example' => $temp);
  339. }
  340. }
  341. /**
  342. * Alter user access rules when trying to view, edit or delete a node.
  343. *
  344. * Node access modules establish rules for user access to content.
  345. * hook_node_grants() defines permissions for a user to view, edit or
  346. * delete nodes by building a $grants array that indicates the permissions
  347. * assigned to the user by each node access module. This hook is called to allow
  348. * modules to modify the $grants array by reference, so the interaction of
  349. * multiple node access modules can be altered or advanced business logic can be
  350. * applied.
  351. *
  352. * @see hook_node_grants()
  353. *
  354. * The resulting grants are then checked against the records stored in the
  355. * {node_access} table to determine if the operation may be completed.
  356. *
  357. * A module may deny all access to a user by setting $grants to an empty array.
  358. *
  359. * @see hook_node_access_records()
  360. * @see hook_node_access_records_alter()
  361. *
  362. * @param $grants
  363. * The $grants array returned by hook_node_grants().
  364. * @param $account
  365. * The user account requesting access to content.
  366. * @param $op
  367. * The operation being performed, 'view', 'update' or 'delete'.
  368. *
  369. * Developers may use this hook to either add additional grants to a user
  370. * or to remove existing grants. These rules are typically based on either the
  371. * permissions assigned to a user role, or specific attributes of a user
  372. * account.
  373. *
  374. * @ingroup node_access
  375. */
  376. function hook_node_grants_alter(&$grants, $account, $op) {
  377. // Our sample module never allows certain roles to edit or delete
  378. // content. Since some other node access modules might allow this
  379. // permission, we expressly remove it by returning an empty $grants
  380. // array for roles specified in our variable setting.
  381. // Get our list of banned roles.
  382. $restricted = variable_get('example_restricted_roles', array());
  383. if ($op != 'view' && !empty($restricted)) {
  384. // Now check the roles for this account against the restrictions.
  385. foreach ($restricted as $role_id) {
  386. if (isset($account->roles[$role_id])) {
  387. $grants = array();
  388. }
  389. }
  390. }
  391. }
  392. /**
  393. * Add mass node operations.
  394. *
  395. * This hook enables modules to inject custom operations into the mass
  396. * operations dropdown found at admin/content, by associating a callback
  397. * function with the operation, which is called when the form is submitted. The
  398. * callback function receives one initial argument, which is an array of the
  399. * checked nodes.
  400. *
  401. * @return
  402. * An array of operations. Each operation is an associative array that may
  403. * contain the following key-value pairs:
  404. * - 'label': Required. The label for the operation, displayed in the dropdown
  405. * menu.
  406. * - 'callback': Required. The function to call for the operation.
  407. * - 'callback arguments': Optional. An array of additional arguments to pass
  408. * to the callback function.
  409. */
  410. function hook_node_operations() {
  411. $operations = array(
  412. 'publish' => array(
  413. 'label' => t('Publish selected content'),
  414. 'callback' => 'node_mass_update',
  415. 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED)),
  416. ),
  417. 'unpublish' => array(
  418. 'label' => t('Unpublish selected content'),
  419. 'callback' => 'node_mass_update',
  420. 'callback arguments' => array('updates' => array('status' => NODE_NOT_PUBLISHED)),
  421. ),
  422. 'promote' => array(
  423. 'label' => t('Promote selected content to front page'),
  424. 'callback' => 'node_mass_update',
  425. 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED, 'promote' => NODE_PROMOTED)),
  426. ),
  427. 'demote' => array(
  428. 'label' => t('Demote selected content from front page'),
  429. 'callback' => 'node_mass_update',
  430. 'callback arguments' => array('updates' => array('promote' => NODE_NOT_PROMOTED)),
  431. ),
  432. 'sticky' => array(
  433. 'label' => t('Make selected content sticky'),
  434. 'callback' => 'node_mass_update',
  435. 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED, 'sticky' => NODE_STICKY)),
  436. ),
  437. 'unsticky' => array(
  438. 'label' => t('Make selected content not sticky'),
  439. 'callback' => 'node_mass_update',
  440. 'callback arguments' => array('updates' => array('sticky' => NODE_NOT_STICKY)),
  441. ),
  442. 'delete' => array(
  443. 'label' => t('Delete selected content'),
  444. 'callback' => NULL,
  445. ),
  446. );
  447. return $operations;
  448. }
  449. /**
  450. * Respond to node deletion.
  451. *
  452. * This hook is invoked from node_delete_multiple() after the type-specific
  453. * hook_delete() has been invoked, but before hook_entity_delete and
  454. * field_attach_delete() are called, and before the node is removed from the
  455. * node table in the database.
  456. *
  457. * @param $node
  458. * The node that is being deleted.
  459. *
  460. * @ingroup node_api_hooks
  461. */
  462. function hook_node_delete($node) {
  463. db_delete('mytable')
  464. ->condition('nid', $node->nid)
  465. ->execute();
  466. }
  467. /**
  468. * Respond to deletion of a node revision.
  469. *
  470. * This hook is invoked from node_revision_delete() after the revision has been
  471. * removed from the node_revision table, and before
  472. * field_attach_delete_revision() is called.
  473. *
  474. * @param $node
  475. * The node revision (node object) that is being deleted.
  476. *
  477. * @ingroup node_api_hooks
  478. */
  479. function hook_node_revision_delete($node) {
  480. db_delete('mytable')
  481. ->condition('vid', $node->vid)
  482. ->execute();
  483. }
  484. /**
  485. * Respond to creation of a new node.
  486. *
  487. * This hook is invoked from node_save() after the database query that will
  488. * insert the node into the node table is scheduled for execution, after the
  489. * type-specific hook_insert() is invoked, and after field_attach_insert() is
  490. * called.
  491. *
  492. * Note that when this hook is invoked, the changes have not yet been written to
  493. * the database, because a database transaction is still in progress. The
  494. * transaction is not finalized until the save operation is entirely completed
  495. * and node_save() goes out of scope. You should not rely on data in the
  496. * database at this time as it is not updated yet. You should also note that any
  497. * write/update database queries executed from this hook are also not committed
  498. * immediately. Check node_save() and db_transaction() for more info.
  499. *
  500. * @param $node
  501. * The node that is being created.
  502. *
  503. * @ingroup node_api_hooks
  504. */
  505. function hook_node_insert($node) {
  506. db_insert('mytable')
  507. ->fields(array(
  508. 'nid' => $node->nid,
  509. 'extra' => $node->extra,
  510. ))
  511. ->execute();
  512. }
  513. /**
  514. * Act on arbitrary nodes being loaded from the database.
  515. *
  516. * This hook should be used to add information that is not in the node or
  517. * node revisions table, not to replace information that is in these tables
  518. * (which could interfere with the entity cache). For performance reasons,
  519. * information for all available nodes should be loaded in a single query where
  520. * 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,
  560. * so 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
  565. * node types. If your module does not want to actively grant or
  566. * block access, return NODE_ACCESS_IGNORE or simply return nothing.
  567. * Blindly returning FALSE will 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()
  632. * and 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
  640. * with the post information (last updated, author) in the default search
  641. * result 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
  702. * the 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
  733. * the node at 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
  846. * an 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": the human-readable name of the node type. Required.
  859. * - "base": the base string used to construct callbacks corresponding to
  860. * this node type.
  861. * (i.e. if base is defined as example_foo, then example_foo_insert will
  862. * be called when inserting a node of that type). This string is usually
  863. * the name of the module, but not always. Required.
  864. * - "description": a brief description of the node type. Required.
  865. * - "help": help information shown to the user when creating a node of
  866. * this type.. Optional (defaults to '').
  867. * - "has_title": boolean indicating whether or not this node type has a title
  868. * field. Optional (defaults to TRUE).
  869. * - "title_label": the label for the title field of this content type.
  870. * Optional (defaults to 'Title').
  871. * - "locked": boolean indicating whether the administrator can change the
  872. * machine name of this type. FALSE = changeable (not locked),
  873. * TRUE = unchangeable (locked). Optional (defaults to TRUE).
  874. *
  875. * The machine name of a node type should contain only letters, numbers, and
  876. * underscores. Underscores will be converted into hyphens for the purpose of
  877. * constructing URLs.
  878. *
  879. * All attributes of a node type that are defined through this hook (except for
  880. * 'locked') can be edited by a site administrator. This includes the
  881. * machine-readable name of a node type, if 'locked' is set to FALSE.
  882. *
  883. * @ingroup node_api_hooks
  884. */
  885. function hook_node_info() {
  886. return array(
  887. 'blog' => array(
  888. 'name' => t('Blog entry'),
  889. 'base' => 'blog',
  890. 'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
  891. )
  892. );
  893. }
  894. /**
  895. * Provide additional methods of scoring for core search results for nodes.
  896. *
  897. * A node's search score is used to rank it among other nodes matched by the
  898. * search, with the highest-ranked nodes appearing first in the search listing.
  899. *
  900. * For example, a module allowing users to vote on content could expose an
  901. * option to allow search results' rankings to be influenced by the average
  902. * voting score of a node.
  903. *
  904. * All scoring mechanisms are provided as options to site administrators, and
  905. * may be tweaked based on individual sites or disabled altogether if they do
  906. * not make sense. Individual scoring mechanisms, if enabled, are assigned a
  907. * weight from 1 to 10. The weight represents the factor of magnification of
  908. * the ranking mechanism, with higher-weighted ranking mechanisms having more
  909. * influence. In order for the weight system to work, each scoring mechanism
  910. * must return a value between 0 and 1 for every node. That value is then
  911. * multiplied by the administrator-assigned weight for the ranking mechanism,
  912. * and then the weighted scores from all ranking mechanisms are added, which
  913. * brings about the same result as a weighted average.
  914. *
  915. * @return
  916. * An associative array of ranking data. The keys should be strings,
  917. * corresponding to the internal name of the ranking mechanism, such as
  918. * 'recent', or 'comments'. The values should be arrays themselves, with the
  919. * following keys available:
  920. * - "title": the human readable name of the ranking mechanism. Required.
  921. * - "join": part of a query string to join to any additional necessary
  922. * table. This is not necessary if the table required is already joined to
  923. * by the base query, such as for the {node} table. Other tables should use
  924. * the full table name as an alias to avoid naming collisions. Optional.
  925. * - "score": part of a query string to calculate the score for the ranking
  926. * mechanism based on values in the database. This does not need to be
  927. * wrapped in parentheses, as it will be done automatically; it also does
  928. * not need to take the weighted system into account, as it will be done
  929. * automatically. It does, however, need to calculate a decimal between
  930. * 0 and 1; be careful not to cast the entire score to an integer by
  931. * inadvertently introducing a variable argument. Required.
  932. * - "arguments": if any arguments are required for the score, they can be
  933. * specified in an array here.
  934. *
  935. * @ingroup node_api_hooks
  936. */
  937. function hook_ranking() {
  938. // If voting is disabled, we can avoid returning the array, no hard feelings.
  939. if (variable_get('vote_node_enabled', TRUE)) {
  940. return array(
  941. 'vote_average' => array(
  942. 'title' => t('Average vote'),
  943. // Note that we use i.sid, the search index's search item id, rather than
  944. // n.nid.
  945. 'join' => 'LEFT JOIN {vote_node_data} vote_node_data ON vote_node_data.nid = i.sid',
  946. // The highest possible score should be 1, and the lowest possible score,
  947. // always 0, should be 0.
  948. 'score' => 'vote_node_data.average / CAST(%f AS DECIMAL)',
  949. // Pass in the highest possible voting score as a decimal argument.
  950. 'arguments' => array(variable_get('vote_score_max', 5)),
  951. ),
  952. );
  953. }
  954. }
  955. /**
  956. * Respond to node type creation.
  957. *
  958. * This hook is invoked from node_type_save() after the node type is added
  959. * to the database.
  960. *
  961. * @param $info
  962. * The node type object that is being created.
  963. */
  964. function hook_node_type_insert($info) {
  965. drupal_set_message(t('You have just created a content type with a machine name %type.', array('%type' => $info->type)));
  966. }
  967. /**
  968. * Respond to node type updates.
  969. *
  970. * This hook is invoked from node_type_save() after the node type is updated
  971. * in the database.
  972. *
  973. * @param $info
  974. * The node type object that is being updated.
  975. */
  976. function hook_node_type_update($info) {
  977. if (!empty($info->old_type) && $info->old_type != $info->type) {
  978. $setting = variable_get('comment_' . $info->old_type, COMMENT_NODE_OPEN);
  979. variable_del('comment_' . $info->old_type);
  980. variable_set('comment_' . $info->type, $setting);
  981. }
  982. }
  983. /**
  984. * Respond to node type deletion.
  985. *
  986. * This hook is invoked from node_type_delete() after the node type is removed
  987. * from the database.
  988. *
  989. * @param $info
  990. * The node type object that is being deleted.
  991. */
  992. function hook_node_type_delete($info) {
  993. variable_del('comment_' . $info->type);
  994. }
  995. /**
  996. * Respond to node deletion.
  997. *
  998. * This hook is invoked only on the module that defines the node's content type
  999. * (use hook_node_delete() to respond to all node deletions).
  1000. *
  1001. * This hook is invoked from node_delete_multiple() after the node has been
  1002. * removed from the node table in the database, before hook_node_delete() is
  1003. * invoked, and before field_attach_delete() is called.
  1004. *
  1005. * @param $node
  1006. * The node that is being deleted.
  1007. *
  1008. * @ingroup node_api_hooks
  1009. */
  1010. function hook_delete($node) {
  1011. db_delete('mytable')
  1012. ->condition('nid', $node->nid)
  1013. ->execute();
  1014. }
  1015. /**
  1016. * Act on a node object about to be shown on the add/edit form.
  1017. *
  1018. * This hook is invoked only on the module that defines the node's content type
  1019. * (use hook_node_prepare() to act on all node preparations).
  1020. *
  1021. * This hook is invoked from node_object_prepare() before the general
  1022. * hook_node_prepare() is invoked.
  1023. *
  1024. * @param $node
  1025. * The node that is about to be shown on the add/edit form.
  1026. *
  1027. * @ingroup node_api_hooks
  1028. */
  1029. function hook_prepare($node) {
  1030. if ($file = file_check_upload($field_name)) {
  1031. $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE));
  1032. if ($file) {
  1033. if (!image_get_info($file->uri)) {
  1034. form_set_error($field_name, t('Uploaded file is not a valid image'));
  1035. return;
  1036. }
  1037. }
  1038. else {
  1039. return;
  1040. }
  1041. $node->images['_original'] = $file->uri;
  1042. _image_build_derivatives($node, TRUE);
  1043. $node->new_file = TRUE;
  1044. }
  1045. }
  1046. /**
  1047. * Display a node editing form.
  1048. *
  1049. * This hook, implemented by node modules, is called to retrieve the form
  1050. * that is displayed to create or edit a node. This form is displayed at path
  1051. * node/add/[node type] or node/[node ID]/edit.
  1052. *
  1053. * The submit and preview buttons, administrative and display controls, and
  1054. * sections added by other modules (such as path settings, menu settings,
  1055. * comment settings, and fields managed by the Field UI module) are
  1056. * displayed automatically by the node module. This hook just needs to
  1057. * return the node title and form editing fields specific to the node type.
  1058. *
  1059. * @param $node
  1060. * The node being added or edited.
  1061. * @param $form_state
  1062. * The form state array.
  1063. *
  1064. * @return
  1065. * An array containing the title and any custom form elements to be displayed
  1066. * in the node editing form.
  1067. *
  1068. * @ingroup node_api_hooks
  1069. */
  1070. function hook_form($node, &$form_state) {
  1071. $type = node_type_get_type($node);
  1072. $form['title'] = array(
  1073. '#type' => 'textfield',
  1074. '#title' => check_plain($type->title_label),
  1075. '#default_value' => !empty($node->title) ? $node->title : '',
  1076. '#required' => TRUE, '#weight' => -5
  1077. );
  1078. $form['field1'] = array(
  1079. '#type' => 'textfield',
  1080. '#title' => t('Custom field'),
  1081. '#default_value' => $node->field1,
  1082. '#maxlength' => 127,
  1083. );
  1084. $form['selectbox'] = array(
  1085. '#type' => 'select',
  1086. '#title' => t('Select box'),
  1087. '#default_value' => $node->selectbox,
  1088. '#options' => array(
  1089. 1 => 'Option A',
  1090. 2 => 'Option B',
  1091. 3 => 'Option C',
  1092. ),
  1093. '#description' => t('Choose an option.'),
  1094. );
  1095. return $form;
  1096. }
  1097. /**
  1098. * Respond to creation of a new node.
  1099. *
  1100. * This hook is invoked only on the module that defines the node's content type
  1101. * (use hook_node_insert() to act on all node insertions).
  1102. *
  1103. * This hook is invoked from node_save() after the node is inserted into the
  1104. * node table in the database, before field_attach_insert() is called, and
  1105. * before hook_node_insert() is invoked.
  1106. *
  1107. * @param $node
  1108. * The node that is being created.
  1109. *
  1110. * @ingroup node_api_hooks
  1111. */
  1112. function hook_insert($node) {
  1113. db_insert('mytable')
  1114. ->fields(array(
  1115. 'nid' => $node->nid,
  1116. 'extra' => $node->extra,
  1117. ))
  1118. ->execute();
  1119. }
  1120. /**
  1121. * Act on nodes being loaded from the database.
  1122. *
  1123. * This hook is invoked only on the module that defines the node's content type
  1124. * (use hook_node_load() to respond to all node loads).
  1125. *
  1126. * This hook is invoked during node loading, which is handled by entity_load(),
  1127. * via classes NodeController and DrupalDefaultEntityController. After the node
  1128. * information is read from the database or the entity cache, hook_load() is
  1129. * invoked on the node's content type module, then field_attach_node_revision()
  1130. * or field_attach_load() is called, then hook_entity_load() is invoked on all
  1131. * implementing modules, and finally hook_node_load() is invoked on all
  1132. * implementing modules.
  1133. *
  1134. * This hook should only be used to add information that is not in the node or
  1135. * node revisions table, not to replace information that is in these tables
  1136. * (which could interfere with the entity cache). For performance reasons,
  1137. * information for all available nodes should be loaded in a single query where
  1138. * possible.
  1139. *
  1140. * @param $nodes
  1141. * An array of the nodes being loaded, keyed by nid.
  1142. *
  1143. * For a detailed usage example, see node_example.module.
  1144. *
  1145. * @ingroup node_api_hooks
  1146. */
  1147. function hook_load($nodes) {
  1148. $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
  1149. foreach ($result as $record) {
  1150. $nodes[$record->nid]->foo = $record->foo;
  1151. }
  1152. }
  1153. /**
  1154. * Respond to updates to a node.
  1155. *
  1156. * This hook is invoked only on the module that defines the node's content type
  1157. * (use hook_node_update() to act on all node updates).
  1158. *
  1159. * This hook is invoked from node_save() after the node is updated in the
  1160. * node table in the database, before field_attach_update() is called, and
  1161. * before hook_node_update() is invoked.
  1162. *
  1163. * @param $node
  1164. * The node that is being updated.
  1165. *
  1166. * @ingroup node_api_hooks
  1167. */
  1168. function hook_update($node) {
  1169. db_update('mytable')
  1170. ->fields(array('extra' => $node->extra))
  1171. ->condition('nid', $node->nid)
  1172. ->execute();
  1173. }
  1174. /**
  1175. * Perform node validation before a node is created or updated.
  1176. *
  1177. * This hook is invoked only on the module that defines the node's content type
  1178. * (use hook_node_validate() to act on all node validations).
  1179. *
  1180. * This hook is invoked from node_validate(), after a user has finished
  1181. * editing the node and is previewing or submitting it. It is invoked at the end
  1182. * of all the standard validation steps, and before hook_node_validate() is
  1183. * invoked.
  1184. *
  1185. * To indicate a validation error, use form_set_error().
  1186. *
  1187. * Note: Changes made to the $node object within your hook implementation will
  1188. * have no effect. The preferred method to change a node's content is to use
  1189. * hook_node_presave() instead.
  1190. *
  1191. * @param $node
  1192. * The node being validated.
  1193. * @param $form
  1194. * The form being used to edit the node.
  1195. * @param $form_state
  1196. * The form state array.
  1197. *
  1198. * @ingroup node_api_hooks
  1199. */
  1200. function hook_validate($node, $form, &$form_state) {
  1201. if (isset($node->end) && isset($node->start)) {
  1202. if ($node->start > $node->end) {
  1203. form_set_error('time', t('An event may not end before it starts.'));
  1204. }
  1205. }
  1206. }
  1207. /**
  1208. * Display a node.
  1209. *
  1210. * This hook is invoked only on the module that defines the node's content type
  1211. * (use hook_node_view() to act on all node views).
  1212. *
  1213. * This hook is invoked during node viewing after the node is fully loaded,
  1214. * so that the node type module can define a custom method for display, or
  1215. * add to the default display.
  1216. *
  1217. * @param $node
  1218. * The node to be displayed, as returned by node_load().
  1219. * @param $view_mode
  1220. * View mode, e.g. 'full', 'teaser', ...
  1221. * @return
  1222. * $node. The passed $node parameter should be modified as necessary and
  1223. * returned so it can be properly presented. Nodes are prepared for display
  1224. * by assembling a structured array, formatted as in the Form API, in
  1225. * $node->content. As with Form API arrays, the #weight property can be
  1226. * used to control the relative positions of added elements. After this
  1227. * hook is invoked, node_view() calls field_attach_view() to add field
  1228. * views to $node->content, and then invokes hook_node_view() and
  1229. * hook_node_view_alter(), so if you want to affect the final
  1230. * view of the node, you might consider implementing one of these hooks
  1231. * instead.
  1232. *
  1233. * @ingroup node_api_hooks
  1234. */
  1235. function hook_view($node, $view_mode) {
  1236. if ($view_mode == 'full' && node_is_page($node)) {
  1237. $breadcrumb = array();
  1238. $breadcrumb[] = l(t('Home'), NULL);
  1239. $breadcrumb[] = l(t('Example'), 'example');
  1240. $breadcrumb[] = l($node->field1, 'example/' . $node->field1);
  1241. drupal_set_breadcrumb($breadcrumb);
  1242. }
  1243. $node->content['myfield'] = array(
  1244. '#markup' => theme('mymodule_myfield', $node->myfield),
  1245. '#weight' => 1,
  1246. );
  1247. return $node;
  1248. }
  1249. /**
  1250. * @} End of "addtogroup hooks".
  1251. */