node.api.php 48 KB

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