node.api.php 47 KB

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