PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/system/system.api.php

https://bitbucket.org/geolivero/pilou
PHP | 4609 lines | 1366 code | 180 blank | 3063 comment | 123 complexity | 8de916a326175b512b7a937415b4f923 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by Drupal core and the System module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Defines one or more hooks that are exposed by a module.
  12. *
  13. * Normally hooks do not need to be explicitly defined. However, by declaring a
  14. * hook explicitly, a module may define a "group" for it. Modules that implement
  15. * a hook may then place their implementation in either $module.module or in
  16. * $module.$group.inc. If the hook is located in $module.$group.inc, then that
  17. * file will be automatically loaded when needed.
  18. * In general, hooks that are rarely invoked and/or are very large should be
  19. * placed in a separate include file, while hooks that are very short or very
  20. * frequently called should be left in the main module file so that they are
  21. * always available.
  22. *
  23. * @return
  24. * An associative array whose keys are hook names and whose values are an
  25. * associative array containing:
  26. * - group: A string defining the group to which the hook belongs. The module
  27. * system will determine whether a file with the name $module.$group.inc
  28. * exists, and automatically load it when required.
  29. *
  30. * See system_hook_info() for all hook groups defined by Drupal core.
  31. *
  32. * @see hook_hook_info_alter().
  33. */
  34. function hook_hook_info() {
  35. $hooks['token_info'] = array(
  36. 'group' => 'tokens',
  37. );
  38. $hooks['tokens'] = array(
  39. 'group' => 'tokens',
  40. );
  41. return $hooks;
  42. }
  43. /**
  44. * Alter information from hook_hook_info().
  45. *
  46. * @param $hooks
  47. * Information gathered by module_hook_info() from other modules'
  48. * implementations of hook_hook_info(). Alter this array directly.
  49. * See hook_hook_info() for information on what this may contain.
  50. */
  51. function hook_hook_info_alter(&$hooks) {
  52. // Our module wants to completely override the core tokens, so make
  53. // sure the core token hooks are not found.
  54. $hooks['token_info']['group'] = 'mytokens';
  55. $hooks['tokens']['group'] = 'mytokens';
  56. }
  57. /**
  58. * Inform the base system and the Field API about one or more entity types.
  59. *
  60. * Inform the system about one or more entity types (i.e., object types that
  61. * can be loaded via entity_load() and, optionally, to which fields can be
  62. * attached).
  63. *
  64. * @return
  65. * An array whose keys are entity type names and whose values identify
  66. * properties of those types that the system needs to know about:
  67. * - label: The human-readable name of the type.
  68. * - controller class: The name of the class that is used to load the objects.
  69. * The class has to implement the DrupalEntityControllerInterface interface.
  70. * Leave blank to use the DrupalDefaultEntityController implementation.
  71. * - base table: (used by DrupalDefaultEntityController) The name of the
  72. * entity type's base table.
  73. * - revision table: The name of the entity type's revision table (if any).
  74. * - static cache: (used by DrupalDefaultEntityController) FALSE to disable
  75. * static caching of entities during a page request. Defaults to TRUE.
  76. * - field cache: (used by Field API loading and saving of field data) FALSE
  77. * to disable Field API's persistent cache of field data. Only recommended
  78. * if a higher level persistent cache is available for the entity type.
  79. * Defaults to TRUE.
  80. * - load hook: The name of the hook which should be invoked by
  81. * DrupalDefaultEntityController:attachLoad(), for example 'node_load'.
  82. * - uri callback: A function taking an entity as argument and returning the
  83. * uri elements of the entity, e.g. 'path' and 'options'. The actual entity
  84. * uri can be constructed by passing these elements to url().
  85. * - label callback: (optional) A function taking an entity and an entity type
  86. * as arguments and returning the label of the entity. The entity label is
  87. * the main string associated with an entity; for example, the title of a
  88. * node or the subject of a comment. If there is an entity object property
  89. * that defines the label, use the 'label' element of the 'entity keys'
  90. * return value component to provide this information (see below). If more
  91. * complex logic is needed to determine the label of an entity, you can
  92. * instead specify a callback function here, which will be called to
  93. * determine the entity label. See also the entity_label() function, which
  94. * implements this logic.
  95. * - fieldable: Set to TRUE if you want your entity type to accept fields
  96. * being attached to it.
  97. * - translation: An associative array of modules registered as field
  98. * translation handlers. Array keys are the module names, array values
  99. * can be any data structure the module uses to provide field translation.
  100. * Any empty value disallows the module to appear as a translation handler.
  101. * - entity keys: An array describing how the Field API can extract the
  102. * information it needs from the objects of the type. Elements:
  103. * - id: The name of the property that contains the primary id of the
  104. * entity. Every entity object passed to the Field API must have this
  105. * property and its value must be numeric.
  106. * - revision: The name of the property that contains the revision id of
  107. * the entity. The Field API assumes that all revision ids are unique
  108. * across all entities of a type. This entry can be omitted if the
  109. * entities of this type are not versionable.
  110. * - bundle: The name of the property that contains the bundle name for the
  111. * entity. The bundle name defines which set of fields are attached to
  112. * the entity (e.g. what nodes call "content type"). This entry can be
  113. * omitted if this entity type exposes a single bundle (all entities have
  114. * the same collection of fields). The name of this single bundle will be
  115. * the same as the entity type.
  116. * - label: The name of the property that contains the entity label. For
  117. * example, if the entity's label is located in $entity->subject, then
  118. * 'subject' should be specified here. If complex logic is required to
  119. * build the label, a 'label callback' should be defined instead (see
  120. * the 'label callback' section above for details).
  121. * - bundle keys: An array describing how the Field API can extract the
  122. * information it needs from the bundle objects for this type. This entry
  123. * is required if the 'path' provided in the 'bundles'/'admin' section
  124. * identifies the bundle using a named menu placeholder whose loader
  125. * callback returns an object (e.g., $vocabulary for taxonomy terms, or
  126. * $node_type for nodes). If the path does not include the bundle, or the
  127. * bundle is just a string rather than an automatically loaded object, then
  128. * this can be omitted. Elements:
  129. * - bundle: The name of the property of the bundle object that contains
  130. * the name of the bundle object.
  131. * - bundles: An array describing all bundles for this object type. Keys are
  132. * bundles machine names, as found in the objects' 'bundle' property
  133. * (defined in the 'entity keys' entry above). Elements:
  134. * - label: The human-readable name of the bundle.
  135. * - uri callback: Same as the 'uri callback' key documented above for the
  136. * entity type, but for the bundle only. When determining the URI of an
  137. * entity, if a 'uri callback' is defined for both the entity type and
  138. * the bundle, the one for the bundle is used.
  139. * - admin: An array of information that allows Field UI pages to attach
  140. * themselves to the existing administration pages for the bundle.
  141. * Elements:
  142. * - path: the path of the bundle's main administration page, as defined
  143. * in hook_menu(). If the path includes a placeholder for the bundle,
  144. * the 'bundle argument' and 'real path' keys below are required.
  145. * - bundle argument: The position of the bundle placeholder in 'path', if
  146. * any.
  147. * - real path: The actual path (no placeholder) of the bundle's main
  148. * administration page. This will be used to generate links.
  149. * - access callback: As in hook_menu(). 'user_access' will be assumed if
  150. * no value is provided.
  151. * - access arguments: As in hook_menu().
  152. * - view modes: An array describing the view modes for the entity type. View
  153. * modes let entities be displayed differently depending on the context.
  154. * For instance, a node can be displayed differently on its own page
  155. * ('full' mode), on the home page or taxonomy listings ('teaser' mode), or
  156. * in an RSS feed ('rss' mode). Modules taking part in the display of the
  157. * entity (notably the Field API) can adjust their behavior depending on
  158. * the requested view mode. An additional 'default' view mode is available
  159. * for all entity types. This view mode is not intended for actual entity
  160. * display, but holds default display settings. For each available view
  161. * mode, administrators can configure whether it should use its own set of
  162. * field display settings, or just replicate the settings of the 'default'
  163. * view mode, thus reducing the amount of display configurations to keep
  164. * track of. Keys of the array are view mode names. Each view mode is
  165. * described by an array with the following key/value pairs:
  166. * - label: The human-readable name of the view mode
  167. * - custom settings: A boolean specifying whether the view mode should by
  168. * default use its own custom field display settings. If FALSE, entities
  169. * displayed in this view mode will reuse the 'default' display settings
  170. * by default (e.g. right after the module exposing the view mode is
  171. * enabled), but administrators can later use the Field UI to apply custom
  172. * display settings specific to the view mode.
  173. *
  174. * @see entity_load()
  175. * @see hook_entity_info_alter()
  176. */
  177. function hook_entity_info() {
  178. $return = array(
  179. 'node' => array(
  180. 'label' => t('Node'),
  181. 'controller class' => 'NodeController',
  182. 'base table' => 'node',
  183. 'revision table' => 'node_revision',
  184. 'uri callback' => 'node_uri',
  185. 'fieldable' => TRUE,
  186. 'translation' => array(
  187. 'locale' => TRUE,
  188. ),
  189. 'entity keys' => array(
  190. 'id' => 'nid',
  191. 'revision' => 'vid',
  192. 'bundle' => 'type',
  193. ),
  194. 'bundle keys' => array(
  195. 'bundle' => 'type',
  196. ),
  197. 'bundles' => array(),
  198. 'view modes' => array(
  199. 'full' => array(
  200. 'label' => t('Full content'),
  201. 'custom settings' => FALSE,
  202. ),
  203. 'teaser' => array(
  204. 'label' => t('Teaser'),
  205. 'custom settings' => TRUE,
  206. ),
  207. 'rss' => array(
  208. 'label' => t('RSS'),
  209. 'custom settings' => FALSE,
  210. ),
  211. ),
  212. ),
  213. );
  214. // Search integration is provided by node.module, so search-related
  215. // view modes for nodes are defined here and not in search.module.
  216. if (module_exists('search')) {
  217. $return['node']['view modes'] += array(
  218. 'search_index' => array(
  219. 'label' => t('Search index'),
  220. 'custom settings' => FALSE,
  221. ),
  222. 'search_result' => array(
  223. 'label' => t('Search result'),
  224. 'custom settings' => FALSE,
  225. ),
  226. );
  227. }
  228. // Bundles must provide a human readable name so we can create help and error
  229. // messages, and the path to attach Field admin pages to.
  230. foreach (node_type_get_names() as $type => $name) {
  231. $return['node']['bundles'][$type] = array(
  232. 'label' => $name,
  233. 'admin' => array(
  234. 'path' => 'admin/structure/types/manage/%node_type',
  235. 'real path' => 'admin/structure/types/manage/' . str_replace('_', '-', $type),
  236. 'bundle argument' => 4,
  237. 'access arguments' => array('administer content types'),
  238. ),
  239. );
  240. }
  241. return $return;
  242. }
  243. /**
  244. * Alter the entity info.
  245. *
  246. * Modules may implement this hook to alter the information that defines an
  247. * entity. All properties that are available in hook_entity_info() can be
  248. * altered here.
  249. *
  250. * @param $entity_info
  251. * The entity info array, keyed by entity name.
  252. *
  253. * @see hook_entity_info()
  254. */
  255. function hook_entity_info_alter(&$entity_info) {
  256. // Set the controller class for nodes to an alternate implementation of the
  257. // DrupalEntityController interface.
  258. $entity_info['node']['controller class'] = 'MyCustomNodeController';
  259. }
  260. /**
  261. * Act on entities when loaded.
  262. *
  263. * This is a generic load hook called for all entity types loaded via the
  264. * entity API.
  265. *
  266. * @param $entities
  267. * The entities keyed by entity ID.
  268. * @param $type
  269. * The type of entities being loaded (i.e. node, user, comment).
  270. */
  271. function hook_entity_load($entities, $type) {
  272. foreach ($entities as $entity) {
  273. $entity->foo = mymodule_add_something($entity, $type);
  274. }
  275. }
  276. /**
  277. * Act on an entity before it is about to be created or updated.
  278. *
  279. * @param $entity
  280. * The entity object.
  281. * @param $type
  282. * The type of entity being saved (i.e. node, user, comment).
  283. */
  284. function hook_entity_presave($entity, $type) {
  285. $entity->changed = REQUEST_TIME;
  286. }
  287. /**
  288. * Act on entities when inserted.
  289. *
  290. * @param $entity
  291. * The entity object.
  292. * @param $type
  293. * The type of entity being inserted (i.e. node, user, comment).
  294. */
  295. function hook_entity_insert($entity, $type) {
  296. // Insert the new entity into a fictional table of all entities.
  297. $info = entity_get_info($type);
  298. list($id) = entity_extract_ids($type, $entity);
  299. db_insert('example_entity')
  300. ->fields(array(
  301. 'type' => $type,
  302. 'id' => $id,
  303. 'created' => REQUEST_TIME,
  304. 'updated' => REQUEST_TIME,
  305. ))
  306. ->execute();
  307. }
  308. /**
  309. * Act on entities when updated.
  310. *
  311. * @param $entity
  312. * The entity object.
  313. * @param $type
  314. * The type of entity being updated (i.e. node, user, comment).
  315. */
  316. function hook_entity_update($entity, $type) {
  317. // Update the entity's entry in a fictional table of all entities.
  318. $info = entity_get_info($type);
  319. list($id) = entity_extract_ids($type, $entity);
  320. db_update('example_entity')
  321. ->fields(array(
  322. 'updated' => REQUEST_TIME,
  323. ))
  324. ->condition('type', $type)
  325. ->condition('id', $id)
  326. ->execute();
  327. }
  328. /**
  329. * Act on entities when deleted.
  330. *
  331. * @param $entity
  332. * The entity object.
  333. * @param $type
  334. * The type of entity being deleted (i.e. node, user, comment).
  335. */
  336. function hook_entity_delete($entity, $type) {
  337. // Delete the entity's entry from a fictional table of all entities.
  338. $info = entity_get_info($type);
  339. list($id) = entity_extract_ids($type, $entity);
  340. db_delete('example_entity')
  341. ->condition('type', $type)
  342. ->condition('id', $id)
  343. ->execute();
  344. }
  345. /**
  346. * Alter or execute an EntityFieldQuery.
  347. *
  348. * @param EntityFieldQuery $query
  349. * An EntityFieldQuery. One of the most important properties to be changed is
  350. * EntityFieldQuery::executeCallback. If this is set to an existing function,
  351. * this function will get the query as its single argument and its result
  352. * will be the returned as the result of EntityFieldQuery::execute(). This can
  353. * be used to change the behavior of EntityFieldQuery entirely. For example,
  354. * the default implementation can only deal with one field storage engine, but
  355. * it is possible to write a module that can query across field storage
  356. * engines. Also, the default implementation presumes entities are stored in
  357. * SQL, but the execute callback could instead query any other entity storage,
  358. * local or remote.
  359. *
  360. * Note the $query->altered attribute which is TRUE in case the query has
  361. * already been altered once. This happens with cloned queries.
  362. * If there is a pager, then such a cloned query will be executed to count
  363. * all elements. This query can be detected by checking for
  364. * ($query->pager && $query->count), allowing the driver to return 0 from
  365. * the count query and disable the pager.
  366. */
  367. function hook_entity_query_alter($query) {
  368. $query->executeCallback = 'my_module_query_callback';
  369. }
  370. /**
  371. * Act on entities being assembled before rendering.
  372. *
  373. * @param $entity
  374. * The entity object.
  375. * @param $type
  376. * The type of entity being rendered (i.e. node, user, comment).
  377. * @param $view_mode
  378. * The view mode the entity is rendered in.
  379. * @param $langcode
  380. * The language code used for rendering.
  381. *
  382. * The module may add elements to $entity->content prior to rendering. The
  383. * structure of $entity->content is a renderable array as expected by
  384. * drupal_render().
  385. *
  386. * @see hook_entity_view_alter()
  387. * @see hook_comment_view()
  388. * @see hook_node_view()
  389. * @see hook_user_view()
  390. */
  391. function hook_entity_view($entity, $type, $view_mode, $langcode) {
  392. $entity->content['my_additional_field'] = array(
  393. '#markup' => $additional_field,
  394. '#weight' => 10,
  395. '#theme' => 'mymodule_my_additional_field',
  396. );
  397. }
  398. /**
  399. * Alter the results of ENTITY_view().
  400. *
  401. * This hook is called after the content has been assembled in a structured
  402. * array and may be used for doing processing which requires that the complete
  403. * entity content structure has been built.
  404. *
  405. * If a module wishes to act on the rendered HTML of the entity rather than the
  406. * structured content array, it may use this hook to add a #post_render
  407. * callback. Alternatively, it could also implement hook_preprocess_ENTITY().
  408. * See drupal_render() and theme() for details.
  409. *
  410. * @param $build
  411. * A renderable array representing the entity content.
  412. * @param $type
  413. * The type of entity being rendered (i.e. node, user, comment).
  414. *
  415. * @see hook_entity_view()
  416. * @see hook_comment_view_alter()
  417. * @see hook_node_view_alter()
  418. * @see hook_taxonomy_term_view_alter()
  419. * @see hook_user_view_alter()
  420. */
  421. function hook_entity_view_alter(&$build, $type) {
  422. if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
  423. // Change its weight.
  424. $build['an_additional_field']['#weight'] = -10;
  425. // Add a #post_render callback to act on the rendered HTML of the entity.
  426. $build['#post_render'][] = 'my_module_node_post_render';
  427. }
  428. }
  429. /**
  430. * Define administrative paths.
  431. *
  432. * Modules may specify whether or not the paths they define in hook_menu() are
  433. * to be considered administrative. Other modules may use this information to
  434. * display those pages differently (e.g. in a modal overlay, or in a different
  435. * theme).
  436. *
  437. * To change the administrative status of menu items defined in another module's
  438. * hook_menu(), modules should implement hook_admin_paths_alter().
  439. *
  440. * @return
  441. * An associative array. For each item, the key is the path in question, in
  442. * a format acceptable to drupal_match_path(). The value for each item should
  443. * be TRUE (for paths considered administrative) or FALSE (for non-
  444. * administrative paths).
  445. *
  446. * @see hook_menu()
  447. * @see drupal_match_path()
  448. * @see hook_admin_paths_alter()
  449. */
  450. function hook_admin_paths() {
  451. $paths = array(
  452. 'mymodule/*/add' => TRUE,
  453. 'mymodule/*/edit' => TRUE,
  454. );
  455. return $paths;
  456. }
  457. /**
  458. * Redefine administrative paths defined by other modules.
  459. *
  460. * @param $paths
  461. * An associative array of administrative paths, as defined by implementations
  462. * of hook_admin_paths().
  463. *
  464. * @see hook_admin_paths()
  465. */
  466. function hook_admin_paths_alter(&$paths) {
  467. // Treat all user pages as administrative.
  468. $paths['user'] = TRUE;
  469. $paths['user/*'] = TRUE;
  470. // Treat the forum topic node form as a non-administrative page.
  471. $paths['node/add/forum'] = FALSE;
  472. }
  473. /**
  474. * Act on entities as they are being prepared for view.
  475. *
  476. * Allows you to operate on multiple entities as they are being prepared for
  477. * view. Only use this if attaching the data during the entity_load() phase
  478. * is not appropriate, for example when attaching other 'entity' style objects.
  479. *
  480. * @param $entities
  481. * The entities keyed by entity ID.
  482. * @param $type
  483. * The type of entities being loaded (i.e. node, user, comment).
  484. * @param $langcode
  485. * The language to display the entity in.
  486. */
  487. function hook_entity_prepare_view($entities, $type, $langcode) {
  488. // Load a specific node into the user object for later theming.
  489. if ($type == 'user') {
  490. $nodes = mymodule_get_user_nodes(array_keys($entities));
  491. foreach ($entities as $uid => $entity) {
  492. $entity->user_node = $nodes[$uid];
  493. }
  494. }
  495. }
  496. /**
  497. * Perform periodic actions.
  498. *
  499. * Modules that require some commands to be executed periodically can
  500. * implement hook_cron(). The engine will then call the hook whenever a cron
  501. * run happens, as defined by the administrator. Typical tasks managed by
  502. * hook_cron() are database maintenance, backups, recalculation of settings
  503. * or parameters, automated mailing, and retrieving remote data.
  504. *
  505. * Short-running or non-resource-intensive tasks can be executed directly in
  506. * the hook_cron() implementation.
  507. *
  508. * Long-running tasks and tasks that could time out, such as retrieving remote
  509. * data, sending email, and intensive file tasks, should use the queue API
  510. * instead of executing the tasks directly. To do this, first define one or
  511. * more queues via hook_cron_queue_info(). Then, add items that need to be
  512. * processed to the defined queues.
  513. */
  514. function hook_cron() {
  515. // Short-running operation example, not using a queue:
  516. // Delete all expired records since the last cron run.
  517. $expires = variable_get('mymodule_cron_last_run', REQUEST_TIME);
  518. db_delete('mymodule_table')
  519. ->condition('expires', $expires, '>=')
  520. ->execute();
  521. variable_set('mymodule_cron_last_run', REQUEST_TIME);
  522. // Long-running operation example, leveraging a queue:
  523. // Fetch feeds from other sites.
  524. $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh <> :never', array(
  525. ':time' => REQUEST_TIME,
  526. ':never' => AGGREGATOR_CLEAR_NEVER,
  527. ));
  528. $queue = DrupalQueue::get('aggregator_feeds');
  529. foreach ($result as $feed) {
  530. $queue->createItem($feed);
  531. }
  532. }
  533. /**
  534. * Declare queues holding items that need to be run periodically.
  535. *
  536. * While there can be only one hook_cron() process running at the same time,
  537. * there can be any number of processes defined here running. Because of
  538. * this, long running tasks are much better suited for this API. Items queued
  539. * in hook_cron() might be processed in the same cron run if there are not many
  540. * items in the queue, otherwise it might take several requests, which can be
  541. * run in parallel.
  542. *
  543. * @return
  544. * An associative array where the key is the queue name and the value is
  545. * again an associative array. Possible keys are:
  546. * - 'worker callback': The name of the function to call. It will be called
  547. * with one argument, the item created via DrupalQueue::createItem() in
  548. * hook_cron().
  549. * - 'time': (optional) How much time Drupal should spend on calling this
  550. * worker in seconds. Defaults to 15.
  551. *
  552. * @see hook_cron()
  553. * @see hook_cron_queue_info_alter()
  554. */
  555. function hook_cron_queue_info() {
  556. $queues['aggregator_feeds'] = array(
  557. 'worker callback' => 'aggregator_refresh',
  558. 'time' => 60,
  559. );
  560. return $queues;
  561. }
  562. /**
  563. * Alter cron queue information before cron runs.
  564. *
  565. * Called by drupal_cron_run() to allow modules to alter cron queue settings
  566. * before any jobs are processesed.
  567. *
  568. * @param array $queues
  569. * An array of cron queue information.
  570. *
  571. * @see hook_cron_queue_info()
  572. * @see drupal_cron_run()
  573. */
  574. function hook_cron_queue_info_alter(&$queues) {
  575. // This site has many feeds so let's spend 90 seconds on each cron run
  576. // updating feeds instead of the default 60.
  577. $queues['aggregator_feeds']['time'] = 90;
  578. }
  579. /**
  580. * Allows modules to declare their own Forms API element types and specify their
  581. * default values.
  582. *
  583. * This hook allows modules to declare their own form element types and to
  584. * specify their default values. The values returned by this hook will be
  585. * merged with the elements returned by hook_form() implementations and so
  586. * can return defaults for any Form APIs keys in addition to those explicitly
  587. * mentioned below.
  588. *
  589. * Each of the form element types defined by this hook is assumed to have
  590. * a matching theme function, e.g. theme_elementtype(), which should be
  591. * registered with hook_theme() as normal.
  592. *
  593. * For more information about custom element types see the explanation at
  594. * http://drupal.org/node/169815.
  595. *
  596. * @return
  597. * An associative array describing the element types being defined. The array
  598. * contains a sub-array for each element type, with the machine-readable type
  599. * name as the key. Each sub-array has a number of possible attributes:
  600. * - "#input": boolean indicating whether or not this element carries a value
  601. * (even if it's hidden).
  602. * - "#process": array of callback functions taking $element, $form_state,
  603. * and $complete_form.
  604. * - "#after_build": array of callback functions taking $element and $form_state.
  605. * - "#validate": array of callback functions taking $form and $form_state.
  606. * - "#element_validate": array of callback functions taking $element and
  607. * $form_state.
  608. * - "#pre_render": array of callback functions taking $element and $form_state.
  609. * - "#post_render": array of callback functions taking $element and $form_state.
  610. * - "#submit": array of callback functions taking $form and $form_state.
  611. * - "#title_display": optional string indicating if and how #title should be
  612. * displayed, see theme_form_element() and theme_form_element_label().
  613. *
  614. * @see hook_element_info_alter()
  615. * @see system_element_info()
  616. */
  617. function hook_element_info() {
  618. $types['filter_format'] = array(
  619. '#input' => TRUE,
  620. );
  621. return $types;
  622. }
  623. /**
  624. * Alter the element type information returned from modules.
  625. *
  626. * A module may implement this hook in order to alter the element type defaults
  627. * defined by a module.
  628. *
  629. * @param $type
  630. * All element type defaults as collected by hook_element_info().
  631. *
  632. * @see hook_element_info()
  633. */
  634. function hook_element_info_alter(&$type) {
  635. // Decrease the default size of textfields.
  636. if (isset($type['textfield']['#size'])) {
  637. $type['textfield']['#size'] = 40;
  638. }
  639. }
  640. /**
  641. * Perform cleanup tasks.
  642. *
  643. * This hook is run at the end of each page request. It is often used for
  644. * page logging and specialized cleanup. This hook MUST NOT print anything.
  645. *
  646. * Only use this hook if your code must run even for cached page views.
  647. * If you have code which must run once on all non-cached pages, use
  648. * hook_init() instead. That is the usual case. If you implement this hook
  649. * and see an error like 'Call to undefined function', it is likely that
  650. * you are depending on the presence of a module which has not been loaded yet.
  651. * It is not loaded because Drupal is still in bootstrap mode.
  652. *
  653. * @param $destination
  654. * If this hook is invoked as part of a drupal_goto() call, then this argument
  655. * will be a fully-qualified URL that is the destination of the redirect.
  656. */
  657. function hook_exit($destination = NULL) {
  658. db_update('counter')
  659. ->expression('hits', 'hits + 1')
  660. ->condition('type', 1)
  661. ->execute();
  662. }
  663. /**
  664. * Perform necessary alterations to the JavaScript before it is presented on
  665. * the page.
  666. *
  667. * @param $javascript
  668. * An array of all JavaScript being presented on the page.
  669. *
  670. * @see drupal_add_js()
  671. * @see drupal_get_js()
  672. * @see drupal_js_defaults()
  673. */
  674. function hook_js_alter(&$javascript) {
  675. // Swap out jQuery to use an updated version of the library.
  676. $javascript['misc/jquery.js']['data'] = drupal_get_path('module', 'jquery_update') . '/jquery.js';
  677. }
  678. /**
  679. * Registers JavaScript/CSS libraries associated with a module.
  680. *
  681. * Modules implementing this return an array of arrays. The key to each
  682. * sub-array is the machine readable name of the library. Each library may
  683. * contain the following items:
  684. *
  685. * - 'title': The human readable name of the library.
  686. * - 'website': The URL of the library's web site.
  687. * - 'version': A string specifying the version of the library; intentionally
  688. * not a float because a version like "1.2.3" is not a valid float. Use PHP's
  689. * version_compare() to compare different versions.
  690. * - 'js': An array of JavaScript elements; each element's key is used as $data
  691. * argument, each element's value is used as $options array for
  692. * drupal_add_js(). To add library-specific (not module-specific) JavaScript
  693. * settings, the key may be skipped, the value must specify
  694. * 'type' => 'setting', and the actual settings must be contained in a 'data'
  695. * element of the value.
  696. * - 'css': Like 'js', an array of CSS elements passed to drupal_add_css().
  697. * - 'dependencies': An array of libraries that are required for a library. Each
  698. * element is an array listing the module and name of another library. Note
  699. * that all dependencies for each dependent library will also be added when
  700. * this library is added.
  701. *
  702. * Registered information for a library should contain re-usable data only.
  703. * Module- or implementation-specific data and integration logic should be added
  704. * separately.
  705. *
  706. * @return
  707. * An array defining libraries associated with a module.
  708. *
  709. * @see system_library()
  710. * @see drupal_add_library()
  711. * @see drupal_get_library()
  712. */
  713. function hook_library() {
  714. // Library One.
  715. $libraries['library-1'] = array(
  716. 'title' => 'Library One',
  717. 'website' => 'http://example.com/library-1',
  718. 'version' => '1.2',
  719. 'js' => array(
  720. drupal_get_path('module', 'my_module') . '/library-1.js' => array(),
  721. ),
  722. 'css' => array(
  723. drupal_get_path('module', 'my_module') . '/library-2.css' => array(
  724. 'type' => 'file',
  725. 'media' => 'screen',
  726. ),
  727. ),
  728. );
  729. // Library Two.
  730. $libraries['library-2'] = array(
  731. 'title' => 'Library Two',
  732. 'website' => 'http://example.com/library-2',
  733. 'version' => '3.1-beta1',
  734. 'js' => array(
  735. // JavaScript settings may use the 'data' key.
  736. array(
  737. 'type' => 'setting',
  738. 'data' => array('library2' => TRUE),
  739. ),
  740. ),
  741. 'dependencies' => array(
  742. // Require jQuery UI core by System module.
  743. array('system', 'ui'),
  744. // Require our other library.
  745. array('my_module', 'library-1'),
  746. // Require another library.
  747. array('other_module', 'library-3'),
  748. ),
  749. );
  750. return $libraries;
  751. }
  752. /**
  753. * Alters the JavaScript/CSS library registry.
  754. *
  755. * Allows certain, contributed modules to update libraries to newer versions
  756. * while ensuring backwards compatibility. In general, such manipulations should
  757. * only be done by designated modules, since most modules that integrate with a
  758. * certain library also depend on the API of a certain library version.
  759. *
  760. * @param $libraries
  761. * The JavaScript/CSS libraries provided by $module. Keyed by internal library
  762. * name and passed by reference.
  763. * @param $module
  764. * The name of the module that registered the libraries.
  765. *
  766. * @see hook_library()
  767. */
  768. function hook_library_alter(&$libraries, $module) {
  769. // Update Farbtastic to version 2.0.
  770. if ($module == 'system' && isset($libraries['farbtastic'])) {
  771. // Verify existing version is older than the one we are updating to.
  772. if (version_compare($libraries['farbtastic']['version'], '2.0', '<')) {
  773. // Update the existing Farbtastic to version 2.0.
  774. $libraries['farbtastic']['version'] = '2.0';
  775. $libraries['farbtastic']['js'] = array(
  776. drupal_get_path('module', 'farbtastic_update') . '/farbtastic-2.0.js' => array(),
  777. );
  778. }
  779. }
  780. }
  781. /**
  782. * Alter CSS files before they are output on the page.
  783. *
  784. * @param $css
  785. * An array of all CSS items (files and inline CSS) being requested on the page.
  786. *
  787. * @see drupal_add_css()
  788. * @see drupal_get_css()
  789. */
  790. function hook_css_alter(&$css) {
  791. // Remove defaults.css file.
  792. unset($css[drupal_get_path('module', 'system') . '/defaults.css']);
  793. }
  794. /**
  795. * Alter the commands that are sent to the user through the Ajax framework.
  796. *
  797. * @param $commands
  798. * An array of all commands that will be sent to the user.
  799. *
  800. * @see ajax_render()
  801. */
  802. function hook_ajax_render_alter($commands) {
  803. // Inject any new status messages into the content area.
  804. $commands[] = ajax_command_prepend('#block-system-main .content', theme('status_messages'));
  805. }
  806. /**
  807. * Add elements to a page before it is rendered.
  808. *
  809. * Use this hook when you want to add elements at the page level. For your
  810. * additions to be printed, they have to be placed below a top level array key
  811. * of the $page array that has the name of a region of the active theme.
  812. *
  813. * By default, valid region keys are 'page_top', 'header', 'sidebar_first',
  814. * 'content', 'sidebar_second' and 'page_bottom'. To get a list of all regions
  815. * of the active theme, use system_region_list($theme). Note that $theme is a
  816. * global variable.
  817. *
  818. * If you want to alter the elements added by other modules or if your module
  819. * depends on the elements of other modules, use hook_page_alter() instead which
  820. * runs after this hook.
  821. *
  822. * @param $page
  823. * Nested array of renderable elements that make up the page.
  824. *
  825. * @see hook_page_alter()
  826. * @see drupal_render_page()
  827. */
  828. function hook_page_build(&$page) {
  829. if (menu_get_object('node', 1)) {
  830. // We are on a node detail page. Append a standard disclaimer to the
  831. // content region.
  832. $page['content']['disclaimer'] = array(
  833. '#markup' => t('Acme, Inc. is not responsible for the contents of this sample code.'),
  834. '#weight' => 25,
  835. );
  836. }
  837. }
  838. /**
  839. * Alter a menu router item right after it has been retrieved from the database or cache.
  840. *
  841. * This hook is invoked by menu_get_item() and allows for run-time alteration of router
  842. * information (page_callback, title, and so on) before it is translated and checked for
  843. * access. The passed-in $router_item is statically cached for the current request, so this
  844. * hook is only invoked once for any router item that is retrieved via menu_get_item().
  845. *
  846. * Usually, modules will only want to inspect the router item and conditionally
  847. * perform other actions (such as preparing a state for the current request).
  848. * Note that this hook is invoked for any router item that is retrieved by
  849. * menu_get_item(), which may or may not be called on the path itself, so implementations
  850. * should check the $path parameter if the alteration should fire for the current request
  851. * only.
  852. *
  853. * @param $router_item
  854. * The menu router item for $path.
  855. * @param $path
  856. * The originally passed path, for which $router_item is responsible.
  857. * @param $original_map
  858. * The path argument map, as contained in $path.
  859. *
  860. * @see menu_get_item()
  861. */
  862. function hook_menu_get_item_alter(&$router_item, $path, $original_map) {
  863. // When retrieving the router item for the current path...
  864. if ($path == $_GET['q']) {
  865. // ...call a function that prepares something for this request.
  866. mymodule_prepare_something();
  867. }
  868. }
  869. /**
  870. * Define menu items and page callbacks.
  871. *
  872. * This hook enables modules to register paths in order to define how URL
  873. * requests are handled. Paths may be registered for URL handling only, or they
  874. * can register a link to be placed in a menu (usually the Navigation menu). A
  875. * path and its associated information is commonly called a "menu router item".
  876. * This hook is rarely called (for example, when modules are enabled), and
  877. * its results are cached in the database.
  878. *
  879. * hook_menu() implementations return an associative array whose keys define
  880. * paths and whose values are an associative array of properties for each
  881. * path. (The complete list of properties is in the return value section below.)
  882. *
  883. * The definition for each path may include a page callback function, which is
  884. * invoked when the registered path is requested. If there is no other
  885. * registered path that fits the requested path better, any further path
  886. * components are passed to the callback function. For example, your module
  887. * could register path 'abc/def':
  888. * @code
  889. * function mymodule_menu() {
  890. * $items['abc/def'] = array(
  891. * 'page callback' => 'mymodule_abc_view',
  892. * );
  893. * return $items;
  894. * }
  895. *
  896. * function mymodule_abc_view($ghi = 0, $jkl = '') {
  897. * // ...
  898. * }
  899. * @endcode
  900. * When path 'abc/def' is requested, no further path components are in the
  901. * request, and no additional arguments are passed to the callback function (so
  902. * $ghi and $jkl would take the default values as defined in the function
  903. * signature). When 'abc/def/123/foo' is requested, $ghi will be '123' and
  904. * $jkl will be 'foo'. Note that this automatic passing of optional path
  905. * arguments applies only to page and theme callback functions.
  906. *
  907. * In addition to optional path arguments, the page callback and other callback
  908. * functions may specify argument lists as arrays. These argument lists may
  909. * contain both fixed/hard-coded argument values and integers that correspond
  910. * to path components. When integers are used and the callback function is
  911. * called, the corresponding path components will be substituted for the
  912. * integers. That is, the integer 0 in an argument list will be replaced with
  913. * the first path component, integer 1 with the second, and so on (path
  914. * components are numbered starting from zero). To pass an integer without it
  915. * being replaced with its respective path component, use the string value of
  916. * the integer (e.g., '1') as the argument value. This substitution feature
  917. * allows you to re-use a callback function for several different paths. For
  918. * example:
  919. * @code
  920. * function mymodule_menu() {
  921. * $items['abc/def'] = array(
  922. * 'page callback' => 'mymodule_abc_view',
  923. * 'page arguments' => array(1, 'foo'),
  924. * );
  925. * return $items;
  926. * }
  927. * @endcode
  928. * When path 'abc/def' is requested, the page callback function will get 'def'
  929. * as the first argument and (always) 'foo' as the second argument.
  930. *
  931. * If a page callback function uses an argument list array, and its path is
  932. * requested with optional path arguments, then the list array's arguments are
  933. * passed to the callback function first, followed by the optional path
  934. * arguments. Using the above example, when path 'abc/def/bar/baz' is requested,
  935. * mymodule_abc_view() will be called with 'def', 'foo', 'bar' and 'baz' as
  936. * arguments, in that order.
  937. *
  938. * Special care should be taken for the page callback drupal_get_form(), because
  939. * your specific form callback function will always receive $form and
  940. * &$form_state as the first function arguments:
  941. * @code
  942. * function mymodule_abc_form($form, &$form_state) {
  943. * // ...
  944. * return $form;
  945. * }
  946. * @endcode
  947. * See @link form_api Form API documentation @endlink for details.
  948. *
  949. * Wildcards within paths also work with integer substitution. For example,
  950. * your module could register path 'my-module/%/edit':
  951. * @code
  952. * $items['my-module/%/edit'] = array(
  953. * 'page callback' => 'mymodule_abc_edit',
  954. * 'page arguments' => array(1),
  955. * );
  956. * @endcode
  957. * When path 'my-module/foo/edit' is requested, integer 1 will be replaced
  958. * with 'foo' and passed to the callback function. Note that wildcards may not
  959. * be used as the first component.
  960. *
  961. * Registered paths may also contain special "auto-loader" wildcard components
  962. * in the form of '%mymodule_abc', where the '%' part means that this path
  963. * component is a wildcard, and the 'mymodule_abc' part defines the prefix for a
  964. * load function, which here would be named mymodule_abc_load(). When a matching
  965. * path is requested, your load function will receive as its first argument the
  966. * path component in the position of the wildcard; load functions may also be
  967. * passed additional arguments (see "load arguments" in the return value
  968. * section below). For example, your module could register path
  969. * 'my-module/%mymodule_abc/edit':
  970. * @code
  971. * $items['my-module/%mymodule_abc/edit'] = array(
  972. * 'page callback' => 'mymodule_abc_edit',
  973. * 'page arguments' => array(1),
  974. * );
  975. * @endcode
  976. * When path 'my-module/123/edit' is requested, your load function
  977. * mymodule_abc_load() will be invoked with the argument '123', and should
  978. * load and return an "abc" object with internal id 123:
  979. * @code
  980. * function mymodule_abc_load($abc_id) {
  981. * return db_query("SELECT * FROM {mymodule_abc} WHERE abc_id = :abc_id", array(':abc_id' => $abc_id))->fetchObject();
  982. * }
  983. * @endcode
  984. * This 'abc' object will then be passed into the callback functions defined
  985. * for the menu item, such as the page callback function mymodule_abc_edit()
  986. * to replace the integer 1 in the argument array. Note that a load function
  987. * should return FALSE when it is unable to provide a loadable object. For
  988. * example, the node_load() function for the 'node/%node/edit' menu item will
  989. * return FALSE for the path 'node/999/edit' if a node with a node ID of 999
  990. * does not exist. The menu routing system will return a 404 error in this case.
  991. *
  992. * You can also define a %wildcard_to_arg() function (for the example menu
  993. * entry above this would be 'mymodule_abc_to_arg()'). The _to_arg() function
  994. * is invoked to retrieve a value that is used in the path in place of the
  995. * wildcard. A good example is user.module, which defines
  996. * user_uid_optional_to_arg() (corresponding to the menu entry
  997. * 'user/%user_uid_optional'). This function returns the user ID of the
  998. * current user.
  999. *
  1000. * The _to_arg() function will get called with three arguments:
  1001. * - $arg: A string representing whatever argument may have been supplied by
  1002. * the caller (this is particularly useful if you want the _to_arg()
  1003. * function only supply a (default) value if no other value is specified,
  1004. * as in the case of user_uid_optional_to_arg().
  1005. * - $map: An array of all path fragments (e.g. array('node','123','edit') for
  1006. * 'node/123/edit').
  1007. * - $index: An integer indicating which element of $map corresponds to $arg.
  1008. *
  1009. * _load() and _to_arg() functions may seem similar at first glance, but they
  1010. * have different purposes and are called at different times. _load()
  1011. * functions are called when the menu system is collecting arguments to pass
  1012. * to the callback functions defined for the menu item. _to_arg() functions
  1013. * are called when the menu system is generating links to related paths, such
  1014. * as the tabs for a set of MENU_LOCAL_TASK items.
  1015. *
  1016. * You can also make groups of menu items to be rendered (by default) as tabs
  1017. * on a page. To do that, first create one menu item of type MENU_NORMAL_ITEM,
  1018. * with your chosen path, such as 'foo'. Then duplicate that menu item, using a
  1019. * subdirectory path, such as 'foo/tab1', and changing the type to
  1020. * MENU_DEFAULT_LOCAL_TASK to make it the default tab for the group. Then add
  1021. * the additional tab items, with paths such as "foo/tab2" etc., with type
  1022. * MENU_LOCAL_TASK. Example:
  1023. * @code
  1024. * // Make "Foo settings" appear on the admin Config page
  1025. * $items['admin/config/system/foo'] = array(
  1026. * 'title' => 'Foo settings',
  1027. * 'type' => MENU_NORMAL_ITEM,
  1028. * // Page callback, etc. need to be added here.
  1029. * );
  1030. * // Make "Tab 1" the main tab on the "Foo settings" page
  1031. * $items['admin/config/system/foo/tab1'] = array(
  1032. * 'title' => 'Tab 1',
  1033. * 'type' => MENU_DEFAULT_LOCAL_TASK,
  1034. * // Access callback, page callback, and theme callback will be inherited
  1035. * // from 'admin/config/system/foo', if not specified here to override.
  1036. * );
  1037. * // Make an additional tab called "Tab 2" on "Foo settings"
  1038. * $items['admin/config/system/foo/tab2'] = array(
  1039. * 'title' => 'Tab 2',
  1040. * 'type' => MENU_LOCAL_TASK,
  1041. * // Page callback and theme callback will be inherited from
  1042. * // 'admin/config/system/foo', if not specified here to override.
  1043. * // Need to add access callback or access arguments.
  1044. * );
  1045. * @endcode
  1046. *
  1047. * @return
  1048. * An array of menu items. Each menu item has a key corresponding to the
  1049. * Drupal path being registered. The corresponding array value is an
  1050. * associative array that may contain the following key-value pairs:
  1051. * - "title": Required. The untranslated title of the menu item.
  1052. * - "title callback": Function to generate the title; defaults to t().
  1053. * If you require only the raw string to be output, set this to FALSE.
  1054. * - "title arguments": Arguments to send to t() or your custom callback,
  1055. * with path component substitution as described above.
  1056. * - "description": The untranslated description of the menu item.
  1057. * - "page callback": The function to call to display a web page when the user
  1058. * visits the path. If omitted, the parent menu item's callback will be used
  1059. * instead.
  1060. * - "page arguments": An array of arguments to pass to the page callback
  1061. * function, with path component substitution as described above.
  1062. * - "delivery callback": The function to call to package the result of the
  1063. * page callback function and send it to the browser. Defaults to
  1064. * drupal_deliver_html_page() unless a value is inherited from a parent menu
  1065. * item. Note that this function is called even if the access checks fail,
  1066. * so any custom delivery callback function should take that into account.
  1067. * See drupal_deliver_html_page() for an example.
  1068. * - "access callback": A function returning TRUE if the user has access
  1069. * rights to this menu item, and FALSE if not. It can also be a boolean
  1070. * constant instead of a function, and you can also use numeric values
  1071. * (will be cast to boolean). Defaults to user_access() unless a value is
  1072. * inherited from the parent menu item; only MENU_DEFAULT_LOCAL_TASK items
  1073. * can inherit access callbacks. To use the user_access() default callback,
  1074. * you must specify the permission to check as 'access arguments' (see
  1075. * below).
  1076. * - "access arguments": An array of arguments to pass to the access callback
  1077. * function, with path component substitution as described above. If the
  1078. * access callback is inherited (see above), the access arguments will be
  1079. * inherited with it, unless overridden in the child menu item.
  1080. * - "theme callback": (optional) A function returning the machine-readable
  1081. * name of the theme that will be used to render the page. If not provided,
  1082. * the value will be inherited from a parent menu item. If there is no
  1083. * theme callback, or if the function does not return the name of a current
  1084. * active theme on the site, the theme for this page will be determined by
  1085. * either hook_custom_theme() or the default theme instead. As a general
  1086. * rule, the use of theme callback functions should be limited to pages
  1087. * whose functionality is very closely tied to a particular theme, since
  1088. * they can only be overridden by modules which specifically target those
  1089. * pages in hook_menu_alter(). Modules implementing more generic theme
  1090. * switching functionality (for example, a module which allows the theme to
  1091. * be set dynamically based on the current user's role) should use
  1092. * hook_custom_theme() instead.
  1093. * - "theme arguments": An array of arguments to pass to the theme callback
  1094. * function, with path component substitution as described above.
  1095. * - "file": A file that will be included before the page callback is called;
  1096. * this allows page callback functions to be in separate files. The file
  1097. * should be relative to the implementing module's directory unless
  1098. * otherwise specified by the "file path" option. Does not apply to other
  1099. * callbacks (only page callback).
  1100. * - "file path": The path to the directory containing the file specified in
  1101. * "file". This defaults to the path to the module implementing the hook.
  1102. * - "load arguments": An array of arguments to be passed to each of the
  1103. * wildcard object loaders in the path, after the path argument itself.
  1104. * For example, if a module registers path node/%node/revisions/%/view
  1105. * with load arguments set to array(3), the '%node' in the path indicates
  1106. * that the loader function node_load() will be called with the second
  1107. * path component as the first argument. The 3 in the load arguments
  1108. * indicates that the fourth path component will also be passed to
  1109. * node_load() (numbering of path components starts at zero). So, if path
  1110. * node/12/revisions/29/view is requested, node_load(12, 29) will be called.
  1111. * There are also two "magic" values that can be used in load arguments.
  1112. * "%index" indicates the index of the wildcard path component. "%map"
  1113. * indicates the path components as an array. For example, if a module
  1114. * registers for several paths of the form 'user/%user_category/edit/*', all
  1115. * of them can use the same load function user_category_load(), by setting
  1116. * the load arguments to array('%map', '%index'). For instance, if the user
  1117. * is editing category 'foo' by requesting path 'user/32/edit/foo', the load
  1118. * function user_category_load() will be called with 32 as its first
  1119. * argument, the array ('user', 32, 'edit', 'foo') as the map argument,
  1120. * and 1 as the index argument (because %user_category is the second path
  1121. * component and numbering starts at zero). user_category_load() can then
  1122. * use these values to extract the information that 'foo' is the category
  1123. * being requested.
  1124. * - "weight": An integer that determines the relative position of items in
  1125. * the menu; higher-weighted items sink. Defaults to 0. Menu items with the
  1126. * same weight are ordered alphabetically.
  1127. * - "menu_name": Optional. Set this to a custom menu if you don't want your
  1128. * item to be placed in Navigation.
  1129. * - "context": (optional) Defines the context a tab may appear in. By
  1130. * default, all tabs are only displayed as local tasks when being rendered
  1131. * in a page context. All tabs that should be accessible as contextual links
  1132. * in page region containers outside of the parent menu item's primary page
  1133. * context should be registered using one of the following contexts:
  1134. * - MENU_CONTEXT_PAGE: (default) The tab is displayed as local task for the
  1135. * page context only.
  1136. * - MENU_CONTEXT_INLINE: The tab is displayed as contextual link outside of
  1137. * the primary page context only.
  1138. * Contex…

Large files files are truncated, but you can click here to view the full file