core.api.php 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  1. <?php
  2. /**
  3. * @file
  4. * Documentation landing page and topics, plus core library hooks.
  5. */
  6. /**
  7. * @mainpage
  8. * Welcome to the Drupal API Documentation!
  9. *
  10. * This site is an API reference for Drupal, generated from comments embedded
  11. * in the source code. More in-depth documentation can be found at
  12. * https://www.drupal.org/developing/api.
  13. *
  14. * Here are some topics to help you get started developing with Drupal.
  15. *
  16. * @section essentials Essential background concepts
  17. *
  18. * - @link oo_conventions Object-oriented conventions used in Drupal @endlink
  19. * - @link extending Extending and altering Drupal @endlink
  20. * - @link best_practices Security and best practices @endlink
  21. * - @link info_types Types of information in Drupal @endlink
  22. *
  23. * @section interface User interface
  24. *
  25. * - @link menu Menu entries, local tasks, and other links @endlink
  26. * - @link routing Routing API and page controllers @endlink
  27. * - @link form_api Forms @endlink
  28. * - @link block_api Blocks @endlink
  29. * - @link ajax Ajax @endlink
  30. *
  31. * @section store_retrieve Storing and retrieving data
  32. *
  33. * - @link entity_api Entities @endlink
  34. * - @link field Fields @endlink
  35. * - @link config_api Configuration API @endlink
  36. * - @link state_api State API @endlink
  37. * - @link views_overview Views @endlink
  38. * - @link database Database abstraction layer @endlink
  39. *
  40. * @section other_essentials Other essential APIs
  41. *
  42. * - @link plugin_api Plugins @endlink
  43. * - @link container Services and the Dependency Injection Container @endlink
  44. * - @link events Events @endlink
  45. * - @link i18n Internationalization @endlink
  46. * - @link cache Caching @endlink
  47. * - @link utility Utility classes and functions @endlink
  48. * - @link user_api User accounts, permissions, and roles @endlink
  49. * - @link theme_render Render API @endlink
  50. * - @link themeable Theme system @endlink
  51. * - @link update_api Update API @endlink
  52. * - @link migration Migration @endlink
  53. *
  54. * @section additional Additional topics
  55. *
  56. * - @link batch Batch API @endlink
  57. * - @link queue Queue API @endlink
  58. * - @link typed_data Typed Data @endlink
  59. * - @link testing Automated tests @endlink
  60. * - @link php_assert PHP Runtime Assert Statements @endlink
  61. * - @link third_party Integrating third-party applications @endlink
  62. *
  63. * @section more_info Further information
  64. *
  65. * - @link https://api.drupal.org/api/drupal/groups/8 All topics @endlink
  66. * - @link https://www.drupal.org/project/examples Examples project (sample modules) @endlink
  67. * - @link https://www.drupal.org/list-changes API change notices @endlink
  68. * - @link https://www.drupal.org/developing/api/8 Drupal 8 API longer references @endlink
  69. */
  70. /**
  71. * @defgroup third_party REST and Application Integration
  72. * @{
  73. * Integrating third-party applications using REST and related operations.
  74. *
  75. * @section sec_overview Overview of web services
  76. * Web services make it possible for applications and web sites to read and
  77. * update information from other web sites. There are several standard
  78. * techniques for providing web services, including:
  79. * - SOAP: http://wikipedia.org/wiki/SOAP
  80. * - XML-RPC: http://wikipedia.org/wiki/XML-RPC
  81. * - REST: http://wikipedia.org/wiki/Representational_state_transfer
  82. * Drupal sites can both provide web services and integrate third-party web
  83. * services.
  84. *
  85. * @section sec_rest_overview Overview of REST
  86. * The REST technique uses basic HTTP requests to obtain and update data, where
  87. * each web service defines a specific API (HTTP GET and/or POST parameters and
  88. * returned response) for its HTTP requests. REST requests are separated into
  89. * several types, known as methods, including:
  90. * - GET: Requests to obtain data.
  91. * - POST: Requests to update or create data.
  92. * - PUT: Requests to update or create data (limited support, currently unused
  93. * by entity resources).
  94. * - PATCH: Requests to update a subset of data, such as one field.
  95. * - DELETE: Requests to delete data.
  96. * The Drupal Core REST module provides support for GET, POST, PATCH, and DELETE
  97. * quests on entities, GET requests on the database log from the Database
  98. * Logging module, and a plugin framework for providing REST support for other
  99. * data and other methods.
  100. *
  101. * REST requests can be authenticated. The Drupal Core Basic Auth module
  102. * provides authentication using the HTTP Basic protocol; the contributed module
  103. * OAuth (https://www.drupal.org/project/oauth) implements the OAuth
  104. * authentication protocol. You can also use cookie-based authentication, which
  105. * would require users to be logged into the Drupal site while using the
  106. * application on the third-party site that is using the REST service.
  107. *
  108. * @section sec_rest Enabling REST for entities and the log
  109. * Here are the steps to take to use the REST operations provided by Drupal
  110. * Core:
  111. * - Enable the REST module, plus Basic Auth (or another authentication method)
  112. * and HAL.
  113. * - Node entity support is configured by default. If you would like to support
  114. * other types of entities, you can copy
  115. * core/modules/rest/config/install/rest.settings.yml to your sync
  116. * configuration directory, appropriately modified for other entity types,
  117. * and import it. Support for GET on the log from the Database Logging module
  118. * can also be enabled in this way; in this case, the 'entity:node' line
  119. * in the configuration would be replaced by the appropriate plugin ID,
  120. * 'dblog'.
  121. * - Set up permissions to allow the desired REST operations for a role, and set
  122. * up one or more user accounts to perform the operations.
  123. * - To perform a REST operation, send a request to either the canonical URL
  124. * for an entity (such as node/12345 for a node), or if the entity does not
  125. * have a canonical URL, a URL like entity/(type)/(ID). The URL for a log
  126. * entry is dblog/(ID). The request must have the following properties:
  127. * - The request method must be set to the REST method you are using (POST,
  128. * GET, PATCH, etc.).
  129. * - The content type for the data you send, or the accept type for the
  130. * data you are receiving, must be set to 'application/hal+json'.
  131. * - If you are sending data, it must be JSON-encoded.
  132. * - You'll also need to make sure the authentication information is sent
  133. * with the request, unless you have allowed access to anonymous users.
  134. *
  135. * For more detailed information on setting up REST, see
  136. * https://www.drupal.org/documentation/modules/rest.
  137. *
  138. * @section sec_plugins Defining new REST plugins
  139. * The REST framework in the REST module has support built in for entities, but
  140. * it is also an extensible plugin-based system. REST plugins implement
  141. * interface \Drupal\rest\Plugin\ResourceInterface, and generally extend base
  142. * class \Drupal\rest\Plugin\ResourceBase. They are annotated with
  143. * \Drupal\rest\Annotation\RestResource annotation, and must be in plugin
  144. * namespace subdirectory Plugin\rest\resource. For more information on how to
  145. * create plugins, see the @link plugin_api Plugin API topic. @endlink
  146. *
  147. * If you create a new REST plugin, you will also need to enable it by
  148. * providing default configuration or configuration import, as outlined in
  149. * @ref sec_rest above.
  150. *
  151. * @section sec_integrate Integrating data from other sites into Drupal
  152. * If you want to integrate data from other web sites into Drupal, here are
  153. * some notes:
  154. * - There are contributed modules available for integrating many third-party
  155. * sites into Drupal. Search on https://www.drupal.org/project/project_module
  156. * - If there is not an existing module, you will need to find documentation on
  157. * the specific web services API for the site you are trying to integrate.
  158. * - There are several classes and functions that are useful for interacting
  159. * with web services:
  160. * - You should make requests using the 'http_client' service, which
  161. * implements \GuzzleHttp\ClientInterface. See the
  162. * @link container Services topic @endlink for more information on
  163. * services. If you cannot use dependency injection to retrieve this
  164. * service, the \Drupal::httpClient() method is available. A good example
  165. * of how to use this service can be found in
  166. * \Drupal\aggregator\Plugin\aggregator\fetcher\DefaultFetcher
  167. * - \Drupal\Component\Serialization\Json (JSON encoding and decoding).
  168. * - PHP has functions and classes for parsing XML; see
  169. * http://php.net/manual/refs.xml.php
  170. * @}
  171. */
  172. /**
  173. * @defgroup state_api State API
  174. * @{
  175. * Information about the State API.
  176. *
  177. * The State API is one of several methods in Drupal for storing information.
  178. * See the @link info_types Information types topic @endlink for an
  179. * overview of the different types of information.
  180. *
  181. * The basic entry point into the State API is \Drupal::state(), which returns
  182. * an object of class \Drupal\Core\State\StateInterface. This class has
  183. * methods for storing and retrieving state information; each piece of state
  184. * information is associated with a string-valued key. Example:
  185. * @code
  186. * // Get the state class.
  187. * $state = \Drupal::state();
  188. * // Find out when cron was last run; the key is 'system.cron_last'.
  189. * $time = $state->get('system.cron_last');
  190. * // Set the cron run time to the current request time.
  191. * $state->set('system.cron_last', REQUEST_TIME);
  192. * @endcode
  193. *
  194. * For more on the State API, see https://www.drupal.org/developing/api/8/state
  195. * @}
  196. */
  197. /**
  198. * @defgroup config_api Configuration API
  199. * @{
  200. * Information about the Configuration API.
  201. *
  202. * The Configuration API is one of several methods in Drupal for storing
  203. * information. See the @link info_types Information types topic @endlink for
  204. * an overview of the different types of information. The sections below have
  205. * more information about the configuration API; see
  206. * https://www.drupal.org/developing/api/8/configuration for more details.
  207. *
  208. * @section sec_storage Configuration storage
  209. * In Drupal, there is a concept of the "active" configuration, which is the
  210. * configuration that is currently in use for a site. The storage used for the
  211. * active configuration is configurable: it could be in the database, in files
  212. * in a particular directory, or in other storage backends; the default storage
  213. * is in the database. Module developers must use the configuration API to
  214. * access the active configuration, rather than being concerned about the
  215. * details of where and how it is stored.
  216. *
  217. * Configuration is divided into individual objects, each of which has a
  218. * unique name or key. Some modules will have only one configuration object,
  219. * typically called 'mymodule.settings'; some modules will have many. Within
  220. * a configuration object, configuration settings have data types (integer,
  221. * string, Boolean, etc.) and settings can also exist in a nested hierarchy,
  222. * known as a "mapping".
  223. *
  224. * Configuration can also be overridden on a global, per-language, or
  225. * per-module basis. See https://www.drupal.org/node/1928898 for more
  226. * information.
  227. *
  228. * @section sec_yaml Configuration YAML files
  229. * Whether or not configuration files are being used for the active
  230. * configuration storage on a particular site, configuration files are always
  231. * used for:
  232. * - Defining the default configuration for an extension (module, theme, or
  233. * profile), which is imported to the active storage when the extension is
  234. * enabled. These configuration items are located in the config/install
  235. * sub-directory of the extension. Note that changes to this configuration
  236. * after a module or theme is already enabled have no effect; to make a
  237. * configuration change after a module or theme is enabled, you would need to
  238. * uninstall/reinstall or use a hook_update_N() function.
  239. * - Defining optional configuration for a module or theme. Optional
  240. * configuration items are located in the config/optional sub-directory of the
  241. * extension. These configuration items have dependencies that are not
  242. * explicit dependencies of the extension, so they are only installed if all
  243. * dependencies are met. For example, in the scenario that module A defines a
  244. * dependency which requires module B, but module A is installed first and
  245. * module B some time later, then module A's config/optional directory will be
  246. * scanned at that time for newly met dependencies, and the configuration will
  247. * be installed then. If module B is never installed, the configuration item
  248. * will not be installed either.
  249. * - Exporting and importing configuration.
  250. *
  251. * The file storage format for configuration information in Drupal is
  252. * @link http://wikipedia.org/wiki/YAML YAML files. @endlink Configuration is
  253. * divided into files, each containing one configuration object. The file name
  254. * for a configuration object is equal to the unique name of the configuration,
  255. * with a '.yml' extension. The default configuration files for each module are
  256. * placed in the config/install directory under the top-level module directory,
  257. * so look there in most Core modules for examples.
  258. *
  259. * @section sec_schema Configuration schema and translation
  260. * Each configuration file has a specific structure, which is expressed as a
  261. * YAML-based configuration schema. The configuration schema details the
  262. * structure of the configuration, its data types, and which of its values need
  263. * to be translatable. Each module needs to define its configuration schema in
  264. * files in the config/schema directory under the top-level module directory, so
  265. * look there in most Core modules for examples.
  266. *
  267. * Configuration can be internationalized; see the
  268. * @link i18n Internationalization topic @endlink for more information. Data
  269. * types label, text, and date_format in configuration schema are translatable;
  270. * string is non-translatable text (the 'translatable' property on a schema
  271. * data type definition indicates that it is translatable).
  272. *
  273. * @section sec_simple Simple configuration
  274. * The simple configuration API should be used for information that will always
  275. * have exactly one copy or version. For instance, if your module has a
  276. * setting that is either on or off, then this is only defined once, and it
  277. * would be a Boolean-valued simple configuration setting.
  278. *
  279. * The first task in using the simple configuration API is to define the
  280. * configuration file structure, file name, and schema of your settings (see
  281. * @ref sec_yaml above). Once you have done that, you can retrieve the active
  282. * configuration object that corresponds to configuration file mymodule.foo.yml
  283. * with a call to:
  284. * @code
  285. * $config = \Drupal::config('mymodule.foo');
  286. * @endcode
  287. *
  288. * This will be an object of class \Drupal\Core\Config\Config, which has methods
  289. * for getting configuration information. For instance, if your YAML file
  290. * structure looks like this:
  291. * @code
  292. * enabled: '0'
  293. * bar:
  294. * baz: 'string1'
  295. * boo: 34
  296. * @endcode
  297. * you can make calls such as:
  298. * @code
  299. * // Get a single value.
  300. * $enabled = $config->get('enabled');
  301. * // Get an associative array.
  302. * $bar = $config->get('bar');
  303. * // Get one element of the array.
  304. * $bar_baz = $config->get('bar.baz');
  305. * @endcode
  306. *
  307. * The Config object that was obtained and used in the previous examples does
  308. * not allow you to change configuration. If you want to change configuration,
  309. * you will instead need to get the Config object by making a call to
  310. * getEditable() on the config factory:
  311. * @code
  312. * $config =\Drupal::service('config.factory')->getEditable('mymodule.foo');
  313. * @endcode
  314. *
  315. * Individual configuration values can be changed or added using the set()
  316. * method and saved using the save() method:
  317. * @code
  318. * // Set a scalar value.
  319. * $config->set('enabled', 1);
  320. * // Save the configuration.
  321. * $config->save();
  322. * @endcode
  323. *
  324. * Configuration values can also be unset using the clear() method, which is
  325. * also chainable:
  326. * @code
  327. * $config->clear('bar.boo')->save();
  328. * $config_data = $config->get('bar');
  329. * @endcode
  330. * In this example $config_data would return an array with one key - 'baz' -
  331. * because 'boo' was unset.
  332. *
  333. * @section sec_entity Configuration entities
  334. * In contrast to the simple configuration settings described in the previous
  335. * section, if your module allows users to create zero or more items (where
  336. * "items" are things like content type definitions, view definitions, and the
  337. * like), then you need to define a configuration entity type to store your
  338. * configuration. Creating an entity type, loading entities, and querying them
  339. * are outlined in the @link entity_api Entity API topic. @endlink Here are a
  340. * few additional steps and notes specific to configuration entities:
  341. * - For examples, look for classes that implement
  342. * \Drupal\Core\Config\Entity\ConfigEntityInterface -- one good example is
  343. * the \Drupal\user\Entity\Role entity type.
  344. * - In the entity type annotation, you will need to define a 'config_prefix'
  345. * string. When Drupal stores a configuration item, it will be given a name
  346. * composed of your module name, your chosen config prefix, and the ID of
  347. * the individual item, separated by '.'. For example, in the Role entity,
  348. * the config prefix is 'role', so one configuration item might be named
  349. * user.role.anonymous, with configuration file user.role.anonymous.yml.
  350. * - You will need to define the schema for your configuration in your
  351. * modulename.schema.yml file, with an entry for 'modulename.config_prefix.*'.
  352. * For example, for the Role entity, the file user.schema.yml has an entry
  353. * user.role.*; see @ref sec_yaml above for more information.
  354. * - Your module can provide default/optional configuration entities in YAML
  355. * files; see @ref sec_yaml above for more information.
  356. * - Some configuration entities have dependencies on other configuration
  357. * entities, and module developers need to consider this so that configuration
  358. * can be imported, uninstalled, and synchronized in the right order. For
  359. * example, a field display configuration entity would need to depend on
  360. * field configuration, which depends on field and bundle configuration.
  361. * Configuration entity classes expose dependencies by overriding the
  362. * \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies()
  363. * method.
  364. * - On routes for paths starting with '/admin' or otherwise designated as
  365. * administration paths (such as node editing when it is set as an admin
  366. * operation), if they have configuration entity placeholders, configuration
  367. * entities are normally loaded in their original language, without
  368. * translations or other overrides. This is usually desirable, because most
  369. * admin paths are for editing configuration, and you need that to be in the
  370. * source language and to lack possibly dynamic overrides. If for some reason
  371. * you need to have your configuration entity loaded in the currently-selected
  372. * language on an admin path (for instance, if you go to
  373. * example.com/es/admin/your_path and you need the entity to be in Spanish),
  374. * then you can add a 'with_config_overrides' parameter option to your route.
  375. * The same applies if you need to load the entity with overrides (or
  376. * translated) on an admin path like '/node/add/article' (when configured to
  377. * be an admin path). Here's an example using the configurable_language config
  378. * entity:
  379. * @code
  380. * mymodule.myroute:
  381. * path: '/admin/mypath/{configurable_language}'
  382. * defaults:
  383. * _controller: '\Drupal\mymodule\MyController::myMethod'
  384. * options:
  385. * parameters:
  386. * configurable_language:
  387. * type: entity:configurable_language
  388. * with_config_overrides: TRUE
  389. * @endcode
  390. * With the route defined this way, the $configurable_language parameter to
  391. * your controller method will come in translated to the current language.
  392. * Without the parameter options section, it would be in the original
  393. * language, untranslated.
  394. *
  395. * @see i18n
  396. *
  397. * @}
  398. */
  399. /**
  400. * @defgroup cache Cache API
  401. * @{
  402. * Information about the Drupal Cache API
  403. *
  404. * @section basics Basics
  405. *
  406. * Note: If not specified, all of the methods mentioned here belong to
  407. * \Drupal\Core\Cache\CacheBackendInterface.
  408. *
  409. * The Cache API is used to store data that takes a long time to compute.
  410. * Caching can either be permanent or valid only for a certain timespan, and
  411. * the cache can contain any type of data.
  412. *
  413. * To use the Cache API:
  414. * - Request a cache object through \Drupal::cache() or by injecting a cache
  415. * service.
  416. * - Define a Cache ID (cid) value for your data. A cid is a string, which must
  417. * contain enough information to uniquely identify the data. For example, if
  418. * your data contains translated strings, then your cid value must include the
  419. * interface text language selected for page.
  420. * - Call the get() method to attempt a cache read, to see if the cache already
  421. * contains your data.
  422. * - If your data is not already in the cache, compute it and add it to the
  423. * cache using the set() method. The third argument of set() can be used to
  424. * control the lifetime of your cache item.
  425. *
  426. * Example:
  427. * @code
  428. * $cid = 'mymodule_example:' . \Drupal::languageManager()->getCurrentLanguage()->getId();
  429. *
  430. * $data = NULL;
  431. * if ($cache = \Drupal::cache()->get($cid)) {
  432. * $data = $cache->data;
  433. * }
  434. * else {
  435. * $data = my_module_complicated_calculation();
  436. * \Drupal::cache()->set($cid, $data);
  437. * }
  438. * @endcode
  439. *
  440. * Note the use of $data and $cache->data in the above example. Calls to
  441. * \Drupal::cache()->get() return a record that contains the information stored
  442. * by \Drupal::cache()->set() in the data property as well as additional meta
  443. * information about the cached data. In order to make use of the cached data
  444. * you can access it via $cache->data.
  445. *
  446. * @section bins Cache bins
  447. *
  448. * Cache storage is separated into "bins", each containing various cache items.
  449. * Each bin can be configured separately; see @ref configuration.
  450. *
  451. * When you request a cache object, you can specify the bin name in your call to
  452. * \Drupal::cache(). Alternatively, you can request a bin by getting service
  453. * "cache.nameofbin" from the container. The default bin is called "default", with
  454. * service name "cache.default", it is used to store common and frequently used
  455. * caches.
  456. *
  457. * Other common cache bins are the following:
  458. * - bootstrap: Data needed from the beginning to the end of most requests,
  459. * that has a very strict limit on variations and is invalidated rarely.
  460. * - render: Contains cached HTML strings like cached pages and blocks, can
  461. * grow to large size.
  462. * - data: Contains data that can vary by path or similar context.
  463. * - discovery: Contains cached discovery data for things such as plugins,
  464. * views_data, or YAML discovered data such as library info.
  465. *
  466. * A module can define a cache bin by defining a service in its
  467. * modulename.services.yml file as follows (substituting the desired name for
  468. * "nameofbin"):
  469. * @code
  470. * cache.nameofbin:
  471. * class: Drupal\Core\Cache\CacheBackendInterface
  472. * tags:
  473. * - { name: cache.bin }
  474. * factory: cache_factory:get
  475. * arguments: [nameofbin]
  476. * @endcode
  477. * See the @link container Services topic @endlink for more on defining
  478. * services.
  479. *
  480. * @section delete Deletion
  481. *
  482. * There are two ways to remove an item from the cache:
  483. * - Deletion (using delete(), deleteMultiple() or deleteAll()) permanently
  484. * removes the item from the cache.
  485. * - Invalidation (using invalidate(), invalidateMultiple() or invalidateAll())
  486. * is a "soft" delete that only marks items as "invalid", meaning "not fresh"
  487. * or "not fresh enough". Invalid items are not usually returned from the
  488. * cache, so in most ways they behave as if they have been deleted. However,
  489. * it is possible to retrieve invalid items, if they have not yet been
  490. * permanently removed by the garbage collector, by passing TRUE as the second
  491. * argument for get($cid, $allow_invalid).
  492. *
  493. * Use deletion if a cache item is no longer useful; for instance, if the item
  494. * contains references to data that has been deleted. Use invalidation if the
  495. * cached item may still be useful to some callers until it has been updated
  496. * with fresh data. The fact that it was fresh a short while ago may often be
  497. * sufficient.
  498. *
  499. * Invalidation is particularly useful to protect against stampedes. Rather than
  500. * having multiple concurrent requests updating the same cache item when it
  501. * expires or is deleted, there can be one request updating the cache, while the
  502. * other requests can proceed using the stale value. As soon as the cache item
  503. * has been updated, all future requests will use the updated value.
  504. *
  505. * @section tags Cache Tags
  506. *
  507. * The fourth argument of the set() method can be used to specify cache tags,
  508. * which are used to identify which data is included in each cache item. A cache
  509. * item can have multiple cache tags (an array of cache tags), and each cache
  510. * tag is a string. The convention is to generate cache tags of the form
  511. * [prefix]:[suffix]. Usually, you'll want to associate the cache tags of
  512. * entities, or entity listings. You won't have to manually construct cache tags
  513. * for them — just get their cache tags via
  514. * \Drupal\Core\Cache\CacheableDependencyInterface::getCacheTags() and
  515. * \Drupal\Core\Entity\EntityTypeInterface::getListCacheTags().
  516. * Data that has been tagged can be invalidated as a group: no matter the Cache
  517. * ID (cid) of the cache item, no matter in which cache bin a cache item lives;
  518. * as long as it is tagged with a certain cache tag, it will be invalidated.
  519. *
  520. * Because of that, cache tags are a solution to the cache invalidation problem:
  521. * - For caching to be effective, each cache item must only be invalidated when
  522. * absolutely necessary. (i.e. maximizing the cache hit ratio.)
  523. * - For caching to be correct, each cache item that depends on a certain thing
  524. * must be invalidated whenever that certain thing is modified.
  525. *
  526. * A typical scenario: a user has modified a node that appears in two views,
  527. * three blocks and on twelve pages. Without cache tags, we couldn't possibly
  528. * know which cache items to invalidate, so we'd have to invalidate everything:
  529. * we had to sacrifice effectiveness to achieve correctness. With cache tags, we
  530. * can have both.
  531. *
  532. * Example:
  533. * @code
  534. * // A cache item with nodes, users, and some custom module data.
  535. * $tags = array(
  536. * 'my_custom_tag',
  537. * 'node:1',
  538. * 'node:3',
  539. * 'user:7',
  540. * );
  541. * \Drupal::cache()->set($cid, $data, CacheBackendInterface::CACHE_PERMANENT, $tags);
  542. *
  543. * // Invalidate all cache items with certain tags.
  544. * \Drupal\Core\Cache\Cache::invalidateTags(array('user:1'));
  545. * @endcode
  546. *
  547. * Drupal is a content management system, so naturally you want changes to your
  548. * content to be reflected everywhere, immediately. That's why we made sure that
  549. * every entity type in Drupal 8 automatically has support for cache tags: when
  550. * you save an entity, you can be sure that the cache items that have the
  551. * corresponding cache tags will be invalidated.
  552. * This also is the case when you define your own entity types: you'll get the
  553. * exact same cache tag invalidation as any of the built-in entity types, with
  554. * the ability to override any of the default behavior if needed.
  555. * See \Drupal\Core\Cache\CacheableDepenencyInterface::getCacheTags(),
  556. * \Drupal\Core\Entity\EntityTypeInterface::getListCacheTags(),
  557. * \Drupal\Core\Entity\Entity::invalidateTagsOnSave() and
  558. * \Drupal\Core\Entity\Entity::invalidateTagsOnDelete().
  559. *
  560. * @section context Cache contexts
  561. *
  562. * Some computed data depends on contextual data, such as the user roles of the
  563. * logged-in user who is viewing a page, the language the page is being rendered
  564. * in, the theme being used, etc. When caching the output of such a calculation,
  565. * you must cache each variation separately, along with information about which
  566. * variation of the contextual data was used in the calculatation. The next time
  567. * the computed data is needed, if the context matches that for an existing
  568. * cached data set, the cached data can be reused; if no context matches, a new
  569. * data set can be calculated and cached for later use.
  570. *
  571. * Cache contexts are services tagged with 'cache.context', whose classes
  572. * implement \Drupal\Core\Cache\Context\CacheContextInterface. See
  573. * https://www.drupal.org/developing/api/8/cache/contexts for more information
  574. * on cache contexts, including a list of the contexts that exist in Drupal
  575. * core, and information on how to define your own contexts. See the
  576. * @link container Services and the Dependency Injection Container @endlink
  577. * topic for more information about services.
  578. *
  579. * Typically, the cache context is specified as part of the #cache property
  580. * of a render array; see the Caching section of the
  581. * @link theme_render Render API overview topic @endlink for details.
  582. *
  583. * @section configuration Configuration
  584. *
  585. * By default cached data is stored in the database. This can be configured
  586. * though so that all cached data, or that of an individual cache bin, uses a
  587. * different cache backend, such as APCu or Memcache, for storage.
  588. *
  589. * In a settings.php file, you can override the service used for a particular
  590. * cache bin. For example, if your service implementation of
  591. * \Drupal\Core\Cache\CacheBackendInterface was called cache.custom, the
  592. * following line would make Drupal use it for the 'cache_render' bin:
  593. * @code
  594. * $settings['cache']['bins']['render'] = 'cache.custom';
  595. * @endcode
  596. *
  597. * Additionally, you can register your cache implementation to be used by
  598. * default for all cache bins with:
  599. * @code
  600. * $settings['cache']['default'] = 'cache.custom';
  601. * @endcode
  602. *
  603. * Finally, you can chain multiple cache backends together, see
  604. * \Drupal\Core\Cache\ChainedFastBackend and \Drupal\Core\Cache\BackendChain.
  605. *
  606. * @see https://www.drupal.org/node/1884796
  607. * @}
  608. */
  609. /**
  610. * @defgroup user_api User accounts, permissions, and roles
  611. * @{
  612. * API for user accounts, access checking, roles, and permissions.
  613. *
  614. * @section sec_overview Overview and terminology
  615. * Drupal's permission system is based on the concepts of accounts, roles,
  616. * and permissions.
  617. *
  618. * Users (site visitors) have accounts, which include a user name, an email
  619. * address, a password (or some other means of authentication), and possibly
  620. * other fields (if defined on the site). Anonymous users have an implicit
  621. * account that does not have a real user name or any account information.
  622. *
  623. * Each user account is assigned one or more roles. The anonymous user account
  624. * automatically has the anonymous user role; real user accounts
  625. * automatically have the authenticated user role, plus any roles defined on
  626. * the site that they have been assigned.
  627. *
  628. * Each role, including the special anonymous and authenticated user roles, is
  629. * granted one or more named permissions, which allow them to perform certain
  630. * tasks or view certain content on the site. It is possible to designate a
  631. * role to be the "administrator" role; if this is set up, this role is
  632. * automatically granted all available permissions whenever a module is
  633. * enabled that defines permissions.
  634. *
  635. * All code in Drupal that allows users to perform tasks or view content must
  636. * check that the current user has the correct permission before allowing the
  637. * action. In the standard case, access checking consists of answering the
  638. * question "Does the current user have permission 'foo'?", and allowing or
  639. * denying access based on the answer. Note that access checking should nearly
  640. * always be done at the permission level, not by checking for a particular role
  641. * or user ID, so that site administrators can set up user accounts and roles
  642. * appropriately for their particular sites.
  643. *
  644. * @section sec_define Defining permissions
  645. * Modules define permissions via a $module.permissions.yml file. See
  646. * \Drupal\user\PermissionHandler for documentation of permissions.yml files.
  647. *
  648. * @section sec_access Access permission checking
  649. * Depending on the situation, there are several methods for ensuring that
  650. * access checks are done properly in Drupal:
  651. * - Routes: When you register a route, include a 'requirements' section that
  652. * either gives the machine name of the permission that is needed to visit the
  653. * URL of the route, or tells Drupal to use an access check method or service
  654. * to check access. See the @link menu Routing topic @endlink for more
  655. * information.
  656. * - Entities: Access for various entity operations is designated either with
  657. * simple permissions or access control handler classes in the entity
  658. * annotation. See the @link entity_api Entity API topic @endlink for more
  659. * information.
  660. * - Other code: There is a 'current_user' service, which can be injected into
  661. * classes to provide access to the current user account (see the
  662. * @link container Services and Dependency Injection topic @endlink for more
  663. * information on dependency injection). In code that cannot use dependency
  664. * injection, you can access this service and retrieve the current user
  665. * account object by calling \Drupal::currentUser(). Once you have a user
  666. * object for the current user (implementing \Drupal\user\UserInterface), you
  667. * can call inherited method
  668. * \Drupal\Core\Session\AccountInterface::hasPermission() to check
  669. * permissions, or pass this object into other functions/methods.
  670. * - Forms: Each element of a form array can have a Boolean '#access' property,
  671. * which determines whether that element is visible and/or usable. This is a
  672. * common need in forms, so the current user service (described above) is
  673. * injected into the form base class as method
  674. * \Drupal\Core\Form\FormBase::currentUser().
  675. *
  676. * @section sec_entities User and role objects
  677. * User objects in Drupal are entity items, implementing
  678. * \Drupal\user\UserInterface. Role objects in Drupal are also entity items,
  679. * implementing \Drupal\user\RoleInterface. See the
  680. * @link entity_api Entity API topic @endlink for more information about
  681. * entities in general (including how to load, create, modify, and query them).
  682. *
  683. * Roles often need to be manipulated in automated test code, such as to add
  684. * permissions to them. Here's an example:
  685. * @code
  686. * $role = \Drupal\user\Entity\Role::load('authenticated');
  687. * $role->grantPermission('access comments');
  688. * $role->save();
  689. * @endcode
  690. *
  691. * Other important interfaces:
  692. * - \Drupal\Core\Session\AccountInterface: The part of UserInterface that
  693. * deals with access checking. In writing code that checks access, your
  694. * method parameters should use this interface, not UserInterface.
  695. * - \Drupal\Core\Session\AccountProxyInterface: The interface for the
  696. * current_user service (described above).
  697. * @}
  698. */
  699. /**
  700. * @defgroup container Services and Dependency Injection Container
  701. * @{
  702. * Overview of the Dependency Injection Container and Services.
  703. *
  704. * @section sec_overview Overview of container, injection, and services
  705. * The Services and Dependency Injection Container concepts have been adopted by
  706. * Drupal from the @link http://symfony.com/ Symfony framework. @endlink A
  707. * "service" (such as accessing the database, sending email, or translating user
  708. * interface text) is defined (given a name and an interface or at least a
  709. * class that defines the methods that may be called), and a default class is
  710. * designated to provide the service. These two steps must be done together, and
  711. * can be done by Drupal Core or a module. Other modules can then define
  712. * alternative classes to provide the same services, overriding the default
  713. * classes. Classes and functions that need to use the service should always
  714. * instantiate the class via the dependency injection container (also known
  715. * simply as the "container"), rather than instantiating a particular service
  716. * provider class directly, so that they get the correct class (default or
  717. * overridden).
  718. *
  719. * See https://www.drupal.org/node/2133171 for more detailed information on
  720. * services and the dependency injection container.
  721. *
  722. * @section sec_discover Discovering existing services
  723. * Drupal core defines many core services in the core.services.yml file (in the
  724. * top-level core directory). Some Drupal Core modules and contributed modules
  725. * also define services in modulename.services.yml files. API reference sites
  726. * (such as https://api.drupal.org) generate lists of all existing services from
  727. * these files. Look for the Services link in the API Navigation block.
  728. * Alternatively you can look through the individual files manually.
  729. *
  730. * A typical service definition in a *.services.yml file looks like this:
  731. * @code
  732. * path.alias_manager:
  733. * class: Drupal\Core\Path\AliasManager
  734. * arguments: ['@path.crud', '@path.alias_whitelist', '@language_manager']
  735. * @endcode
  736. * Some services use other services as factories; a typical service definition
  737. * is:
  738. * @code
  739. * cache.entity:
  740. * class: Drupal\Core\Cache\CacheBackendInterface
  741. * tags:
  742. * - { name: cache.bin }
  743. * factory: cache_factory:get
  744. * arguments: [entity]
  745. * @endcode
  746. *
  747. * The first line of a service definition gives the unique machine name of the
  748. * service. This is often prefixed by the module name if provided by a module;
  749. * however, by convention some service names are prefixed by a group name
  750. * instead, such as cache.* for cache bins and plugin.manager.* for plugin
  751. * managers.
  752. *
  753. * The class line either gives the default class that provides the service, or
  754. * if the service uses a factory class, the interface for the service. If the
  755. * class depends on other services, the arguments line lists the machine
  756. * names of the dependencies (preceded by '@'); objects for each of these
  757. * services are instantiated from the container and passed to the class
  758. * constructor when the service class is instantiated. Other arguments can also
  759. * be passed in; see the section at https://www.drupal.org/node/2133171 for more
  760. * detailed information.
  761. *
  762. * Services using factories can be defined as shown in the above example, if the
  763. * factory is itself a service. The factory can also be a class; details of how
  764. * to use service factories can be found in the section at
  765. * https://www.drupal.org/node/2133171.
  766. *
  767. * @section sec_container Accessing a service through the container
  768. * As noted above, if you need to use a service in your code, you should always
  769. * instantiate the service class via a call to the container, using the machine
  770. * name of the service, so that the default class can be overridden. There are
  771. * several ways to make sure this happens:
  772. * - For service-providing classes, see other sections of this documentation
  773. * describing how to pass services as arguments to the constructor.
  774. * - Plugin classes, controllers, and similar classes have create() or
  775. * createInstance() methods that are used to create an instance of the class.
  776. * These methods come from different interfaces, and have different
  777. * arguments, but they all include an argument $container of type
  778. * \Symfony\Component\DependencyInjection\ContainerInterface.
  779. * If you are defining one of these classes, in the create() or
  780. * createInstance() method, call $container->get('myservice.name') to
  781. * instantiate a service. The results of these calls are generally passed to
  782. * the class constructor and saved as member variables in the class.
  783. * - For functions and class methods that do not have access to either of
  784. * the above methods of dependency injection, you can use service location to
  785. * access services, via a call to the global \Drupal class. This class has
  786. * special methods for accessing commonly-used services, or you can call a
  787. * generic method to access any service. Examples:
  788. * @code
  789. * // Retrieve the entity.manager service object (special method exists).
  790. * $manager = \Drupal::entityManager();
  791. * // Retrieve the service object for machine name 'foo.bar'.
  792. * $foobar = \Drupal::service('foo.bar');
  793. * @endcode
  794. *
  795. * As a note, you should always use dependency injection (via service arguments
  796. * or create()/createInstance() methods) if possible to instantiate services,
  797. * rather than service location (via the \Drupal class), because:
  798. * - Dependency injection facilitates writing unit tests, since the container
  799. * argument can be mocked and the create() method can be bypassed by using
  800. * the class constructor. If you use the \Drupal class, unit tests are much
  801. * harder to write and your code has more dependencies.
  802. * - Having the service interfaces on the class constructor and member variables
  803. * is useful for IDE auto-complete and self-documentation.
  804. *
  805. * @section sec_define Defining a service
  806. * If your module needs to define a new service, here are the steps:
  807. * - Choose a unique machine name for your service. Typically, this should
  808. * start with your module name. Example: mymodule.myservice.
  809. * - Create a PHP interface to define what your service does.
  810. * - Create a default class implementing your interface that provides your
  811. * service. If your class needs to use existing services (such as database
  812. * access), be sure to make these services arguments to your class
  813. * constructor, and save them in member variables. Also, if the needed
  814. * services are provided by other modules and not Drupal Core, you'll want
  815. * these modules to be dependencies of your module.
  816. * - Add an entry to a modulename.services.yml file for the service. See
  817. * @ref sec_discover above, or existing *.services.yml files in Core, for the
  818. * syntax; it will start with your machine name, refer to your default class,
  819. * and list the services that need to be passed into your constructor.
  820. *
  821. * Services can also be defined dynamically, as in the
  822. * \Drupal\Core\CoreServiceProvider class, but this is less common for modules.
  823. *
  824. * @section sec_tags Service tags
  825. * Some services have tags, which are defined in the service definition. See
  826. * @link service_tag Service Tags @endlink for usage.
  827. *
  828. * @section sec_injection Overriding the default service class
  829. * Modules can override the default classes used for services. Here are the
  830. * steps:
  831. * - Define a class in the top-level namespace for your module
  832. * (Drupal\my_module), whose name is the camel-case version of your module's
  833. * machine name followed by "ServiceProvider" (for example, if your module
  834. * machine name is my_module, the class must be named
  835. * MyModuleServiceProvider).
  836. * - The class needs to implement
  837. * \Drupal\Core\DependencyInjection\ServiceModifierInterface, which is
  838. * typically done by extending
  839. * \Drupal\Core\DependencyInjection\ServiceProviderBase.
  840. * - The class needs to contain one method: alter(). This method does the
  841. * actual work of telling Drupal to use your class instead of the default.
  842. * Here's an example:
  843. * @code
  844. * public function alter(ContainerBuilder $container) {
  845. * // Override the language_manager class with a new class.
  846. * $definition = $container->getDefinition('language_manager');
  847. * $definition->setClass('Drupal\my_module\MyLanguageManager');
  848. * }
  849. * @endcode
  850. * Note that $container here is an instance of
  851. * \Drupal\Core\DependencyInjection\ContainerBuilder.
  852. *
  853. * @see https://www.drupal.org/node/2133171
  854. * @see core.services.yml
  855. * @see \Drupal
  856. * @see \Symfony\Component\DependencyInjection\ContainerInterface
  857. * @see plugin_api
  858. * @see menu
  859. * @}
  860. */
  861. /**
  862. * @defgroup listing_page_service Page header for Services page
  863. * @{
  864. * Introduction to services
  865. *
  866. * A "service" (such as accessing the database, sending email, or translating
  867. * user interface text) can be defined by a module or Drupal core. Defining a
  868. * service means giving it a name and designating a default class to provide the
  869. * service; ideally, there should also be an interface that defines the methods
  870. * that may be called. Services are collected into the Dependency Injection
  871. * Container, and can be overridden to use different classes or different
  872. * instantiation by modules. See the
  873. * @link container Services and Dependency Injection Container topic @endlink
  874. * for details.
  875. *
  876. * Some services have tags, which are defined in the service definition. Tags
  877. * are used to define a group of related services, or to specify some aspect of
  878. * how the service behaves. See the
  879. * @link service_tag Service Tags topic @endlink for more information.
  880. *
  881. * @see container
  882. * @see service_tag
  883. *
  884. * @}
  885. */
  886. /**
  887. * @defgroup typed_data Typed Data API
  888. * @{
  889. * API for describing data based on a set of available data types.
  890. *
  891. * PHP has data types, such as int, string, float, array, etc., and it is an
  892. * object-oriented language that lets you define classes and interfaces.
  893. * However, in some cases, it is useful to be able to define an abstract
  894. * type (as in an interface, free of implementation details), that still has
  895. * properties (which an interface cannot) as well as meta-data. The Typed Data
  896. * API provides this abstraction.
  897. *
  898. * @section sec_overview Overview
  899. * Each data type in the Typed Data API is a plugin class (annotation class
  900. * example: \Drupal\Core\TypedData\Annotation\DataType); these plugins are
  901. * managed by the typed_data_manager service (by default
  902. * \Drupal\Core\TypedData\TypedDataManager). Each data object encapsulates a
  903. * single piece of data, provides access to the metadata, and provides
  904. * validation capability. Also, the typed data plugins have a shorthand
  905. * for easily accessing data values, described in @ref sec_tree.
  906. *
  907. * The metadata of a data object is defined by an object based on a class called
  908. * the definition class (see \Drupal\Core\TypedData\DataDefinitionInterface).
  909. * The class used can vary by data type and can be specified in the data type's
  910. * plugin definition, while the default is set in the $definition_class property
  911. * of the annotation class. The default class is
  912. * \Drupal\Core\TypedData\DataDefinition. For data types provided by a plugin
  913. * deriver, the plugin deriver can set the definition_class property too.
  914. * The metadata object provides information about the data, such as the data
  915. * type, whether it is translatable, the names of its properties (for complex
  916. * types), and who can access it.
  917. *
  918. * See https://www.drupal.org/node/1794140 for more information about the Typed
  919. * Data API.
  920. *
  921. * @section sec_varieties Varieties of typed data
  922. * There are three kinds of typed data: primitive, complex, and list.
  923. *
  924. * @subsection sub_primitive Primitive data types
  925. * Primitive data types wrap PHP data types and also serve as building blocks
  926. * for complex and list typed data. Each primitive data type has an interface
  927. * that extends \Drupal\Core\TypedData\PrimitiveInterface, with getValue()
  928. * and setValue() methods for accessing the data value, and a default plugin
  929. * implementation. Here's a list:
  930. * - \Drupal\Core\TypedData\Type\IntegerInterface: Plugin ID integer,
  931. * corresponds to PHP type int.
  932. * - \Drupal\Core\TypedData\Type\StringInterface: Plugin ID string,
  933. * corresponds to PHP type string.
  934. * - \Drupal\Core\TypedData\Type\FloatInterface: Plugin ID float,
  935. * corresponds to PHP type float.
  936. * - \Drupal\Core\TypedData\Type\BooleanInterface: Plugin ID bool,
  937. * corresponds to PHP type bool.
  938. * - \Drupal\Core\TypedData\Type\BinaryInterface: Plugin ID binary,
  939. * corresponds to a PHP file resource.
  940. * - \Drupal\Core\TypedData\Type\UriInterface: Plugin ID uri.
  941. *
  942. * @subsection sec_complex Complex data
  943. * Complex data types, with interface
  944. * \Drupal\Core\TypedData\ComplexDataInterface, represent data with named
  945. * properties; the properties can be accessed with get() and set() methods.
  946. * The value of each property is itself a typed data object, which can be
  947. * primitive, complex, or list data.
  948. *
  949. * The base type for most complex data is the
  950. * \Drupal\Core\TypedData\Plugin\DataType\Map class, which represents an
  951. * associative array. Map provides its own definition class in the annotation,
  952. * \Drupal\Core\TypedData\MapDataDefinition, and most complex data classes
  953. * extend this class. The getValue() and setValue() methods on the Map class
  954. * enforce the data definition and its property structure.
  955. *
  956. * The Drupal Field API uses complex typed data for its field items, with
  957. * definition class \Drupal\Core\Field\TypedData\FieldItemDataDefinition.
  958. *
  959. * @section sec_list Lists
  960. * List data types, with interface \Drupal\Core\TypedData\ListInterface,
  961. * represent data that is an ordered list of typed data, all of the same type.
  962. * More precisely, the plugins in the list must have the same base plugin ID;
  963. * however, some types (for example field items and entities) are provided by
  964. * plugin derivatives and the sub IDs can be different.
  965. *
  966. * @section sec_tree Tree handling
  967. * Typed data allows you to use shorthand to get data values nested in the
  968. * implicit tree structure of the data. For example, to get the value from
  969. * an entity field item, the Entity Field API allows you to call:
  970. * @code
  971. * $value = $entity->fieldName->propertyName;
  972. * @endcode
  973. * This is really shorthand for:
  974. * @code
  975. * $field_item_list = $entity->get('fieldName');
  976. * $field_item = $field_item_list->get(0);
  977. * $property = $field_item->get('propertyName');
  978. * $value = $property->getValue();
  979. * @endcode
  980. * Some notes:
  981. * - $property, $field_item, and $field_item_list are all typed data objects,
  982. * while $value is a raw PHP value.
  983. * - You can call $property->getParent() to get $field_item,
  984. * $field_item->getParent() to get $field_item_list, or
  985. * $field_item_list->getParent() to get $typed_entity ($entity wrapped in a
  986. * typed data object). $typed_entity->getParent() is NULL.
  987. * - For all of these ->getRoot() returns $typed_entity.
  988. * - The langcode property is on $field_item_list, but you can access it
  989. * on $property as well, so that all items will report the same langcode.
  990. * - When the value of $property is changed by calling $property->setValue(),
  991. * $property->onChange() will fire, which in turn calls the parent object's
  992. * onChange() method and so on. This allows parent objects to react upon
  993. * changes of contained properties or list items.
  994. *
  995. * @section sec_defining Defining data types
  996. * To define a new data type:
  997. * - Create a class that implements one of the Typed Data interfaces.
  998. * Typically, you will want to extend one of the classes listed in the
  999. * sections above as a starting point.
  1000. * - Make your class into a DataType plugin. To do that, put it in namespace
  1001. * \Drupal\yourmodule\Plugin\DataType (where "yourmodule" is your module's
  1002. * short name), and add annotation of type
  1003. * \Drupal\Core\TypedData\Annotation\DataType to the documentation header.
  1004. * See the @link plugin_api Plugin API topic @endlink and the
  1005. * @link annotation Annotations topic @endlink for more information.
  1006. *
  1007. * @section sec_using Using data types
  1008. * The data types of the Typed Data API can be used in several ways, once they
  1009. * have been defined:
  1010. * - In the Field API, data types can be used as the class in the property
  1011. * definition of the field. See the @link field Field API topic @endlink for
  1012. * more information.
  1013. * - In configuration schema files, you can use the unique ID ('id' annotation)
  1014. * from any DataType plugin class as the 'type' value for an entry. See the
  1015. * @link config_api Confuration API topic @endlink for more information.
  1016. * - If you need to create a typed data object in code, first get the
  1017. * typed_data_manager service from the container or by calling
  1018. * \Drupal::typedDataManager(). Then pass the plugin ID to
  1019. * $manager::createDataDefinition() to create an appropriate data definition
  1020. * object. Then pass the data definition object and the value of the data to
  1021. * $manager::create() to create a typed data object.
  1022. *
  1023. * @see plugin_api
  1024. * @see container
  1025. * @}
  1026. */
  1027. /**
  1028. * @defgroup testing Automated tests
  1029. * @{
  1030. * Overview of PHPUnit tests and Simpletest tests.
  1031. *
  1032. * The Drupal project has embraced a philosophy of using automated tests,
  1033. * consisting of both unit tests (which test the functionality of classes at a
  1034. * low level) and functional tests (which test the functionality of Drupal
  1035. * systems at a higher level, usually involving web output). The goal is to
  1036. * have test coverage for all or most of the components and features, and to
  1037. * run the automated tests before any code is changed or added, to make sure
  1038. * it doesn't break any existing functionality (regression testing).
  1039. *
  1040. * In order to implement this philosophy, developers need to do the following:
  1041. * - When making a patch to fix a bug, make sure that the bug fix patch includes
  1042. * a test that fails without the code change and passes with the code change.
  1043. * This helps reviewers understand what the bug is, demonstrates that the code
  1044. * actually fixes the bug, and ensures the bug will not reappear due to later
  1045. * code changes.
  1046. * - When making a patch to implement a new feature, include new unit and/or
  1047. * functional tests in the patch. This serves to both demonstrate that the
  1048. * code actually works, and ensure that later changes do not break the new
  1049. * functionality.
  1050. *
  1051. * @section write_unit Writing PHPUnit tests for classes
  1052. * PHPUnit tests for classes are written using the industry-standard PHPUnit
  1053. * framework. Use a PHPUnit test to test functionality of a class if the Drupal
  1054. * environment (database, settings, etc.) and web browser are not needed for the
  1055. * test, or if the Drupal environment can be replaced by a "mock" object. To
  1056. * write a PHPUnit test:
  1057. * - Define a class that extends \Drupal\Tests\UnitTestCase.
  1058. * - The class name needs to end in the word Test.
  1059. * - The namespace must be a subspace/subdirectory of \Drupal\yourmodule\Tests,
  1060. * where yourmodule is your module's machine name.
  1061. * - The test class file must be named and placed under the
  1062. * yourmodule/tests/src/Unit directory, according to the PSR-4 standard.
  1063. * - Your test class needs a phpDoc comment block with a description and
  1064. * a @group annotation, which gives information about the test.
  1065. * - Methods in your test class whose names start with 'test' are the actual
  1066. * test cases. Each one should test a logical subset of the functionality.
  1067. * For more details, see:
  1068. * - https://www.drupal.org/phpunit for full documentation on how to write
  1069. * PHPUnit tests for Drupal.
  1070. * - http://phpunit.de for general information on the PHPUnit framework.
  1071. * - @link oo_conventions Object-oriented programming topic @endlink for more
  1072. * on PSR-4, namespaces, and where to place classes.
  1073. *
  1074. * @section write_functional Writing functional tests
  1075. * Functional tests are written using a Drupal-specific framework that is, for
  1076. * historical reasons, known as "Simpletest". Use a Simpletest test to test the
  1077. * functionality of sub-system of Drupal, if the functionality depends on the
  1078. * Drupal database and settings, or to test the web output of Drupal. To
  1079. * write a Simpletest test:
  1080. * - For functional tests of the web output of Drupal, define a class that
  1081. * extends \Drupal\simpletest\WebTestBase, which contains an internal web
  1082. * browser and defines many helpful test assertion methods that you can use
  1083. * in your tests. You can specify modules to be enabled by defining a
  1084. * $modules member variable -- keep in mind that by default, WebTestBase uses
  1085. * a "testing" install profile, with a minimal set of modules enabled.
  1086. * - For functional tests that do not test web output, define a class that
  1087. * extends \Drupal\KernelTests\KernelTestBase. This class is much faster
  1088. * than WebTestBase, because instead of making a full install of Drupal, it
  1089. * uses an in-memory pseudo-installation (similar to what the installer and
  1090. * update scripts use). To use this test class, you will need to create the
  1091. * database tables you need and install needed modules manually.
  1092. * - The namespace must be a subspace/subdirectory of \Drupal\yourmodule\Tests,
  1093. * where yourmodule is your module's machine name.
  1094. * - The test class file must be named and placed under the yourmodule/src/Tests
  1095. * directory, according to the PSR-4 standard.
  1096. * - Your test class needs a phpDoc comment block with a description and
  1097. * a @group annotation, which gives information about the test.
  1098. * - You may also override the default setUp() method, which can set be used to
  1099. * set up content types and similar procedures.
  1100. * - In some cases, you may need to write a test module to support your test;
  1101. * put such modules under the yourmodule/tests/modules directory.
  1102. * - Methods in your test class whose names start with 'test', and which have
  1103. * no arguments, are the actual test cases. Each one should test a logical
  1104. * subset of the functionality, and each one runs in a new, isolated test
  1105. * environment, so it can only rely on the setUp() method, not what has
  1106. * been set up by other test methods.
  1107. * For more details, see:
  1108. * - https://www.drupal.org/simpletest for full documentation on how to write
  1109. * functional tests for Drupal.
  1110. * - @link oo_conventions Object-oriented programming topic @endlink for more
  1111. * on PSR-4, namespaces, and where to place classes.
  1112. *
  1113. * @section running Running tests
  1114. * You can run both Simpletest and PHPUnit tests by enabling the core Testing
  1115. * module (core/modules/simpletest). Once that module is enabled, tests can be
  1116. * run using the core/scripts/run-tests.sh script, using
  1117. * @link https://www.drupal.org/project/drush Drush @endlink, or from the
  1118. * Testing module user interface.
  1119. *
  1120. * PHPUnit tests can also be run from the command line, using the PHPUnit
  1121. * framework. See https://www.drupal.org/node/2116263 for more information.
  1122. * @}
  1123. */
  1124. /**
  1125. * @defgroup php_assert PHP Runtime Assert Statements
  1126. * @{
  1127. * Use of the assert() statement in Drupal.
  1128. *
  1129. * Unit tests also use the term "assertion" to refer to test conditions, so to
  1130. * avoid confusion the term "runtime assertion" will be used for the assert()
  1131. * statement throughout the documentation.
  1132. *
  1133. * A runtime assertion is a statement that is expected to always be true at
  1134. * the point in the code it appears at. They are tested using PHP's internal
  1135. * @link http://php.net/assert assert() @endlink statement. If an
  1136. * assertion is ever FALSE it indicates an error in the code or in module or
  1137. * theme configuration files. User-provided configuration files should be
  1138. * verified with standard control structures at all times, not just checked in
  1139. * development environments with assert() statements on.
  1140. *
  1141. * When runtime assertions fail in PHP 7 an \AssertionError is thrown.
  1142. * Drupal uses an assertion callback to do the same in PHP 5.x so that unit
  1143. * tests involving runtime assertions will work uniformly across both versions.
  1144. *
  1145. * The Drupal project primarily uses runtime assertions to enforce the
  1146. * expectations of the API by failing when incorrect calls are made by code
  1147. * under development. While PHP type hinting does this for objects and arrays,
  1148. * runtime assertions do this for scalars (strings, integers, floats, etc.) and
  1149. * complex data structures such as cache and render arrays. They ensure that
  1150. * methods' return values are the documented datatypes. They also verify that
  1151. * objects have been properly configured and set up by the service container.
  1152. * Runtime assertions are checked throughout development. They supplement unit
  1153. * tests by checking scenarios that do not have unit tests written for them,
  1154. * and by testing the API calls made by all the code in the system.
  1155. *
  1156. * When using assert() keep the following in mind:
  1157. * - Runtime assertions are disabled by default in production and enabled in
  1158. * development, so they can't be used as control structures. Use exceptions
  1159. * for errors that can occur in production no matter how unlikely they are.
  1160. * - Assert() functions in a buggy manner prior to PHP 7. If you do not use a
  1161. * string for the first argument of the statement but instead use a function
  1162. * call or expression then that code will be evaluated even when runtime
  1163. * assertions are turned off. To avoid this you must use a string as the
  1164. * first argument, and assert will pass this string to the eval() statement.
  1165. * - Since runtime assertion strings are parsed by eval() use caution when
  1166. * using them to work with data that may be unsanitized.
  1167. *
  1168. * See https://www.drupal.org/node/2492225 for more information on runtime
  1169. * assertions.
  1170. * @}
  1171. */
  1172. /**
  1173. * @defgroup info_types Information types
  1174. * @{
  1175. * Types of information in Drupal.
  1176. *
  1177. * Drupal has several distinct types of information, each with its own methods
  1178. * for storage and retrieval:
  1179. * - Content: Information meant to be displayed on your site: articles, basic
  1180. * pages, images, files, custom blocks, etc. Content is stored and accessed
  1181. * using @link entity_api Entities @endlink.
  1182. * - Session: Information about individual users' interactions with the site,
  1183. * such as whether they are logged in. This is really "state" information, but
  1184. * it is not stored the same way so it's a separate type here. Session
  1185. * information is available from the Request object. The session implements
  1186. * \Symfony\Component\HttpFoundation\Session\SessionInterface.
  1187. * - State: Information of a temporary nature, generally machine-generated and
  1188. * not human-edited, about the current state of your site. Examples: the time
  1189. * when Cron was last run, whether node access permissions need rebuilding,
  1190. * etc. See @link state_api the State API topic @endlink for more information.
  1191. * - Configuration: Information about your site that is generally (or at least
  1192. * can be) human-edited, but is not Content, and is meant to be relatively
  1193. * permanent. Examples: the name of your site, the content types and views
  1194. * you have defined, etc. See
  1195. * @link config_api the Configuration API topic @endlink for more information.
  1196. *
  1197. * @see cache
  1198. * @see i18n
  1199. * @}
  1200. */
  1201. /**
  1202. * @defgroup extending Extending and altering Drupal
  1203. * @{
  1204. * Overview of extensions and alteration methods for Drupal.
  1205. *
  1206. * @section sec_types Types of extensions
  1207. * Drupal's core behavior can be extended and altered via these three basic
  1208. * types of extensions:
  1209. * - Themes: Themes alter the appearance of Drupal sites. They can include
  1210. * template files, which alter the HTML markup and other raw output of the
  1211. * site; CSS files, which alter the styling applied to the HTML; and
  1212. * JavaScript, Flash, images, and other files. For more information, see the
  1213. * @link theme_render Theme system and render API topic @endlink and
  1214. * https://www.drupal.org/theme-guide/8
  1215. * - Modules: Modules add to or alter the behavior and functionality of Drupal,
  1216. * by using one or more of the methods listed below. For more information
  1217. * about creating modules, see https://www.drupal.org/developing/modules/8
  1218. * - Installation profiles: Installation profiles can be used to
  1219. * create distributions, which are complete specific-purpose packages of
  1220. * Drupal including additional modules, themes, and data. For more
  1221. * information, see https://www.drupal.org/developing/distributions.
  1222. *
  1223. * @section sec_alter Alteration methods for modules
  1224. * Here is a list of the ways that modules can alter or extend Drupal's core
  1225. * behavior, or the behavior of other modules:
  1226. * - Hooks: Specially-named functions that a module defines, which are
  1227. * discovered and called at specific times, usually to alter behavior or data.
  1228. * See the @link hooks Hooks topic @endlink for more information.
  1229. * - Plugins: Classes that a module defines, which are discovered and
  1230. * instantiated at specific times to add functionality. See the
  1231. * @link plugin_api Plugin API topic @endlink for more information.
  1232. * - Entities: Special plugins that define entity types for storing new types
  1233. * of content or configuration in Drupal. See the
  1234. * @link entity_api Entity API topic @endlink for more information.
  1235. * - Services: Classes that perform basic operations within Drupal, such as
  1236. * accessing the database and sending email. See the
  1237. * @link container Dependency Injection Container and Services topic @endlink
  1238. * for more information.
  1239. * - Routing: Providing or altering "routes", which are URLs that Drupal
  1240. * responds to, or altering routing behavior with event listener classes.
  1241. * See the @link menu Routing and menu topic @endlink for more information.
  1242. * - Events: Modules can register as event subscribers; when an event is
  1243. * dispatched, a method is called on each registered subscriber, allowing each
  1244. * one to react. See the @link events Events topic @endlink for more
  1245. * information.
  1246. *
  1247. * @section sec_sample *.info.yml files
  1248. * Extensions must each be located in a directory whose name matches the short
  1249. * name (or machine name) of the extension, and this directory must contain a
  1250. * file named machine_name.info.yml (where machine_name is the machine name of
  1251. * the extension). See \Drupal\Core\Extension\InfoParserInterface::parse() for
  1252. * documentation of the format of .info.yml files.
  1253. * @}
  1254. */
  1255. /**
  1256. * @defgroup plugin_api Plugin API
  1257. * @{
  1258. * Using the Plugin API
  1259. *
  1260. * @section sec_overview Overview and terminology
  1261. *
  1262. * The basic idea of plugins is to allow a particular module or subsystem of
  1263. * Drupal to provide functionality in an extensible, object-oriented way. The
  1264. * controlling module or subsystem defines the basic framework (interface) for
  1265. * the functionality, and other modules can create plugins (implementing the
  1266. * interface) with particular behaviors. The controlling module instantiates
  1267. * existing plugins as needed, and calls methods to invoke their functionality.
  1268. * Examples of functionality in Drupal Core that use plugins include: the block
  1269. * system (block types are plugins), the entity/field system (entity types,
  1270. * field types, field formatters, and field widgets are plugins), the image
  1271. * manipulation system (image effects and image toolkits are plugins), and the
  1272. * search system (search page types are plugins).
  1273. *
  1274. * Plugins are grouped into plugin types, each generally defined by an
  1275. * interface. Each plugin type is managed by a plugin manager service, which
  1276. * uses a plugin discovery method to discover provided plugins of that type and
  1277. * instantiate them using a plugin factory.
  1278. *
  1279. * Some plugin types make use of the following concepts or components:
  1280. * - Plugin derivatives: Allows a single plugin class to present itself as
  1281. * multiple plugins. Example: the Menu module provides a block for each
  1282. * defined menu via a block plugin derivative.
  1283. * - Plugin mapping: Allows a plugin class to map a configuration string to an
  1284. * instance, and have the plugin automatically instantiated without writing
  1285. * additional code.
  1286. * - Plugin collections: Provide a way to lazily instantiate a set of plugin
  1287. * instances from a single plugin definition.
  1288. *
  1289. * There are several things a module developer may need to do with plugins:
  1290. * - Define a completely new plugin type: see @ref sec_define below.
  1291. * - Create a plugin of an existing plugin type: see @ref sec_create below.
  1292. * - Perform tasks that involve plugins: see @ref sec_use below.
  1293. *
  1294. * See https://www.drupal.org/developing/api/8/plugins for more detailed
  1295. * documentation on the plugin system. There are also topics for a few
  1296. * of the many existing types of plugins:
  1297. * - @link block_api Block API @endlink
  1298. * - @link entity_api Entity API @endlink
  1299. * - @link field Various types of field-related plugins @endlink
  1300. * - @link views_plugins Views plugins @endlink (has links to topics covering
  1301. * various specific types of Views plugins).
  1302. * - @link search Search page plugins @endlink
  1303. *
  1304. * @section sec_define Defining a new plugin type
  1305. * To define a new plugin type:
  1306. * - Define an interface for the plugin. This describes the common set of
  1307. * behavior, and the methods you will call on each plugin class that is
  1308. * instantiated. Usually this interface will extend one or more of the
  1309. * following interfaces:
  1310. * - \Drupal\Component\Plugin\PluginInspectionInterface
  1311. * - \Drupal\Component\Plugin\ConfigurablePluginInterface
  1312. * - \Drupal\Component\Plugin\ContextAwarePluginInterface
  1313. * - \Drupal\Core\Plugin\PluginFormInterface
  1314. * - \Drupal\Core\Executable\ExecutableInterface
  1315. * - (optional) Create a base class that provides a partial implementation of
  1316. * the interface, for the convenience of developers wishing to create plugins
  1317. * of your type. The base class usually extends
  1318. * \Drupal\Core\Plugin\PluginBase, or one of the base classes that extends
  1319. * this class.
  1320. * - Choose a method for plugin discovery, and define classes as necessary.
  1321. * See @ref sub_discovery below.
  1322. * - Create a plugin manager/factory class and service, which will discover and
  1323. * instantiate plugins. See @ref sub_manager below.
  1324. * - Use the plugin manager to instantiate plugins. Call methods on your plugin
  1325. * interface to perform the tasks of your plugin type.
  1326. * - (optional) If appropriate, define a plugin collection. See @ref
  1327. * sub_collection below for more information.
  1328. *
  1329. * @subsection sub_discovery Plugin discovery
  1330. * Plugin discovery is the process your plugin manager uses to discover the
  1331. * individual plugins of your type that have been defined by your module and
  1332. * other modules. Plugin discovery methods are classes that implement
  1333. * \Drupal\Component\Plugin\Discovery\DiscoveryInterface. Most plugin types use
  1334. * one of the following discovery mechanisms:
  1335. * - Annotation: Plugin classes are annotated and placed in a defined namespace
  1336. * subdirectory. Most Drupal Core plugins use this method of discovery.
  1337. * - Hook: Plugin modules need to implement a hook to tell the manager about
  1338. * their plugins.
  1339. * - YAML: Plugins are listed in YAML files. Drupal Core uses this method for
  1340. * discovering local tasks and local actions. This is mainly useful if all
  1341. * plugins use the same class, so it is kind of like a global derivative.
  1342. * - Static: Plugin classes are registered within the plugin manager class
  1343. * itself. Static discovery is only useful if modules cannot define new
  1344. * plugins of this type (if the list of available plugins is static).
  1345. *
  1346. * It is also possible to define your own custom discovery mechanism or mix
  1347. * methods together. And there are many more details, such as annotation
  1348. * decorators, that apply to some of the discovery methods. See
  1349. * https://www.drupal.org/developing/api/8/plugins for more details.
  1350. *
  1351. * The remainder of this documentation will assume Annotation-based discovery,
  1352. * since this is the most common method.
  1353. *
  1354. * @subsection sub_manager Defining a plugin manager class and service
  1355. * To define an annotation-based plugin manager:
  1356. * - Choose a namespace subdirectory for your plugin. For example, search page
  1357. * plugins go in directory Plugin/Search under the module namespace.
  1358. * - Define an annotation class for your plugin type. This class should extend
  1359. * \Drupal\Component\Annotation\Plugin, and for most plugin types, it should
  1360. * contain member variables corresponding to the annotations plugins will
  1361. * need to provide. All plugins have at least $id: a unique string
  1362. * identifier.
  1363. * - Define an alter hook for altering the discovered plugin definitions. You
  1364. * should document the hook in a *.api.php file.
  1365. * - Define a plugin manager class. This class should implement
  1366. * \Drupal\Component\Plugin\PluginManagerInterface; most plugin managers do
  1367. * this by extending \Drupal\Core\Plugin\DefaultPluginManager. If you do
  1368. * extend the default plugin manager, the only method you will probably need
  1369. * to define is the class constructor, which will need to call the parent
  1370. * constructor to provide information about the annotation class and plugin
  1371. * namespace for discovery, set up the alter hook, and possibly set up
  1372. * caching. See classes that extend DefaultPluginManager for examples.
  1373. * - Define a service for your plugin manager. See the
  1374. * @link container Services topic for more information. @endlink Your service
  1375. * definition should look something like this, referencing your manager
  1376. * class and the parent (default) plugin manager service to inherit
  1377. * constructor arguments:
  1378. * @code
  1379. * plugin.manager.mymodule:
  1380. * class: Drupal\mymodule\MyPluginManager
  1381. * parent: default_plugin_manager
  1382. * @endcode
  1383. * - If your plugin is configurable, you will also need to define the
  1384. * configuration schema and possibly a configuration entity type. See the
  1385. * @link config_api Configuration API topic @endlink for more information.
  1386. *
  1387. * @subsection sub_collection Defining a plugin collection
  1388. * Some configurable plugin types allow administrators to create zero or more
  1389. * instances of each plugin, each with its own configuration. For example,
  1390. * a single block plugin can be configured several times, to display in
  1391. * different regions of a theme, with different visibility settings, a
  1392. * different title, or other plugin-specific settings. To make this possible,
  1393. * a plugin type can make use of what's known as a plugin collection.
  1394. *
  1395. * A plugin collection is a class that extends
  1396. * \Drupal\Component\Plugin\LazyPluginCollection or one of its subclasses; there
  1397. * are several examples in Drupal Core. If your plugin type uses a plugin
  1398. * collection, it will usually also have a configuration entity, and the entity
  1399. * class should implement
  1400. * \Drupal\Core\Entity\EntityWithPluginCollectionInterface. Again, there are
  1401. * several examples in Drupal Core; see also the @link config_api Configuration
  1402. * API topic @endlink for more information about configuration entities.
  1403. *
  1404. * @section sec_create Creating a plugin of an existing type
  1405. * Assuming the plugin type uses annotation-based discovery, in order to create
  1406. * a plugin of an existing type, you will be creating a class. This class must:
  1407. * - Implement the plugin interface, so that it has the required methods
  1408. * defined. Usually, you'll want to extend the plugin base class, if one has
  1409. * been provided.
  1410. * - Have the right annotation in its documentation header. See the
  1411. * @link annotation Annotation topic @endlink for more information about
  1412. * annotation.
  1413. * - Be in the right plugin namespace, in order to be discovered.
  1414. * Often, the easiest way to make sure this happens is to find an existing
  1415. * example of a working plugin class of the desired type, and copy it into your
  1416. * module as a starting point.
  1417. *
  1418. * You can also create a plugin derivative, which allows your plugin class
  1419. * to present itself to the user interface as multiple plugins. To do this,
  1420. * in addition to the plugin class, you'll need to create a separate plugin
  1421. * derivative class implementing
  1422. * \Drupal\Component\Plugin\Derivative\DerivativeInterface. The classes
  1423. * \Drupal\system\Plugin\Block\SystemMenuBlock (plugin class) and
  1424. * \Drupal\system\Plugin\Derivative\SystemMenuBlock (derivative class) are a
  1425. * good example to look at.
  1426. *
  1427. * @section sec_use Performing tasks involving plugins
  1428. * Here are the steps to follow to perform a task that involves plugins:
  1429. * - Locate the machine name of the plugin manager service, and instantiate the
  1430. * service. See the @link container Services topic @endlink for more
  1431. * information on how to do this.
  1432. * - On the plugin manager class, use methods like getDefinition(),
  1433. * getDefinitions(), or other methods specific to particular plugin managers
  1434. * to retrieve information about either specific plugins or the entire list of
  1435. * defined plugins.
  1436. * - Call the createInstance() method on the plugin manager to instantiate
  1437. * individual plugin objects.
  1438. * - Call methods on the plugin objects to perform the desired tasks.
  1439. *
  1440. * @see annotation
  1441. * @}
  1442. */
  1443. /**
  1444. * @defgroup oo_conventions Objected-oriented programming conventions
  1445. * @{
  1446. * PSR-4, namespaces, class naming, and other conventions.
  1447. *
  1448. * A lot of the PHP code in Drupal is object oriented (OO), making use of
  1449. * @link http://php.net/manual/language.oop5.php PHP classes, interfaces, and traits @endlink
  1450. * (which are loosely referred to as "classes" in the rest of this topic). The
  1451. * following conventions and standards apply to this version of Drupal:
  1452. * - Each class must be in its own file.
  1453. * - Classes must be namespaced. If a module defines a class, the namespace
  1454. * must start with \Drupal\module_name. If it is defined by Drupal Core for
  1455. * use across many modules, the namespace should be \Drupal\Core or
  1456. * \Drupal\Component, with the exception of the global class \Drupal. See
  1457. * https://www.drupal.org/node/1353118 for more about namespaces.
  1458. * - In order for the PSR-4-based class auto-loader to find the class, it must
  1459. * be located in a directory corresponding to the namespace. For
  1460. * module-defined classes, if the namespace is \Drupal\module_name\foo\bar,
  1461. * then the class goes under the main module directory in directory
  1462. * src/foo/bar. For Drupal-wide classes, if the namespace is
  1463. * \Drupal\Core\foo\bar, then it goes in directory
  1464. * core/lib/Drupal/Core/foo/bar. See https://www.drupal.org/node/2156625 for
  1465. * more information about PSR-4.
  1466. * - Some classes have annotations added to their documentation headers. See
  1467. * the @link annotation Annotation topic @endlink for more information.
  1468. * - Standard plugin discovery requires particular namespaces and annotation
  1469. * for most plugin classes. See the
  1470. * @link plugin_api Plugin API topic @endlink for more information.
  1471. * - There are project-wide coding standards for OO code, including naming:
  1472. * https://www.drupal.org/node/608152
  1473. * - Documentation standards for classes are covered on:
  1474. * https://www.drupal.org/coding-standards/docs#classes
  1475. * @}
  1476. */
  1477. /**
  1478. * @defgroup listing_page_class Page header for Classes page
  1479. * @{
  1480. * Introduction to classes
  1481. *
  1482. * A lot of the PHP code in Drupal is object oriented (OO), making use of
  1483. * @link http://php.net/manual/language.oop5.php PHP classes, interfaces, and traits. @endlink
  1484. * See the
  1485. * @link oo_conventions Objected-oriented programming conventions @endlink
  1486. * for more information.
  1487. *
  1488. * @see oo_conventions
  1489. *
  1490. * @}
  1491. */
  1492. /**
  1493. * @defgroup listing_page_namespace Page header for Namespaces page
  1494. * @{
  1495. * Introduction to namespaces
  1496. *
  1497. * PHP classes, interfaces, and traits in Drupal are
  1498. * @link http://php.net/manual/en/language.namespaces.rationale.php namespaced. @endlink
  1499. * See the
  1500. * @link oo_conventions Objected-oriented programming conventions @endlink
  1501. * for more information.
  1502. *
  1503. * @see oo_conventions
  1504. *
  1505. * @}
  1506. */
  1507. /**
  1508. * @defgroup best_practices Best practices for developers
  1509. * @{
  1510. * Overview of standards and best practices for developers
  1511. *
  1512. * Ideally, all code that is included in Drupal Core and contributed modules,
  1513. * themes, and distributions will be secure, internationalized, maintainable,
  1514. * and efficient. In order to facilitate this, the Drupal community has
  1515. * developed a set of guidelines and standards for developers to follow. Most of
  1516. * these standards can be found under
  1517. * @link https://www.drupal.org/developing/best-practices Best practices on Drupal.org @endlink
  1518. *
  1519. * Standards and best practices that developers should be aware of include:
  1520. * - Security: https://www.drupal.org/writing-secure-code and the
  1521. * @link sanitization Sanitization functions topic @endlink
  1522. * - Coding standards: https://www.drupal.org/coding-standards
  1523. * and https://www.drupal.org/coding-standards/docs
  1524. * - Accessibility: https://www.drupal.org/node/1637990 (modules) and
  1525. * https://www.drupal.org/node/464472 (themes)
  1526. * - Usability: https://www.drupal.org/ui-standards
  1527. * - Internationalization: @link i18n Internationalization topic @endlink
  1528. * - Automated testing: @link testing Automated tests topic @endlink
  1529. * @}
  1530. */
  1531. /**
  1532. * @defgroup utility Utility classes and functions
  1533. * @{
  1534. * Overview of utility classes and functions for developers.
  1535. *
  1536. * Drupal provides developers with a variety of utility functions that make it
  1537. * easier and more efficient to perform tasks that are either really common,
  1538. * tedious, or difficult. Utility functions help to reduce code duplication and
  1539. * should be used in place of one-off code whenever possible.
  1540. *
  1541. * @see common.inc
  1542. * @see file
  1543. * @see format
  1544. * @see php_wrappers
  1545. * @see sanitization
  1546. * @see transliteration
  1547. * @see validation
  1548. * @}
  1549. */
  1550. /**
  1551. * @defgroup hooks Hooks
  1552. * @{
  1553. * Define functions that alter the behavior of Drupal core.
  1554. *
  1555. * One way for modules to alter the core behavior of Drupal (or another module)
  1556. * is to use hooks. Hooks are specially-named functions that a module defines
  1557. * (this is known as "implementing the hook"), which are discovered and called
  1558. * at specific times to alter or add to the base behavior or data (this is
  1559. * known as "invoking the hook"). Each hook has a name (example:
  1560. * hook_batch_alter()), a defined set of parameters, and a defined return value.
  1561. * Your modules can implement hooks that are defined by Drupal core or other
  1562. * modules that they interact with. Your modules can also define their own
  1563. * hooks, in order to let other modules interact with them.
  1564. *
  1565. * To implement a hook:
  1566. * - Locate the documentation for the hook. Hooks are documented in *.api.php
  1567. * files, by defining functions whose name starts with "hook_" (these
  1568. * files and their functions are never loaded by Drupal -- they exist solely
  1569. * for documentation). The function should have a documentation header, as
  1570. * well as a sample function body. For example, in the core file
  1571. * system.api.php, you can find hooks such as hook_batch_alter(). Also, if
  1572. * you are viewing this documentation on an API reference site, the Core
  1573. * hooks will be listed in this topic.
  1574. * - Copy the function to your module's .module file.
  1575. * - Change the name of the function, substituting your module's short name
  1576. * (name of the module's directory, and .info.yml file without the extension)
  1577. * for the "hook" part of the sample function name. For instance, to implement
  1578. * hook_batch_alter(), you would rename it to my_module_batch_alter().
  1579. * - Edit the documentation for the function (normally, your implementation
  1580. * should just have one line saying "Implements hook_batch_alter().").
  1581. * - Edit the body of the function, substituting in what you need your module
  1582. * to do.
  1583. *
  1584. * To define a hook:
  1585. * - Choose a unique name for your hook. It should start with "hook_", followed
  1586. * by your module's short name.
  1587. * - Provide documentation in a *.api.php file in your module's main
  1588. * directory. See the "implementing" section above for details of what this
  1589. * should contain (parameters, return value, and sample function body).
  1590. * - Invoke the hook in your module's code.
  1591. *
  1592. * To invoke a hook, use methods on
  1593. * \Drupal\Core\Extension\ModuleHandlerInterface such as alter(), invoke(),
  1594. * and invokeAll(). You can obtain a module handler by calling
  1595. * \Drupal::moduleHandler(), or getting the 'module_handler' service on an
  1596. * injected container.
  1597. *
  1598. * @see extending
  1599. * @see themeable
  1600. * @see callbacks
  1601. * @see \Drupal\Core\Extension\ModuleHandlerInterface
  1602. * @see \Drupal::moduleHandler()
  1603. *
  1604. * @}
  1605. */
  1606. /**
  1607. * @defgroup callbacks Callbacks
  1608. * @{
  1609. * Callback function signatures.
  1610. *
  1611. * Drupal's API sometimes uses callback functions to allow you to define how
  1612. * some type of processing happens. A callback is a function with a defined
  1613. * signature, which you define in a module. Then you pass the function name as
  1614. * a parameter to a Drupal API function or return it as part of a hook
  1615. * implementation return value, and your function is called at an appropriate
  1616. * time. For instance, when setting up batch processing you might need to
  1617. * provide a callback function for each processing step and/or a callback for
  1618. * when processing is finished; you would do that by defining these functions
  1619. * and passing their names into the batch setup function.
  1620. *
  1621. * Callback function signatures, like hook definitions, are described by
  1622. * creating and documenting dummy functions in a *.api.php file; normally, the
  1623. * dummy callback function's name should start with "callback_", and you should
  1624. * document the parameters and return value and provide a sample function body.
  1625. * Then your API documentation can refer to this callback function in its
  1626. * documentation. A user of your API can usually name their callback function
  1627. * anything they want, although a standard name would be to replace "callback_"
  1628. * with the module name.
  1629. *
  1630. * @see hooks
  1631. * @see themeable
  1632. *
  1633. * @}
  1634. */
  1635. /**
  1636. * @defgroup form_api Form generation
  1637. * @{
  1638. * Describes how to generate and manipulate forms and process form submissions.
  1639. *
  1640. * Drupal provides a Form API in order to achieve consistency in its form
  1641. * processing and presentation, while simplifying code and reducing the amount
  1642. * of HTML that must be explicitly generated by a module.
  1643. *
  1644. * @section generating_forms Creating forms
  1645. * Forms are defined as classes that implement the
  1646. * \Drupal\Core\Form\FormInterface and are built using the
  1647. * \Drupal\Core\Form\FormBuilder class. Drupal provides a couple of utility
  1648. * classes that can be extended as a starting point for most basic forms, the
  1649. * most commonly used of which is \Drupal\Core\Form\FormBase. FormBuilder
  1650. * handles the low level processing of forms such as rendering the necessary
  1651. * HTML, initial processing of incoming $_POST data, and delegating to your
  1652. * implementation of FormInterface for validation and processing of submitted
  1653. * data.
  1654. *
  1655. * Here is an example of a Form class:
  1656. * @code
  1657. * namespace Drupal\mymodule\Form;
  1658. *
  1659. * use Drupal\Core\Form\FormBase;
  1660. * use Drupal\Core\Form\FormStateInterface;
  1661. *
  1662. * class ExampleForm extends FormBase {
  1663. * public function getFormId() {
  1664. * // Unique ID of the form.
  1665. * return 'example_form';
  1666. * }
  1667. *
  1668. * public function buildForm(array $form, FormStateInterface $form_state) {
  1669. * // Create a $form API array.
  1670. * $form['phone_number'] = array(
  1671. * '#type' => 'tel',
  1672. * '#title' => $this->t('Your phone number')
  1673. * );
  1674. * return $form;
  1675. * }
  1676. *
  1677. * public function validateForm(array &$form, FormStateInterface $form_state) {
  1678. * // Validate submitted form data.
  1679. * }
  1680. *
  1681. * public function submitForm(array &$form, FormStateInterface $form_state) {
  1682. * // Handle submitted form data.
  1683. * }
  1684. * }
  1685. * @endcode
  1686. *
  1687. * @section retrieving_forms Retrieving and displaying forms
  1688. * \Drupal::formBuilder()->getForm() should be used to handle retrieving,
  1689. * processing, and displaying a rendered HTML form. Given the ExampleForm
  1690. * defined above,
  1691. * \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\ExampleForm') would
  1692. * return the rendered HTML of the form defined by ExampleForm::buildForm(), or
  1693. * call the validateForm() and submitForm(), methods depending on the current
  1694. * processing state.
  1695. *
  1696. * The argument to \Drupal::formBuilder()->getForm() is the name of a class that
  1697. * implements FormBuilderInterface. Any additional arguments passed to the
  1698. * getForm() method will be passed along as additional arguments to the
  1699. * ExampleForm::buildForm() method.
  1700. *
  1701. * For example:
  1702. * @code
  1703. * $extra = '612-123-4567';
  1704. * $form = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\ExampleForm', $extra);
  1705. * ...
  1706. * public function buildForm(array $form, FormStateInterface $form_state, $extra = NULL)
  1707. * $form['phone_number'] = array(
  1708. * '#type' => 'tel',
  1709. * '#title' => $this->t('Your phone number'),
  1710. * '#value' => $extra,
  1711. * );
  1712. * return $form;
  1713. * }
  1714. * @endcode
  1715. *
  1716. * Alternatively, forms can be built directly via the routing system which will
  1717. * take care of calling \Drupal::formBuilder()->getForm(). The following example
  1718. * demonstrates the use of a routing.yml file to display a form at the given
  1719. * route.
  1720. *
  1721. * @code
  1722. * example.form:
  1723. * path: '/example-form'
  1724. * defaults:
  1725. * _title: 'Example form'
  1726. * _form: '\Drupal\mymodule\Form\ExampleForm'
  1727. * @endcode
  1728. *
  1729. * The $form argument to form-related functions is a specialized render array
  1730. * containing the elements and properties of the form. For more about render
  1731. * arrays, see the @link theme_render Render API topic. @endlink For more
  1732. * detailed explanations of the Form API workflow, see the
  1733. * @link https://www.drupal.org/node/2117411 Form API documentation section. @endlink
  1734. * In addition, there is a set of Form API tutorials in the
  1735. * @link https://www.drupal.org/project/examples Examples for Developers project. @endlink
  1736. *
  1737. * In the form builder, validation, submission, and other form methods,
  1738. * $form_state is the primary influence on the processing of the form and is
  1739. * passed to most methods, so they can use it to communicate with the form
  1740. * system and each other. $form_state is an object that implements
  1741. * \Drupal\Core\Form\FormStateInterface.
  1742. * @}
  1743. */
  1744. /**
  1745. * @defgroup queue Queue operations
  1746. * @{
  1747. * Queue items to allow later processing.
  1748. *
  1749. * The queue system allows placing items in a queue and processing them later.
  1750. * The system tries to ensure that only one consumer can process an item.
  1751. *
  1752. * Before a queue can be used it needs to be created by
  1753. * Drupal\Core\Queue\QueueInterface::createQueue().
  1754. *
  1755. * Items can be added to the queue by passing an arbitrary data object to
  1756. * Drupal\Core\Queue\QueueInterface::createItem().
  1757. *
  1758. * To process an item, call Drupal\Core\Queue\QueueInterface::claimItem() and
  1759. * specify how long you want to have a lease for working on that item.
  1760. * When finished processing, the item needs to be deleted by calling
  1761. * Drupal\Core\Queue\QueueInterface::deleteItem(). If the consumer dies, the
  1762. * item will be made available again by the Drupal\Core\Queue\QueueInterface
  1763. * implementation once the lease expires. Another consumer will then be able to
  1764. * receive it when calling Drupal\Core\Queue\QueueInterface::claimItem().
  1765. * Due to this, the processing code should be aware that an item might be handed
  1766. * over for processing more than once.
  1767. *
  1768. * The $item object used by the Drupal\Core\Queue\QueueInterface can contain
  1769. * arbitrary metadata depending on the implementation. Systems using the
  1770. * interface should only rely on the data property which will contain the
  1771. * information passed to Drupal\Core\Queue\QueueInterface::createItem().
  1772. * The full queue item returned by Drupal\Core\Queue\QueueInterface::claimItem()
  1773. * needs to be passed to Drupal\Core\Queue\QueueInterface::deleteItem() once
  1774. * processing is completed.
  1775. *
  1776. * There are two kinds of queue backends available: reliable, which preserves
  1777. * the order of messages and guarantees that every item will be executed at
  1778. * least once. The non-reliable kind only does a best effort to preserve order
  1779. * in messages and to execute them at least once but there is a small chance
  1780. * that some items get lost. For example, some distributed back-ends like
  1781. * Amazon SQS will be managing jobs for a large set of producers and consumers
  1782. * where a strict FIFO ordering will likely not be preserved. Another example
  1783. * would be an in-memory queue backend which might lose items if it crashes.
  1784. * However, such a backend would be able to deal with significantly more writes
  1785. * than a reliable queue and for many tasks this is more important. See
  1786. * aggregator_cron() for an example of how to effectively use a non-reliable
  1787. * queue. Another example is doing Twitter statistics -- the small possibility
  1788. * of losing a few items is insignificant next to power of the queue being able
  1789. * to keep up with writes. As described in the processing section, regardless
  1790. * of the queue being reliable or not, the processing code should be aware that
  1791. * an item might be handed over for processing more than once (because the
  1792. * processing code might time out before it finishes).
  1793. * @}
  1794. */
  1795. /**
  1796. * @defgroup annotation Annotations
  1797. * @{
  1798. * Annotations for class discovery and metadata description.
  1799. *
  1800. * The Drupal plugin system has a set of reusable components that developers
  1801. * can use, override, and extend in their modules. Most of the plugins use
  1802. * annotations, which let classes register themselves as plugins and describe
  1803. * their metadata. (Annotations can also be used for other purposes, though
  1804. * at the moment, Drupal only uses them for the plugin system.)
  1805. *
  1806. * To annotate a class as a plugin, add code similar to the following to the
  1807. * end of the documentation block immediately preceding the class declaration:
  1808. * @code
  1809. * * @ContentEntityType(
  1810. * * id = "comment",
  1811. * * label = @Translation("Comment"),
  1812. * * ...
  1813. * * base_table = "comment"
  1814. * * )
  1815. * @endcode
  1816. *
  1817. * Note that you must use double quotes; single quotes will not work in
  1818. * annotations.
  1819. *
  1820. * Some annotation types, which extend the "@ PluginID" annotation class, have
  1821. * only a single 'id' key in their annotation. For these, it is possible to use
  1822. * a shorthand annotation. For example:
  1823. * @code
  1824. * * @ViewsArea("entity")
  1825. * @endcode
  1826. * in place of
  1827. * @code
  1828. * * @ViewsArea(
  1829. * * id = "entity"
  1830. * *)
  1831. * @endcode
  1832. *
  1833. * The available annotation classes are listed in this topic, and can be
  1834. * identified when you are looking at the Drupal source code by having
  1835. * "@ Annotation" in their documentation blocks (without the space after @). To
  1836. * find examples of annotation for a particular annotation class, such as
  1837. * EntityType, look for class files that have an @ annotation section using the
  1838. * annotation class.
  1839. *
  1840. * @see plugin_translatable
  1841. * @see plugin_context
  1842. *
  1843. * @}
  1844. */
  1845. /**
  1846. * @addtogroup hooks
  1847. * @{
  1848. */
  1849. /**
  1850. * Perform periodic actions.
  1851. *
  1852. * Modules that require some commands to be executed periodically can
  1853. * implement hook_cron(). The engine will then call the hook whenever a cron
  1854. * run happens, as defined by the administrator. Typical tasks managed by
  1855. * hook_cron() are database maintenance, backups, recalculation of settings
  1856. * or parameters, automated mailing, and retrieving remote data.
  1857. *
  1858. * Short-running or non-resource-intensive tasks can be executed directly in
  1859. * the hook_cron() implementation.
  1860. *
  1861. * Long-running tasks and tasks that could time out, such as retrieving remote
  1862. * data, sending email, and intensive file tasks, should use the queue API
  1863. * instead of executing the tasks directly. To do this, first define one or
  1864. * more queues via a \Drupal\Core\Annotation\QueueWorker plugin. Then, add items
  1865. * that need to be processed to the defined queues.
  1866. */
  1867. function hook_cron() {
  1868. // Short-running operation example, not using a queue:
  1869. // Delete all expired records since the last cron run.
  1870. $expires = \Drupal::state()->get('mymodule.cron_last_run', REQUEST_TIME);
  1871. db_delete('mymodule_table')
  1872. ->condition('expires', $expires, '>=')
  1873. ->execute();
  1874. \Drupal::state()->set('mymodule.cron_last_run', REQUEST_TIME);
  1875. // Long-running operation example, leveraging a queue:
  1876. // Fetch feeds from other sites.
  1877. $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh <> :never', array(
  1878. ':time' => REQUEST_TIME,
  1879. ':never' => AGGREGATOR_CLEAR_NEVER,
  1880. ));
  1881. $queue = \Drupal::queue('aggregator_feeds');
  1882. foreach ($result as $feed) {
  1883. $queue->createItem($feed);
  1884. }
  1885. }
  1886. /**
  1887. * Alter available data types for typed data wrappers.
  1888. *
  1889. * @param array $data_types
  1890. * An array of data type information.
  1891. *
  1892. * @see hook_data_type_info()
  1893. */
  1894. function hook_data_type_info_alter(&$data_types) {
  1895. $data_types['email']['class'] = '\Drupal\mymodule\Type\Email';
  1896. }
  1897. /**
  1898. * Alter cron queue information before cron runs.
  1899. *
  1900. * Called by \Drupal\Core\Cron to allow modules to alter cron queue settings
  1901. * before any jobs are processesed.
  1902. *
  1903. * @param array $queues
  1904. * An array of cron queue information.
  1905. *
  1906. * @see \Drupal\Core\QueueWorker\QueueWorkerInterface
  1907. * @see \Drupal\Core\Annotation\QueueWorker
  1908. * @see \Drupal\Core\Cron
  1909. */
  1910. function hook_queue_info_alter(&$queues) {
  1911. // This site has many feeds so let's spend 90 seconds on each cron run
  1912. // updating feeds instead of the default 60.
  1913. $queues['aggregator_feeds']['cron']['time'] = 90;
  1914. }
  1915. /**
  1916. * Alter an email message created with MailManagerInterface->mail().
  1917. *
  1918. * hook_mail_alter() allows modification of email messages created and sent
  1919. * with MailManagerInterface->mail(). Usage examples include adding and/or
  1920. * changing message text, message fields, and message headers.
  1921. *
  1922. * Email messages sent using functions other than MailManagerInterface->mail()
  1923. * will not invoke hook_mail_alter(). For example, a contributed module directly
  1924. * calling the MailInterface->mail() or PHP mail() function will not invoke
  1925. * this hook. All core modules use MailManagerInterface->mail() for messaging,
  1926. * it is best practice but not mandatory in contributed modules.
  1927. *
  1928. * @param $message
  1929. * An array containing the message data. Keys in this array include:
  1930. * - 'id':
  1931. * The MailManagerInterface->mail() id of the message. Look at module source
  1932. * code or MailManagerInterface->mail() for possible id values.
  1933. * - 'to':
  1934. * The address or addresses the message will be sent to. The
  1935. * formatting of this string must comply with RFC 2822.
  1936. * - 'from':
  1937. * The address the message will be marked as being from, which is
  1938. * either a custom address or the site-wide default email address.
  1939. * - 'subject':
  1940. * Subject of the email to be sent. This must not contain any newline
  1941. * characters, or the email may not be sent properly.
  1942. * - 'body':
  1943. * An array of strings or objects that implement
  1944. * \Drupal\Component\Render\MarkupInterface containing the message text. The
  1945. * message body is created by concatenating the individual array strings
  1946. * into a single text string using "\n\n" as a separator.
  1947. * - 'headers':
  1948. * Associative array containing mail headers, such as From, Sender,
  1949. * MIME-Version, Content-Type, etc.
  1950. * - 'params':
  1951. * An array of optional parameters supplied by the caller of
  1952. * MailManagerInterface->mail() that is used to build the message before
  1953. * hook_mail_alter() is invoked.
  1954. * - 'language':
  1955. * The language object used to build the message before hook_mail_alter()
  1956. * is invoked.
  1957. * - 'send':
  1958. * Set to FALSE to abort sending this email message.
  1959. *
  1960. * @see \Drupal\Core\Mail\MailManagerInterface::mail()
  1961. */
  1962. function hook_mail_alter(&$message) {
  1963. if ($message['id'] == 'modulename_messagekey') {
  1964. if (!example_notifications_optin($message['to'], $message['id'])) {
  1965. // If the recipient has opted to not receive such messages, cancel
  1966. // sending.
  1967. $message['send'] = FALSE;
  1968. return;
  1969. }
  1970. $message['body'][] = "--\nMail sent out from " . \Drupal::config('system.site')->get('name');
  1971. }
  1972. }
  1973. /**
  1974. * Prepares a message based on parameters;
  1975. *
  1976. * This hook is called from MailManagerInterface->mail(). Note that hook_mail(),
  1977. * unlike hook_mail_alter(), is only called on the $module argument to
  1978. * MailManagerInterface->mail(), not all modules.
  1979. *
  1980. * @param $key
  1981. * An identifier of the mail.
  1982. * @param $message
  1983. * An array to be filled in. Elements in this array include:
  1984. * - id: An ID to identify the mail sent. Look at module source code or
  1985. * MailManagerInterface->mail() for possible id values.
  1986. * - to: The address or addresses the message will be sent to. The
  1987. * formatting of this string must comply with RFC 2822.
  1988. * - subject: Subject of the email to be sent. This must not contain any
  1989. * newline characters, or the mail may not be sent properly.
  1990. * MailManagerInterface->mail() sets this to an empty
  1991. * string when the hook is invoked.
  1992. * - body: An array of lines containing the message to be sent. Drupal will
  1993. * format the correct line endings for you. MailManagerInterface->mail()
  1994. * sets this to an empty array when the hook is invoked. The array may
  1995. * contain either strings or objects implementing
  1996. * \Drupal\Component\Render\MarkupInterface.
  1997. * - from: The address the message will be marked as being from, which is
  1998. * set by MailManagerInterface->mail() to either a custom address or the
  1999. * site-wide default email address when the hook is invoked.
  2000. * - headers: Associative array containing mail headers, such as From,
  2001. * Sender, MIME-Version, Content-Type, etc.
  2002. * MailManagerInterface->mail() pre-fills several headers in this array.
  2003. * @param $params
  2004. * An array of parameters supplied by the caller of
  2005. * MailManagerInterface->mail().
  2006. *
  2007. * @see \Drupal\Core\Mail\MailManagerInterface->mail()
  2008. */
  2009. function hook_mail($key, &$message, $params) {
  2010. $account = $params['account'];
  2011. $context = $params['context'];
  2012. $variables = array(
  2013. '%site_name' => \Drupal::config('system.site')->get('name'),
  2014. '%username' => $account->getDisplayName(),
  2015. );
  2016. if ($context['hook'] == 'taxonomy') {
  2017. $entity = $params['entity'];
  2018. $vocabulary = Vocabulary::load($entity->id());
  2019. $variables += array(
  2020. '%term_name' => $entity->name,
  2021. '%term_description' => $entity->description,
  2022. '%term_id' => $entity->id(),
  2023. '%vocabulary_name' => $vocabulary->label(),
  2024. '%vocabulary_description' => $vocabulary->getDescription(),
  2025. '%vocabulary_id' => $vocabulary->id(),
  2026. );
  2027. }
  2028. // Node-based variable translation is only available if we have a node.
  2029. if (isset($params['node'])) {
  2030. /** @var \Drupal\node\NodeInterface $node */
  2031. $node = $params['node'];
  2032. $variables += array(
  2033. '%uid' => $node->getOwnerId(),
  2034. '%url' => $node->url('canonical', array('absolute' => TRUE)),
  2035. '%node_type' => node_get_type_label($node),
  2036. '%title' => $node->getTitle(),
  2037. '%teaser' => $node->teaser,
  2038. '%body' => $node->body,
  2039. );
  2040. }
  2041. $subject = strtr($context['subject'], $variables);
  2042. $body = strtr($context['message'], $variables);
  2043. $message['subject'] .= str_replace(array("\r", "\n"), '', $subject);
  2044. $message['body'][] = MailFormatHelper::htmlToText($body);
  2045. }
  2046. /**
  2047. * Alter the list of mail backend plugin definitions.
  2048. *
  2049. * @param array $info
  2050. * The mail backend plugin definitions to be altered.
  2051. *
  2052. * @see \Drupal\Core\Annotation\Mail
  2053. * @see \Drupal\Core\Mail\MailManager
  2054. */
  2055. function hook_mail_backend_info_alter(&$info) {
  2056. unset($info['test_mail_collector']);
  2057. }
  2058. /**
  2059. * Alter the default country list.
  2060. *
  2061. * @param $countries
  2062. * The associative array of countries keyed by two-letter country code.
  2063. *
  2064. * @see \Drupal\Core\Locale\CountryManager::getList()
  2065. */
  2066. function hook_countries_alter(&$countries) {
  2067. // Elbonia is now independent, so add it to the country list.
  2068. $countries['EB'] = 'Elbonia';
  2069. }
  2070. /**
  2071. * Alter display variant plugin definitions.
  2072. *
  2073. * @param array $definitions
  2074. * The array of display variant definitions, keyed by plugin ID.
  2075. *
  2076. * @see \Drupal\Core\Display\VariantManager
  2077. * @see \Drupal\Core\Display\Annotation\DisplayVariant
  2078. */
  2079. function hook_display_variant_plugin_alter(array &$definitions) {
  2080. $definitions['full_page']['admin_label'] = t('Block layout');
  2081. }
  2082. /**
  2083. * Flush all persistent and static caches.
  2084. *
  2085. * This hook asks your module to clear all of its static caches,
  2086. * in order to ensure a clean environment for subsequently
  2087. * invoked data rebuilds.
  2088. *
  2089. * Do NOT use this hook for rebuilding information. Only use it to flush custom
  2090. * caches.
  2091. *
  2092. * Static caches using drupal_static() do not need to be reset manually.
  2093. * However, all other static variables that do not use drupal_static() must be
  2094. * manually reset.
  2095. *
  2096. * This hook is invoked by drupal_flush_all_caches(). It runs before module data
  2097. * is updated and before hook_rebuild().
  2098. *
  2099. * @see drupal_flush_all_caches()
  2100. * @see hook_rebuild()
  2101. */
  2102. function hook_cache_flush() {
  2103. if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update') {
  2104. _update_cache_clear();
  2105. }
  2106. }
  2107. /**
  2108. * Rebuild data based upon refreshed caches.
  2109. *
  2110. * This hook allows your module to rebuild its data based on the latest/current
  2111. * module data. It runs after hook_cache_flush() and after all module data has
  2112. * been updated.
  2113. *
  2114. * This hook is only invoked after the system has been completely cleared;
  2115. * i.e., all previously cached data is known to be gone and every API in the
  2116. * system is known to return current information, so your module can safely rely
  2117. * on all available data to rebuild its own.
  2118. *
  2119. * @see hook_cache_flush()
  2120. * @see drupal_flush_all_caches()
  2121. */
  2122. function hook_rebuild() {
  2123. $themes = \Drupal::service('theme_handler')->listInfo();
  2124. foreach ($themes as $theme) {
  2125. _block_rehash($theme->getName());
  2126. }
  2127. }
  2128. /**
  2129. * Alter the configuration synchronization steps.
  2130. *
  2131. * @param array $sync_steps
  2132. * A one-dimensional array of \Drupal\Core\Config\ConfigImporter method names
  2133. * or callables that are invoked to complete the import, in the order that
  2134. * they will be processed. Each callable item defined in $sync_steps should
  2135. * either be a global function or a public static method. The callable should
  2136. * accept a $context array by reference. For example:
  2137. * <code>
  2138. * function _additional_configuration_step(&$context) {
  2139. * // Do stuff.
  2140. * // If finished set $context['finished'] = 1.
  2141. * }
  2142. * </code>
  2143. * For more information on creating batches, see the
  2144. * @link batch Batch operations @endlink documentation.
  2145. *
  2146. * @see callback_batch_operation()
  2147. * @see \Drupal\Core\Config\ConfigImporter::initialize()
  2148. */
  2149. function hook_config_import_steps_alter(&$sync_steps, \Drupal\Core\Config\ConfigImporter $config_importer) {
  2150. $deletes = $config_importer->getUnprocessedConfiguration('delete');
  2151. if (isset($deletes['field.storage.node.body'])) {
  2152. $sync_steps[] = '_additional_configuration_step';
  2153. }
  2154. }
  2155. /**
  2156. * Alter config typed data definitions.
  2157. *
  2158. * For example you can alter the typed data types representing each
  2159. * configuration schema type to change default labels or form element renderers
  2160. * used for configuration translation.
  2161. *
  2162. * If implementations of this hook add or remove configuration schema a
  2163. * ConfigSchemaAlterException will be thrown. Keep in mind that there are tools
  2164. * that may use the configuration schema for static analysis of configuration
  2165. * files, like the string extractor for the localization system. Such systems
  2166. * won't work with dynamically defined configuration schemas.
  2167. *
  2168. * For adding new data types use configuration schema YAML files instead.
  2169. *
  2170. * @param $definitions
  2171. * Associative array of configuration type definitions keyed by schema type
  2172. * names. The elements are themselves array with information about the type.
  2173. *
  2174. * @see \Drupal\Core\Config\TypedConfigManager
  2175. * @see \Drupal\Core\Config\Schema\ConfigSchemaAlterException
  2176. */
  2177. function hook_config_schema_info_alter(&$definitions) {
  2178. // Enhance the text and date type definitions with classes to generate proper
  2179. // form elements in ConfigTranslationFormBase. Other translatable types will
  2180. // appear as a one line textfield.
  2181. $definitions['text']['form_element_class'] = '\Drupal\config_translation\FormElement\Textarea';
  2182. $definitions['date_format']['form_element_class'] = '\Drupal\config_translation\FormElement\DateFormat';
  2183. }
  2184. /**
  2185. * Alter validation constraint plugin definitions.
  2186. *
  2187. * @param array[] $definitions
  2188. * The array of validation constraint definitions, keyed by plugin ID.
  2189. *
  2190. * @see \Drupal\Core\Validation\ConstraintManager
  2191. * @see \Drupal\Core\Validation\Annotation\Constraint
  2192. */
  2193. function hook_validation_constraint_alter(array &$definitions) {
  2194. $definitions['Null']['class'] = '\Drupal\mymodule\Validator\Constraints\MyClass';
  2195. }
  2196. /**
  2197. * @} End of "addtogroup hooks".
  2198. */
  2199. /**
  2200. * @defgroup ajax Ajax API
  2201. * @{
  2202. * Overview for Drupal's Ajax API.
  2203. *
  2204. * @section sec_overview Overview of Ajax
  2205. * Ajax is the process of dynamically updating parts of a page's HTML based on
  2206. * data from the server. When a specified event takes place, a PHP callback is
  2207. * triggered, which performs server-side logic and may return updated markup or
  2208. * JavaScript commands to run. After the return, the browser runs the JavaScript
  2209. * or updates the markup on the fly, with no full page refresh necessary.
  2210. *
  2211. * Many different events can trigger Ajax responses, including:
  2212. * - Clicking a button
  2213. * - Pressing a key
  2214. * - Moving the mouse
  2215. *
  2216. * @section sec_framework Ajax responses in forms
  2217. * Forms that use the Drupal Form API (see the
  2218. * @link form_api Form API topic @endlink for more information about forms) can
  2219. * trigger AJAX responses. Here is an outline of the steps:
  2220. * - Add property '#ajax' to a form element in your form array, to trigger an
  2221. * Ajax response.
  2222. * - Write an Ajax callback to process the input and respond.
  2223. * See sections below for details on these two steps.
  2224. *
  2225. * @subsection sub_form Adding Ajax triggers to a form
  2226. * As an example of adding Ajax triggers to a form, consider editing a date
  2227. * format, where the user is provided with a sample of the generated date output
  2228. * as they type. To accomplish this, typing in the text field should trigger an
  2229. * Ajax response. This is done in the text field form array element
  2230. * in \Drupal\config_translation\FormElement\DateFormat::getFormElement():
  2231. * @code
  2232. * '#ajax' => array(
  2233. * 'callback' => 'Drupal\config_translation\FormElement\DateFormat::ajaxSample',
  2234. * 'event' => 'keyup',
  2235. * 'progress' => array(
  2236. * 'type' => 'throbber',
  2237. * 'message' => NULL,
  2238. * ),
  2239. * ),
  2240. * @endcode
  2241. *
  2242. * As you can see from this example, the #ajax property for a form element is
  2243. * an array. Here are the details of its elements, all of which are optional:
  2244. * - callback: The callback to invoke to handle the server side of the
  2245. * Ajax event. More information on callbacks is below in @ref sub_callback.
  2246. * - wrapper: The HTML 'id' attribute of the area where the content returned by
  2247. * the callback should be placed. Note that callbacks have a choice of
  2248. * returning content or JavaScript commands; 'wrapper' is used for content
  2249. * returns.
  2250. * - method: The jQuery method for placing the new content (used with
  2251. * 'wrapper'). Valid options are 'replaceWith' (default), 'append', 'prepend',
  2252. * 'before', 'after', or 'html'. See
  2253. * http://api.jquery.com/category/manipulation/ for more information on these
  2254. * methods.
  2255. * - effect: The jQuery effect to use when placing the new HTML (used with
  2256. * 'wrapper'). Valid options are 'none' (default), 'slide', or 'fade'.
  2257. * - speed: The effect speed to use (used with 'effect' and 'wrapper'). Valid
  2258. * options are 'slow' (default), 'fast', or the number of milliseconds the
  2259. * effect should run.
  2260. * - event: The JavaScript event to respond to. This is selected automatically
  2261. * for the type of form element; provide a value to override the default.
  2262. * - prevent: A JavaScript event to prevent when the event is triggered. For
  2263. * example, if you use event 'mousedown' on a button, you might want to
  2264. * prevent 'click' events from also being triggered.
  2265. * - progress: An array indicating how to show Ajax processing progress. Can
  2266. * contain one or more of these elements:
  2267. * - type: Type of indicator: 'throbber' (default) or 'bar'.
  2268. * - message: Translated message to display.
  2269. * - url: For a bar progress indicator, URL path for determining progress.
  2270. * - interval: For a bar progress indicator, how often to update it.
  2271. * - url: A \Drupal\Core\Url to which to submit the Ajax request. If omitted,
  2272. * defaults to either the same URL as the form or link destination is for
  2273. * someone with JavaScript disabled, or a slightly modified version (e.g.,
  2274. * with a query parameter added, removed, or changed) of that URL if
  2275. * necessary to support Drupal's content negotiation. It is recommended to
  2276. * omit this key and use Drupal's content negotiation rather than using
  2277. * substantially different URLs between Ajax and non-Ajax.
  2278. *
  2279. * @subsection sub_callback Setting up a callback to process Ajax
  2280. * Once you have set up your form to trigger an Ajax response (see @ref sub_form
  2281. * above), you need to write some PHP code to process the response. If you use
  2282. * 'path' in your Ajax set-up, your route controller will be triggered with only
  2283. * the information you provide in the URL. If you use 'callback', your callback
  2284. * method is a function, which will receive the $form and $form_state from the
  2285. * triggering form. You can use $form_state to get information about the
  2286. * data the user has entered into the form. For instance, in the above example
  2287. * for the date format preview,
  2288. * \Drupal\config_translation\FormElement\DateFormat\ajaxSample() does this to
  2289. * get the format string entered by the user:
  2290. * @code
  2291. * $format_value = \Drupal\Component\Utility\NestedArray::getValue(
  2292. * $form_state->getValues(),
  2293. * $form_state->getTriggeringElement()['#array_parents']);
  2294. * @endcode
  2295. *
  2296. * Once you have processed the input, you have your choice of returning HTML
  2297. * markup or a set of Ajax commands. If you choose to return HTML markup, you
  2298. * can return it as a string or a renderable array, and it will be placed in
  2299. * the defined 'wrapper' element (see documentation above in @ref sub_form).
  2300. * In addition, any messages returned by drupal_get_messages(), themed as in
  2301. * status-messages.html.twig, will be prepended.
  2302. *
  2303. * To return commands, you need to set up an object of class
  2304. * \Drupal\Core\Ajax\AjaxResponse, and then use its addCommand() method to add
  2305. * individual commands to it. In the date format preview example, the format
  2306. * output is calculated, and then it is returned as replacement markup for a div
  2307. * like this:
  2308. * @code
  2309. * $response = new AjaxResponse();
  2310. * $response->addCommand(new ReplaceCommand(
  2311. * '#edit-date-format-suffix',
  2312. * '<small id="edit-date-format-suffix">' . $format . '</small>'));
  2313. * return $response;
  2314. * @endcode
  2315. *
  2316. * The individual commands that you can return implement interface
  2317. * \Drupal\Core\Ajax\CommandInterface. Available commands provide the ability
  2318. * to pop up alerts, manipulate text and markup in various ways, redirect
  2319. * to a new URL, and the generic \Drupal\Core\Ajax\InvokeCommand, which
  2320. * invokes an arbitrary jQuery command.
  2321. *
  2322. * As noted above, status messages are prepended automatically if you use the
  2323. * 'wrapper' method and return HTML markup. This is not the case if you return
  2324. * commands, but if you would like to show status messages, you can add
  2325. * @code
  2326. * array('#type' => 'status_messages')
  2327. * @endcode
  2328. * to a render array, use drupal_render() to render it, and add a command to
  2329. * place the messages in an appropriate location.
  2330. *
  2331. * @section sec_other Other methods for triggering Ajax
  2332. * Here are some additional methods you can use to trigger Ajax responses in
  2333. * Drupal:
  2334. * - Add class 'use-ajax' to a link. The link will be loaded using an Ajax
  2335. * call. When using this method, the href of the link can contain '/nojs/' as
  2336. * part of the path. When the Ajax JavaScript processes the page, it will
  2337. * convert this to '/ajax/'. The server is then able to easily tell if this
  2338. * request was made through an actual Ajax request or in a degraded state, and
  2339. * respond appropriately.
  2340. * - Add class 'use-ajax-submit' to a submit button in a form. The form will
  2341. * then be submitted via Ajax to the path specified in the #action. Like the
  2342. * ajax-submit class on links, this path will have '/nojs/' replaced with
  2343. * '/ajax/' so that the submit handler can tell if the form was submitted in a
  2344. * degraded state or not.
  2345. * - Add property '#autocomplete_route_name' to a text field in a form. The
  2346. * route controller for this route must return an array of options for
  2347. * autocomplete, as a \Symfony\Component\HttpFoundation\JsonResponse object.
  2348. * See the @link menu Routing topic @endlink for more information about
  2349. * routing.
  2350. */
  2351. /**
  2352. * @} End of "defgroup ajax".
  2353. */
  2354. /**
  2355. * @defgroup service_tag Service Tags
  2356. * @{
  2357. * Service tags overview
  2358. *
  2359. * Some services have tags, which are defined in the service definition. Tags
  2360. * are used to define a group of related services, or to specify some aspect of
  2361. * how the service behaves. Typically, if you tag a service, your service class
  2362. * must also implement a corresponding interface. Some common examples:
  2363. * - access_check: Indicates a route access checking service; see the
  2364. * @link menu Menu and routing system topic @endlink for more information.
  2365. * - cache.bin: Indicates a cache bin service; see the
  2366. * @link cache Cache topic @endlink for more information.
  2367. * - event_subscriber: Indicates an event subscriber service. Event subscribers
  2368. * can be used for dynamic routing and route altering; see the
  2369. * @link menu Menu and routing system topic @endlink for more information.
  2370. * They can also be used for other purposes; see
  2371. * http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html
  2372. * for more information.
  2373. * - needs_destruction: Indicates that a destruct() method needs to be called
  2374. * at the end of a request to finalize operations, if this service was
  2375. * instantiated. Services should implement \Drupal\Core\DestructableInterface
  2376. * in this case.
  2377. * - context_provider: Indicates a block context provider, used for example
  2378. * by block conditions. It has to implement
  2379. * \Drupal\Core\Plugin\Context\ContextProviderInterface.
  2380. * - http_client_middleware: Indicates that the service provides a guzzle
  2381. * middleware, see
  2382. * https://guzzle.readthedocs.org/en/latest/handlers-and-middleware.html for
  2383. * more information.
  2384. *
  2385. * Creating a tag for a service does not do anything on its own, but tags
  2386. * can be discovered or queried in a compiler pass when the container is built,
  2387. * and a corresponding action can be taken. See
  2388. * \Drupal\Core\Render\MainContent\MainContentRenderersPass for an example of
  2389. * finding tagged services.
  2390. *
  2391. * See @link container Services and Dependency Injection Container @endlink for
  2392. * information on services and the dependency injection container.
  2393. *
  2394. * @}
  2395. */
  2396. /**
  2397. * @defgroup events Events
  2398. * @{
  2399. * Overview of event dispatch and subscribing
  2400. *
  2401. * @section sec_intro Introduction and terminology
  2402. * Events are part of the Symfony framework: they allow for different components
  2403. * of the system to interact and communicate with each other. Each event has a
  2404. * unique string name. One system component dispatches the event at an
  2405. * appropriate time; many events are dispatched by Drupal core and the Symfony
  2406. * framework in every request. Other system components can register as event
  2407. * subscribers; when an event is dispatched, a method is called on each
  2408. * registered subscriber, allowing each one to react. For more on the general
  2409. * concept of events, see
  2410. * http://symfony.com/doc/current/components/event_dispatcher/introduction.html
  2411. *
  2412. * @section sec_dispatch Dispatching events
  2413. * To dispatch an event, call the
  2414. * \Symfony\Component\EventDispatcher\EventDispatchInterface::dispatch() method
  2415. * on the 'event_dispatcher' service (see the
  2416. * @link container Services topic @endlink for more information about how to
  2417. * interact with services). The first argument is the unique event name, which
  2418. * you should normally define as a constant in a separate static class (see
  2419. * \Symfony\Component\HttpKernel\KernelEvents and
  2420. * \Drupal\Core\Config\ConfigEvents for examples). The second argument is a
  2421. * \Symfony\Component\EventDispatcher\Event object; normally you will need to
  2422. * extend this class, so that your event class can provide data to the event
  2423. * subscribers.
  2424. *
  2425. * @section sec_subscribe Registering event subscribers
  2426. * Here are the steps to register an event subscriber:
  2427. * - Define a service in your module, tagged with 'event_subscriber' (see the
  2428. * @link container Services topic @endlink for instructions).
  2429. * - Define a class for your subscriber service that implements
  2430. * \Symfony\Component\EventDispatcher\EventSubscriberInterface
  2431. * - In your class, the getSubscribedEvents method returns a list of the events
  2432. * this class is subscribed to, and which methods on the class should be
  2433. * called for each one. Example:
  2434. * @code
  2435. * public function getSubscribedEvents() {
  2436. * // Subscribe to kernel terminate with priority 100.
  2437. * $events[KernelEvents::TERMINATE][] = array('onTerminate', 100);
  2438. * // Subscribe to kernel request with default priority of 0.
  2439. * $events[KernelEvents::REQUEST][] = array('onRequest');
  2440. * return $events;
  2441. * }
  2442. * @endcode
  2443. * - Write the methods that respond to the events; each one receives the
  2444. * event object provided in the dispatch as its one argument. In the above
  2445. * example, you would need to write onTerminate() and onRequest() methods.
  2446. *
  2447. * Note that in your getSubscribedEvents() method, you can optionally set the
  2448. * priority of your event subscriber (see terminate example above). Event
  2449. * subscribers with higher priority numbers get executed first; the default
  2450. * priority is zero. If two event subscribers for the same event have the same
  2451. * priority, the one defined in a module with a lower module weight will fire
  2452. * first. Subscribers defined in the same services file are fired in
  2453. * definition order. If order matters defining a priority is strongly advised
  2454. * instead of relying on these two tie breaker rules as they might change in a
  2455. * minor release.
  2456. * @}
  2457. */