node.api.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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 node is inserted into the
  488. * node table in the database, after the type-specific hook_insert() is invoked,
  489. * and after field_attach_insert() is called.
  490. *
  491. * @param $node
  492. * The node that is being created.
  493. *
  494. * @ingroup node_api_hooks
  495. */
  496. function hook_node_insert($node) {
  497. db_insert('mytable')
  498. ->fields(array(
  499. 'nid' => $node->nid,
  500. 'extra' => $node->extra,
  501. ))
  502. ->execute();
  503. }
  504. /**
  505. * Act on nodes being loaded from the database.
  506. *
  507. * This hook is invoked during node loading, which is handled by entity_load(),
  508. * via classes NodeController and DrupalDefaultEntityController. After the node
  509. * information is read from the database or the entity cache, hook_load() is
  510. * invoked on the node's content type module, then field_attach_node_revision()
  511. * or field_attach_load() is called, then hook_entity_load() is invoked on all
  512. * implementing modules, and finally hook_node_load() is invoked on all
  513. * implementing modules.
  514. *
  515. * This hook should only be used to add information that is not in the node or
  516. * node revisions table, not to replace information that is in these tables
  517. * (which could interfere with the entity cache). For performance reasons,
  518. * information for all available nodes should be loaded in a single query where
  519. * possible.
  520. *
  521. * The $types parameter allows for your module to have an early return (for
  522. * efficiency) if your module only supports certain node types. However, if your
  523. * module defines a content type, you can use hook_load() to respond to loading
  524. * of just that content type.
  525. *
  526. * @param $nodes
  527. * An array of the nodes being loaded, keyed by nid.
  528. * @param $types
  529. * An array containing the types of the nodes.
  530. *
  531. * For a detailed usage example, see nodeapi_example.module.
  532. *
  533. * @ingroup node_api_hooks
  534. */
  535. function hook_node_load($nodes, $types) {
  536. $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)));
  537. foreach ($result as $record) {
  538. $nodes[$record->nid]->foo = $record->foo;
  539. }
  540. }
  541. /**
  542. * Control access to a node.
  543. *
  544. * Modules may implement this hook if they want to have a say in whether or not
  545. * a given user has access to perform a given operation on a node.
  546. *
  547. * The administrative account (user ID #1) always passes any access check,
  548. * so this hook is not called in that case. Users with the "bypass node access"
  549. * permission may always view and edit content through the administrative
  550. * interface.
  551. *
  552. * Note that not all modules will want to influence access on all
  553. * node types. If your module does not want to actively grant or
  554. * block access, return NODE_ACCESS_IGNORE or simply return nothing.
  555. * Blindly returning FALSE will break other node access modules.
  556. *
  557. * Also note that this function isn't called for node listings (e.g., RSS feeds,
  558. * the default home page at path 'node', a recent content block, etc.) See
  559. * @link node_access Node access rights @endlink for a full explanation.
  560. *
  561. * @param $node
  562. * Either a node object or the machine name of the content type on which to
  563. * perform the access check.
  564. * @param $op
  565. * The operation to be performed. Possible values:
  566. * - "create"
  567. * - "delete"
  568. * - "update"
  569. * - "view"
  570. * @param $account
  571. * The user object to perform the access check operation on.
  572. *
  573. * @return
  574. * - NODE_ACCESS_ALLOW: if the operation is to be allowed.
  575. * - NODE_ACCESS_DENY: if the operation is to be denied.
  576. * - NODE_ACCESS_IGNORE: to not affect this operation at all.
  577. *
  578. * @ingroup node_access
  579. */
  580. function hook_node_access($node, $op, $account) {
  581. $type = is_string($node) ? $node : $node->type;
  582. if (in_array($type, node_permissions_get_configured_types())) {
  583. if ($op == 'create' && user_access('create ' . $type . ' content', $account)) {
  584. return NODE_ACCESS_ALLOW;
  585. }
  586. if ($op == 'update') {
  587. if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
  588. return NODE_ACCESS_ALLOW;
  589. }
  590. }
  591. if ($op == 'delete') {
  592. if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
  593. return NODE_ACCESS_ALLOW;
  594. }
  595. }
  596. }
  597. // Returning nothing from this function would have the same effect.
  598. return NODE_ACCESS_IGNORE;
  599. }
  600. /**
  601. * Act on a node object about to be shown on the add/edit form.
  602. *
  603. * This hook is invoked from node_object_prepare() after the type-specific
  604. * hook_prepare() is invoked.
  605. *
  606. * @param $node
  607. * The node that is about to be shown on the add/edit form.
  608. *
  609. * @ingroup node_api_hooks
  610. */
  611. function hook_node_prepare($node) {
  612. if (!isset($node->comment)) {
  613. $node->comment = variable_get("comment_$node->type", COMMENT_NODE_OPEN);
  614. }
  615. }
  616. /**
  617. * Act on a node being displayed as a search result.
  618. *
  619. * This hook is invoked from node_search_execute(), after node_load()
  620. * and node_view() have been called.
  621. *
  622. * @param $node
  623. * The node being displayed in a search result.
  624. *
  625. * @return array
  626. * Extra information to be displayed with search result. This information
  627. * should be presented as an associative array. It will be concatenated
  628. * with the post information (last updated, author) in the default search
  629. * result theming.
  630. *
  631. * @see template_preprocess_search_result()
  632. * @see search-result.tpl.php
  633. *
  634. * @ingroup node_api_hooks
  635. */
  636. function hook_node_search_result($node) {
  637. $comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField();
  638. return array('comment' => format_plural($comments, '1 comment', '@count comments'));
  639. }
  640. /**
  641. * Act on a node being inserted or updated.
  642. *
  643. * This hook is invoked from node_save() before the node is saved to the
  644. * database.
  645. *
  646. * @param $node
  647. * The node that is being inserted or updated.
  648. *
  649. * @ingroup node_api_hooks
  650. */
  651. function hook_node_presave($node) {
  652. if ($node->nid && $node->moderate) {
  653. // Reset votes when node is updated:
  654. $node->score = 0;
  655. $node->users = '';
  656. $node->votes = 0;
  657. }
  658. }
  659. /**
  660. * Respond to updates to a node.
  661. *
  662. * This hook is invoked from node_save() after the node is updated in the node
  663. * table in the database, after the type-specific hook_update() is invoked, and
  664. * after field_attach_update() is called.
  665. *
  666. * @param $node
  667. * The node that is being updated.
  668. *
  669. * @ingroup node_api_hooks
  670. */
  671. function hook_node_update($node) {
  672. db_update('mytable')
  673. ->fields(array('extra' => $node->extra))
  674. ->condition('nid', $node->nid)
  675. ->execute();
  676. }
  677. /**
  678. * Act on a node being indexed for searching.
  679. *
  680. * This hook is invoked during search indexing, after node_load(), and after
  681. * the result of node_view() is added as $node->rendered to the node object.
  682. *
  683. * @param $node
  684. * The node being indexed.
  685. *
  686. * @return string
  687. * Additional node information to be indexed.
  688. *
  689. * @ingroup node_api_hooks
  690. */
  691. function hook_node_update_index($node) {
  692. $text = '';
  693. $comments = db_query('SELECT subject, comment, format FROM {comment} WHERE nid = :nid AND status = :status', array(':nid' => $node->nid, ':status' => COMMENT_PUBLISHED));
  694. foreach ($comments as $comment) {
  695. $text .= '<h2>' . check_plain($comment->subject) . '</h2>' . check_markup($comment->comment, $comment->format, '', TRUE);
  696. }
  697. return $text;
  698. }
  699. /**
  700. * Perform node validation before a node is created or updated.
  701. *
  702. * This hook is invoked from node_validate(), after a user has has finished
  703. * editing the node and is previewing or submitting it. It is invoked at the
  704. * end of all the standard validation steps, and after the type-specific
  705. * hook_validate() is invoked.
  706. *
  707. * To indicate a validation error, use form_set_error().
  708. *
  709. * Note: Changes made to the $node object within your hook implementation will
  710. * have no effect. The preferred method to change a node's content is to use
  711. * hook_node_presave() instead. If it is really necessary to change
  712. * the node at the validate stage, you can use form_set_value().
  713. *
  714. * @param $node
  715. * The node being validated.
  716. * @param $form
  717. * The form being used to edit the node.
  718. * @param $form_state
  719. * The form state array.
  720. *
  721. * @ingroup node_api_hooks
  722. */
  723. function hook_node_validate($node, $form, &$form_state) {
  724. if (isset($node->end) && isset($node->start)) {
  725. if ($node->start > $node->end) {
  726. form_set_error('time', t('An event may not end before it starts.'));
  727. }
  728. }
  729. }
  730. /**
  731. * Act on a node after validated form values have been copied to it.
  732. *
  733. * This hook is invoked when a node form is submitted with either the "Save" or
  734. * "Preview" button, after form values have been copied to the form state's node
  735. * object, but before the node is saved or previewed. It is a chance for modules
  736. * to adjust the node's properties from what they are simply after a copy from
  737. * $form_state['values']. This hook is intended for adjusting non-field-related
  738. * properties. See hook_field_attach_submit() for customizing field-related
  739. * properties.
  740. *
  741. * @param $node
  742. * The node object being updated in response to a form submission.
  743. * @param $form
  744. * The form being used to edit the node.
  745. * @param $form_state
  746. * The form state array.
  747. *
  748. * @ingroup node_api_hooks
  749. */
  750. function hook_node_submit($node, $form, &$form_state) {
  751. // Decompose the selected menu parent option into 'menu_name' and 'plid', if
  752. // the form used the default parent selection widget.
  753. if (!empty($form_state['values']['menu']['parent'])) {
  754. list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']);
  755. }
  756. }
  757. /**
  758. * Act on a node that is being assembled before rendering.
  759. *
  760. * The module may add elements to $node->content prior to rendering. This hook
  761. * will be called after hook_view(). The structure of $node->content is a
  762. * renderable array as expected by drupal_render().
  763. *
  764. * When $view_mode is 'rss', modules can also add extra RSS elements and
  765. * namespaces to $node->rss_elements and $node->rss_namespaces respectively for
  766. * the RSS item generated for this node.
  767. * For details on how this is used, see node_feed().
  768. *
  769. * @see blog_node_view()
  770. * @see forum_node_view()
  771. * @see comment_node_view()
  772. *
  773. * @param $node
  774. * The node that is being assembled for rendering.
  775. * @param $view_mode
  776. * The $view_mode parameter from node_view().
  777. * @param $langcode
  778. * The language code used for rendering.
  779. *
  780. * @see hook_entity_view()
  781. *
  782. * @ingroup node_api_hooks
  783. */
  784. function hook_node_view($node, $view_mode, $langcode) {
  785. $node->content['my_additional_field'] = array(
  786. '#markup' => $additional_field,
  787. '#weight' => 10,
  788. '#theme' => 'mymodule_my_additional_field',
  789. );
  790. }
  791. /**
  792. * Alter the results of node_view().
  793. *
  794. * This hook is called after the content has been assembled in a structured
  795. * array and may be used for doing processing which requires that the complete
  796. * node content structure has been built.
  797. *
  798. * If the module wishes to act on the rendered HTML of the node rather than the
  799. * structured content array, it may use this hook to add a #post_render
  800. * callback. Alternatively, it could also implement hook_preprocess_node(). See
  801. * drupal_render() and theme() documentation respectively for details.
  802. *
  803. * @param $build
  804. * A renderable array representing the node content.
  805. *
  806. * @see node_view()
  807. * @see hook_entity_view_alter()
  808. *
  809. * @ingroup node_api_hooks
  810. */
  811. function hook_node_view_alter(&$build) {
  812. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
  813. // Change its weight.
  814. $build['an_additional_field']['#weight'] = -10;
  815. }
  816. // Add a #post_render callback to act on the rendered HTML of the node.
  817. $build['#post_render'][] = 'my_module_node_post_render';
  818. }
  819. /**
  820. * Define module-provided node types.
  821. *
  822. * This hook allows a module to define one or more of its own node types. For
  823. * example, the blog module uses it to define a blog node-type named "Blog
  824. * entry." The name and attributes of each desired node type are specified in
  825. * an array returned by the hook.
  826. *
  827. * Only module-provided node types should be defined through this hook. User-
  828. * provided (or 'custom') node types should be defined only in the 'node_type'
  829. * database table, and should be maintained by using the node_type_save() and
  830. * node_type_delete() functions.
  831. *
  832. * @return
  833. * An array of information defining the module's node types. The array
  834. * contains a sub-array for each node type, with the machine-readable type
  835. * name as the key. Each sub-array has up to 10 attributes. Possible
  836. * attributes:
  837. * - "name": the human-readable name of the node type. Required.
  838. * - "base": the base string used to construct callbacks corresponding to
  839. * this node type.
  840. * (i.e. if base is defined as example_foo, then example_foo_insert will
  841. * be called when inserting a node of that type). This string is usually
  842. * the name of the module, but not always. Required.
  843. * - "description": a brief description of the node type. Required.
  844. * - "help": help information shown to the user when creating a node of
  845. * this type.. Optional (defaults to '').
  846. * - "has_title": boolean indicating whether or not this node type has a title
  847. * field. Optional (defaults to TRUE).
  848. * - "title_label": the label for the title field of this content type.
  849. * Optional (defaults to 'Title').
  850. * - "locked": boolean indicating whether the administrator can change the
  851. * machine name of this type. FALSE = changeable (not locked),
  852. * TRUE = unchangeable (locked). Optional (defaults to TRUE).
  853. *
  854. * The machine name of a node type should contain only letters, numbers, and
  855. * underscores. Underscores will be converted into hyphens for the purpose of
  856. * constructing URLs.
  857. *
  858. * All attributes of a node type that are defined through this hook (except for
  859. * 'locked') can be edited by a site administrator. This includes the
  860. * machine-readable name of a node type, if 'locked' is set to FALSE.
  861. *
  862. * @ingroup node_api_hooks
  863. */
  864. function hook_node_info() {
  865. return array(
  866. 'blog' => array(
  867. 'name' => t('Blog entry'),
  868. 'base' => 'blog',
  869. 'description' => t('Use for multi-user blogs. Every user gets a personal blog.'),
  870. )
  871. );
  872. }
  873. /**
  874. * Provide additional methods of scoring for core search results for nodes.
  875. *
  876. * A node's search score is used to rank it among other nodes matched by the
  877. * search, with the highest-ranked nodes appearing first in the search listing.
  878. *
  879. * For example, a module allowing users to vote on content could expose an
  880. * option to allow search results' rankings to be influenced by the average
  881. * voting score of a node.
  882. *
  883. * All scoring mechanisms are provided as options to site administrators, and
  884. * may be tweaked based on individual sites or disabled altogether if they do
  885. * not make sense. Individual scoring mechanisms, if enabled, are assigned a
  886. * weight from 1 to 10. The weight represents the factor of magnification of
  887. * the ranking mechanism, with higher-weighted ranking mechanisms having more
  888. * influence. In order for the weight system to work, each scoring mechanism
  889. * must return a value between 0 and 1 for every node. That value is then
  890. * multiplied by the administrator-assigned weight for the ranking mechanism,
  891. * and then the weighted scores from all ranking mechanisms are added, which
  892. * brings about the same result as a weighted average.
  893. *
  894. * @return
  895. * An associative array of ranking data. The keys should be strings,
  896. * corresponding to the internal name of the ranking mechanism, such as
  897. * 'recent', or 'comments'. The values should be arrays themselves, with the
  898. * following keys available:
  899. * - "title": the human readable name of the ranking mechanism. Required.
  900. * - "join": part of a query string to join to any additional necessary
  901. * table. This is not necessary if the table required is already joined to
  902. * by the base query, such as for the {node} table. Other tables should use
  903. * the full table name as an alias to avoid naming collisions. Optional.
  904. * - "score": part of a query string to calculate the score for the ranking
  905. * mechanism based on values in the database. This does not need to be
  906. * wrapped in parentheses, as it will be done automatically; it also does
  907. * not need to take the weighted system into account, as it will be done
  908. * automatically. It does, however, need to calculate a decimal between
  909. * 0 and 1; be careful not to cast the entire score to an integer by
  910. * inadvertently introducing a variable argument. Required.
  911. * - "arguments": if any arguments are required for the score, they can be
  912. * specified in an array here.
  913. *
  914. * @ingroup node_api_hooks
  915. */
  916. function hook_ranking() {
  917. // If voting is disabled, we can avoid returning the array, no hard feelings.
  918. if (variable_get('vote_node_enabled', TRUE)) {
  919. return array(
  920. 'vote_average' => array(
  921. 'title' => t('Average vote'),
  922. // Note that we use i.sid, the search index's search item id, rather than
  923. // n.nid.
  924. 'join' => 'LEFT JOIN {vote_node_data} vote_node_data ON vote_node_data.nid = i.sid',
  925. // The highest possible score should be 1, and the lowest possible score,
  926. // always 0, should be 0.
  927. 'score' => 'vote_node_data.average / CAST(%f AS DECIMAL)',
  928. // Pass in the highest possible voting score as a decimal argument.
  929. 'arguments' => array(variable_get('vote_score_max', 5)),
  930. ),
  931. );
  932. }
  933. }
  934. /**
  935. * Respond to node type creation.
  936. *
  937. * This hook is invoked from node_type_save() after the node type is added
  938. * to the database.
  939. *
  940. * @param $info
  941. * The node type object that is being created.
  942. */
  943. function hook_node_type_insert($info) {
  944. drupal_set_message(t('You have just created a content type with a machine name %type.', array('%type' => $info->type)));
  945. }
  946. /**
  947. * Respond to node type updates.
  948. *
  949. * This hook is invoked from node_type_save() after the node type is updated
  950. * in the database.
  951. *
  952. * @param $info
  953. * The node type object that is being updated.
  954. */
  955. function hook_node_type_update($info) {
  956. if (!empty($info->old_type) && $info->old_type != $info->type) {
  957. $setting = variable_get('comment_' . $info->old_type, COMMENT_NODE_OPEN);
  958. variable_del('comment_' . $info->old_type);
  959. variable_set('comment_' . $info->type, $setting);
  960. }
  961. }
  962. /**
  963. * Respond to node type deletion.
  964. *
  965. * This hook is invoked from node_type_delete() after the node type is removed
  966. * from the database.
  967. *
  968. * @param $info
  969. * The node type object that is being deleted.
  970. */
  971. function hook_node_type_delete($info) {
  972. variable_del('comment_' . $info->type);
  973. }
  974. /**
  975. * Respond to node deletion.
  976. *
  977. * This hook is invoked only on the module that defines the node's content type
  978. * (use hook_node_delete() to respond to all node deletions).
  979. *
  980. * This hook is invoked from node_delete_multiple() after the node has been
  981. * removed from the node table in the database, before hook_node_delete() is
  982. * invoked, and before field_attach_delete() is called.
  983. *
  984. * @param $node
  985. * The node that is being deleted.
  986. *
  987. * @ingroup node_api_hooks
  988. */
  989. function hook_delete($node) {
  990. db_delete('mytable')
  991. ->condition('nid', $node->nid)
  992. ->execute();
  993. }
  994. /**
  995. * Act on a node object about to be shown on the add/edit form.
  996. *
  997. * This hook is invoked only on the module that defines the node's content type
  998. * (use hook_node_prepare() to act on all node preparations).
  999. *
  1000. * This hook is invoked from node_object_prepare() before the general
  1001. * hook_node_prepare() is invoked.
  1002. *
  1003. * @param $node
  1004. * The node that is about to be shown on the add/edit form.
  1005. *
  1006. * @ingroup node_api_hooks
  1007. */
  1008. function hook_prepare($node) {
  1009. if ($file = file_check_upload($field_name)) {
  1010. $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE));
  1011. if ($file) {
  1012. if (!image_get_info($file->uri)) {
  1013. form_set_error($field_name, t('Uploaded file is not a valid image'));
  1014. return;
  1015. }
  1016. }
  1017. else {
  1018. return;
  1019. }
  1020. $node->images['_original'] = $file->uri;
  1021. _image_build_derivatives($node, TRUE);
  1022. $node->new_file = TRUE;
  1023. }
  1024. }
  1025. /**
  1026. * Display a node editing form.
  1027. *
  1028. * This hook, implemented by node modules, is called to retrieve the form
  1029. * that is displayed to create or edit a node. This form is displayed at path
  1030. * node/add/[node type] or node/[node ID]/edit.
  1031. *
  1032. * The submit and preview buttons, administrative and display controls, and
  1033. * sections added by other modules (such as path settings, menu settings,
  1034. * comment settings, and fields managed by the Field UI module) are
  1035. * displayed automatically by the node module. This hook just needs to
  1036. * return the node title and form editing fields specific to the node type.
  1037. *
  1038. * @param $node
  1039. * The node being added or edited.
  1040. * @param $form_state
  1041. * The form state array.
  1042. *
  1043. * @return
  1044. * An array containing the title and any custom form elements to be displayed
  1045. * in the node editing form.
  1046. *
  1047. * @ingroup node_api_hooks
  1048. */
  1049. function hook_form($node, &$form_state) {
  1050. $type = node_type_get_type($node);
  1051. $form['title'] = array(
  1052. '#type' => 'textfield',
  1053. '#title' => check_plain($type->title_label),
  1054. '#default_value' => !empty($node->title) ? $node->title : '',
  1055. '#required' => TRUE, '#weight' => -5
  1056. );
  1057. $form['field1'] = array(
  1058. '#type' => 'textfield',
  1059. '#title' => t('Custom field'),
  1060. '#default_value' => $node->field1,
  1061. '#maxlength' => 127,
  1062. );
  1063. $form['selectbox'] = array(
  1064. '#type' => 'select',
  1065. '#title' => t('Select box'),
  1066. '#default_value' => $node->selectbox,
  1067. '#options' => array(
  1068. 1 => 'Option A',
  1069. 2 => 'Option B',
  1070. 3 => 'Option C',
  1071. ),
  1072. '#description' => t('Choose an option.'),
  1073. );
  1074. return $form;
  1075. }
  1076. /**
  1077. * Respond to creation of a new node.
  1078. *
  1079. * This hook is invoked only on the module that defines the node's content type
  1080. * (use hook_node_insert() to act on all node insertions).
  1081. *
  1082. * This hook is invoked from node_save() after the node is inserted into the
  1083. * node table in the database, before field_attach_insert() is called, and
  1084. * before hook_node_insert() is invoked.
  1085. *
  1086. * @param $node
  1087. * The node that is being created.
  1088. *
  1089. * @ingroup node_api_hooks
  1090. */
  1091. function hook_insert($node) {
  1092. db_insert('mytable')
  1093. ->fields(array(
  1094. 'nid' => $node->nid,
  1095. 'extra' => $node->extra,
  1096. ))
  1097. ->execute();
  1098. }
  1099. /**
  1100. * Act on nodes being loaded from the database.
  1101. *
  1102. * This hook is invoked only on the module that defines the node's content type
  1103. * (use hook_node_load() to respond to all node loads).
  1104. *
  1105. * This hook is invoked during node loading, which is handled by entity_load(),
  1106. * via classes NodeController and DrupalDefaultEntityController. After the node
  1107. * information is read from the database or the entity cache, hook_load() is
  1108. * invoked on the node's content type module, then field_attach_node_revision()
  1109. * or field_attach_load() is called, then hook_entity_load() is invoked on all
  1110. * implementing modules, and finally hook_node_load() is invoked on all
  1111. * implementing modules.
  1112. *
  1113. * This hook should only be used to add information that is not in the node or
  1114. * node revisions table, not to replace information that is in these tables
  1115. * (which could interfere with the entity cache). For performance reasons,
  1116. * information for all available nodes should be loaded in a single query where
  1117. * possible.
  1118. *
  1119. * @param $nodes
  1120. * An array of the nodes being loaded, keyed by nid.
  1121. *
  1122. * For a detailed usage example, see node_example.module.
  1123. *
  1124. * @ingroup node_api_hooks
  1125. */
  1126. function hook_load($nodes) {
  1127. $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
  1128. foreach ($result as $record) {
  1129. $nodes[$record->nid]->foo = $record->foo;
  1130. }
  1131. }
  1132. /**
  1133. * Respond to updates to a node.
  1134. *
  1135. * This hook is invoked only on the module that defines the node's content type
  1136. * (use hook_node_update() to act on all node updates).
  1137. *
  1138. * This hook is invoked from node_save() after the node is updated in the
  1139. * node table in the database, before field_attach_update() is called, and
  1140. * before hook_node_update() is invoked.
  1141. *
  1142. * @param $node
  1143. * The node that is being updated.
  1144. *
  1145. * @ingroup node_api_hooks
  1146. */
  1147. function hook_update($node) {
  1148. db_update('mytable')
  1149. ->fields(array('extra' => $node->extra))
  1150. ->condition('nid', $node->nid)
  1151. ->execute();
  1152. }
  1153. /**
  1154. * Perform node validation before a node is created or updated.
  1155. *
  1156. * This hook is invoked only on the module that defines the node's content type
  1157. * (use hook_node_validate() to act on all node validations).
  1158. *
  1159. * This hook is invoked from node_validate(), after a user has finished
  1160. * editing the node and is previewing or submitting it. It is invoked at the end
  1161. * of all the standard validation steps, and before hook_node_validate() is
  1162. * invoked.
  1163. *
  1164. * To indicate a validation error, use form_set_error().
  1165. *
  1166. * Note: Changes made to the $node object within your hook implementation will
  1167. * have no effect. The preferred method to change a node's content is to use
  1168. * hook_node_presave() instead.
  1169. *
  1170. * @param $node
  1171. * The node being validated.
  1172. * @param $form
  1173. * The form being used to edit the node.
  1174. * @param $form_state
  1175. * The form state array.
  1176. *
  1177. * @ingroup node_api_hooks
  1178. */
  1179. function hook_validate($node, $form, &$form_state) {
  1180. if (isset($node->end) && isset($node->start)) {
  1181. if ($node->start > $node->end) {
  1182. form_set_error('time', t('An event may not end before it starts.'));
  1183. }
  1184. }
  1185. }
  1186. /**
  1187. * Display a node.
  1188. *
  1189. * This hook is invoked only on the module that defines the node's content type
  1190. * (use hook_node_view() to act on all node views).
  1191. *
  1192. * This hook is invoked during node viewing after the node is fully loaded,
  1193. * so that the node type module can define a custom method for display, or
  1194. * add to the default display.
  1195. *
  1196. * @param $node
  1197. * The node to be displayed, as returned by node_load().
  1198. * @param $view_mode
  1199. * View mode, e.g. 'full', 'teaser', ...
  1200. * @return
  1201. * $node. The passed $node parameter should be modified as necessary and
  1202. * returned so it can be properly presented. Nodes are prepared for display
  1203. * by assembling a structured array, formatted as in the Form API, in
  1204. * $node->content. As with Form API arrays, the #weight property can be
  1205. * used to control the relative positions of added elements. After this
  1206. * hook is invoked, node_view() calls field_attach_view() to add field
  1207. * views to $node->content, and then invokes hook_node_view() and
  1208. * hook_node_view_alter(), so if you want to affect the final
  1209. * view of the node, you might consider implementing one of these hooks
  1210. * instead.
  1211. *
  1212. * @ingroup node_api_hooks
  1213. */
  1214. function hook_view($node, $view_mode) {
  1215. if ($view_mode == 'full' && node_is_page($node)) {
  1216. $breadcrumb = array();
  1217. $breadcrumb[] = l(t('Home'), NULL);
  1218. $breadcrumb[] = l(t('Example'), 'example');
  1219. $breadcrumb[] = l($node->field1, 'example/' . $node->field1);
  1220. drupal_set_breadcrumb($breadcrumb);
  1221. }
  1222. $node->content['myfield'] = array(
  1223. '#markup' => theme('mymodule_myfield', $node->myfield),
  1224. '#weight' => 1,
  1225. );
  1226. return $node;
  1227. }
  1228. /**
  1229. * @} End of "addtogroup hooks".
  1230. */