PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/core.api.php

http://github.com/drupal/drupal
PHP | 2598 lines | 112 code | 48 blank | 2438 comment | 12 complexity | 88133ae60fabbe9b0ec54849ee5e40f2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

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

  1. <?php
  2. /**
  3. * @file
  4. * 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 time span, 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\CacheableDependencyInterface::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 calculation. 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. * For cache bins that are stored in the database, the number of rows is limited
  604. * to 5000 by default. This can be changed for all database cache bins. For
  605. * example, to instead limit the number of rows to 50000:
  606. * @code
  607. * $settings['database_cache_max_rows']['default'] = 50000;
  608. * @endcode
  609. *
  610. * Or per bin (in this example we allow infinite entries):
  611. * @code
  612. * $settings['database_cache_max_rows']['bins']['dynamic_page_cache'] = -1;
  613. * @endcode
  614. *
  615. * For monitoring reasons it might be useful to figure out the amount of data
  616. * stored in tables. The following SQL snippet can be used for that:
  617. * @code
  618. * SELECT table_name AS `Table`, table_rows AS 'Num. of Rows',
  619. * ROUND(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` FROM
  620. * information_schema.TABLES WHERE table_schema = '***DATABASE_NAME***' AND
  621. * table_name LIKE 'cache_%' ORDER BY (data_length + index_length) DESC
  622. * LIMIT 10;
  623. * @endcode
  624. *
  625. * @see \Drupal\Core\Cache\DatabaseBackend
  626. *
  627. * Finally, you can chain multiple cache backends together, see
  628. * \Drupal\Core\Cache\ChainedFastBackend and \Drupal\Core\Cache\BackendChain.
  629. *
  630. * @see https://www.drupal.org/node/1884796
  631. * @}
  632. */
  633. /**
  634. * @defgroup user_api User accounts, permissions, and roles
  635. * @{
  636. * API for user accounts, access checking, roles, and permissions.
  637. *
  638. * @section sec_overview Overview and terminology
  639. * Drupal's permission system is based on the concepts of accounts, roles,
  640. * and permissions.
  641. *
  642. * Users (site visitors) have accounts, which include a user name, an email
  643. * address, a password (or some other means of authentication), and possibly
  644. * other fields (if defined on the site). Anonymous users have an implicit
  645. * account that does not have a real user name or any account information.
  646. *
  647. * Each user account is assigned one or more roles. The anonymous user account
  648. * automatically has the anonymous user role; real user accounts
  649. * automatically have the authenticated user role, plus any roles defined on
  650. * the site that they have been assigned.
  651. *
  652. * Each role, including the special anonymous and authenticated user roles, is
  653. * granted one or more named permissions, which allow them to perform certain
  654. * tasks or view certain content on the site. It is possible to designate a
  655. * role to be the "administrator" role; if this is set up, this role is
  656. * automatically granted all available permissions whenever a module is
  657. * enabled that defines permissions.
  658. *
  659. * All code in Drupal that allows users to perform tasks or view content must
  660. * check that the current user has the correct permission before allowing the
  661. * action. In the standard case, access checking consists of answering the
  662. * question "Does the current user have permission 'foo'?", and allowing or
  663. * denying access based on the answer. Note that access checking should nearly
  664. * always be done at the permission level, not by checking for a particular role
  665. * or user ID, so that site administrators can set up user accounts and roles
  666. * appropriately for their particular sites.
  667. *
  668. * @section sec_define Defining permissions
  669. * Modules define permissions via a $module.permissions.yml file. See
  670. * \Drupal\user\PermissionHandler for documentation of permissions.yml files.
  671. *
  672. * @section sec_access Access permission checking
  673. * Depending on the situation, there are several methods for ensuring that
  674. * access checks are done properly in Drupal:
  675. * - Routes: When you register a route, include a 'requirements' section that
  676. * either gives the machine name of the permission that is needed to visit the
  677. * URL of the route, or tells Drupal to use an access check method or service
  678. * to check access. See the @link menu Routing topic @endlink for more
  679. * information.
  680. * - Entities: Access for various entity operations is designated either with
  681. * simple permissions or access control handler classes in the entity
  682. * annotation. See the @link entity_api Entity API topic @endlink for more
  683. * information.
  684. * - Other code: There is a 'current_user' service, which can be injected into
  685. * classes to provide access to the current user account (see the
  686. * @link container Services and Dependency Injection topic @endlink for more
  687. * information on dependency injection). In code that cannot use dependency
  688. * injection, you can access this service and retrieve the current user
  689. * account object by calling \Drupal::currentUser(). Once you have a user
  690. * object for the current user (implementing \Drupal\user\UserInterface), you
  691. * can call inherited method
  692. * \Drupal\Core\Session\AccountInterface::hasPermission() to check
  693. * permissions, or pass this object into other functions/methods.
  694. * - Forms: Each element of a form array can have a Boolean '#access' property,
  695. * which determines whether that element is visible and/or usable. This is a
  696. * common need in forms, so the current user service (described above) is
  697. * injected into the form base class as method
  698. * \Drupal\Core\Form\FormBase::currentUser().
  699. *
  700. * @section sec_entities User and role objects
  701. * User objects in Drupal are entity items, implementing
  702. * \Drupal\user\UserInterface. Role objects in Drupal are also entity items,
  703. * implementing \Drupal\user\RoleInterface. See the
  704. * @link entity_api Entity API topic @endlink for more information about
  705. * entities in general (including how to load, create, modify, and query them).
  706. *
  707. * Roles often need to be manipulated in automated test code, such as to add
  708. * permissions to them. Here's an example:
  709. * @code
  710. * $role = \Drupal\user\Entity\Role::load('authenticated');
  711. * $role->grantPermission('access comments');
  712. * $role->save();
  713. * @endcode
  714. *
  715. * Other important interfaces:
  716. * - \Drupal\Core\Session\AccountInterface: The part of UserInterface that
  717. * deals with access checking. In writing code that checks access, your
  718. * method parameters should use this interface, not UserInterface.
  719. * - \Drupal\Core\Session\AccountProxyInterface: The interface for the
  720. * current_user service (described above).
  721. * @}
  722. */
  723. /**
  724. * @defgroup container Services and Dependency Injection Container
  725. * @{
  726. * Overview of the Dependency Injection Container and Services.
  727. *
  728. * @section sec_overview Overview of container, injection, and services
  729. * The Services and Dependency Injection Container concepts have been adopted by
  730. * Drupal from the @link http://symfony.com/ Symfony framework. @endlink A
  731. * "service" (such as accessing the database, sending email, or translating user
  732. * interface text) is defined (given a name and an interface or at least a
  733. * class that defines the methods that may be called), and a default class is
  734. * designated to provide the service. These two steps must be done together, and
  735. * can be done by Drupal Core or a module. Other modules can then define
  736. * alternative classes to provide the same services, overriding the default
  737. * classes. Classes and functions that need to use the service should always
  738. * instantiate the class via the dependency injection container (also known
  739. * simply as the "container"), rather than instantiating a particular service
  740. * provider class directly, so that they get the correct class (default or
  741. * overridden).
  742. *
  743. * See https://www.drupal.org/node/2133171 for more detailed information on
  744. * services and the dependency injection container.
  745. *
  746. * @section sec_discover Discovering existing services
  747. * Drupal core defines many core services in the core.services.yml file (in the
  748. * top-level core directory). Some Drupal Core modules and contributed modules
  749. * also define services in modulename.services.yml files. API reference sites
  750. * (such as https://api.drupal.org) generate lists of all existing services from
  751. * these files. Look for the Services link in the API Navigation block.
  752. * Alternatively you can look through the individual files manually.
  753. *
  754. * A typical service definition in a *.services.yml file looks like this:
  755. * @code
  756. * path_alias.manager:
  757. * class: Drupal\path_alias\AliasManager
  758. * arguments: ['@path_alias.repository', '@path_alias.whitelist', '@language_manager']
  759. * @endcode
  760. * Some services use other services as factories; a typical service definition
  761. * is:
  762. * @code
  763. * cache.entity:
  764. * class: Drupal\Core\Cache\CacheBackendInterface
  765. * tags:
  766. * - { name: cache.bin }
  767. * factory: cache_factory:get
  768. * arguments: [entity]
  769. * @endcode
  770. *
  771. * The first line of a service definition gives the unique machine name of the
  772. * service. This is often prefixed by the module name if provided by a module;
  773. * however, by convention some service names are prefixed by a group name
  774. * instead, such as cache.* for cache bins and plugin.manager.* for plugin
  775. * managers.
  776. *
  777. * The class line either gives the default class that provides the service, or
  778. * if the service uses a factory class, the interface for the service. If the
  779. * class depends on other services, the arguments line lists the machine
  780. * names of the dependencies (preceded by '@'); objects for each of these
  781. * services are instantiated from the container and passed to the class
  782. * constructor when the service class is instantiated. Other arguments can also
  783. * be passed in; see the section at https://www.drupal.org/node/2133171 for more
  784. * detailed information.
  785. *
  786. * Services using factories can be defined as shown in the above example, if the
  787. * factory is itself a service. The factory can also be a class; details of how
  788. * to use service factories can be found in the section at
  789. * https://www.drupal.org/node/2133171.
  790. *
  791. * @section sec_container Accessing a service through the container
  792. * As noted above, if you need to use a service in your code, you should always
  793. * instantiate the service class via a call to the container, using the machine
  794. * name of the service, so that the default class can be overridden. There are
  795. * several ways to make sure this happens:
  796. * - For service-providing classes, see other sections of this documentation
  797. * describing how to pass services as arguments to the constructor.
  798. * - Plugin classes, controllers, and similar classes have create() or
  799. * createInstance() methods that are used to create an instance of the class.
  800. * These methods come from different interfaces, and have different
  801. * arguments, but they all include an argument $container of type
  802. * \Symfony\Component\DependencyInjection\ContainerInterface.
  803. * If you are defining one of these classes, in the create() or
  804. * createInstance() method, call $container->get('myservice.name') to
  805. * instantiate a service. The results of these calls are generally passed to
  806. * the class constructor and saved as member variables in the class.
  807. * - For functions and class methods that do not have access to either of
  808. * the above methods of dependency injection, you can use service location to
  809. * access services, via a call to the global \Drupal class. This class has
  810. * special methods for accessing commonly-used services, or you can call a
  811. * generic method to access any service. Examples:
  812. * @code
  813. * // Retrieve the entity.manager service object (special method exists).
  814. * $manager = \Drupal::entityManager();
  815. * // Retrieve the service object for machine name 'foo.bar'.
  816. * $foobar = \Drupal::service('foo.bar');
  817. * @endcode
  818. *
  819. * As a note, you should always use dependency injection (via service arguments
  820. * or create()/createInstance() methods) if possible to instantiate services,
  821. * rather than service location (via the \Drupal class), because:
  822. * - Dependency injection facilitates writing unit tests, since the container
  823. * argument can be mocked and the create() method can be bypassed by using
  824. * the class constructor. If you use the \Drupal class, unit tests are much
  825. * harder to write and your code has more dependencies.
  826. * - Having the service interfaces on the class constructor and member variables
  827. * is useful for IDE auto-complete and self-documentation.
  828. *
  829. * @section sec_define Defining a service
  830. * If your module needs to define a new service, here are the steps:
  831. * - Choose a unique machine name for your service. Typically, this should
  832. * start with your module name. Example: mymodule.myservice.
  833. * - Create a PHP interface to define what your service does.
  834. * - Create a default class implementing your interface that provides your
  835. * service. If your class needs to use existing services (such as database
  836. * access), be sure to make these services arguments to your class
  837. * constructor, and save them in member variables. Also, if the needed
  838. * services are provided by other modules and not Drupal Core, you'll want
  839. * these modules to be dependencies of your module.
  840. * - Add an entry to a modulename.services.yml file for the service. See
  841. * @ref sec_discover above, or existing *.services.yml files in Core, for the
  842. * syntax; it will start with your machine name, refer to your default class,
  843. * and list the services that need to be passed into your constructor.
  844. *
  845. * Services can also be defined dynamically, as in the
  846. * \Drupal\Core\CoreServiceProvider class, but this is less common for modules.
  847. *
  848. * @section sec_tags Service tags
  849. * Some services have tags, which are defined in the service definition. See
  850. * @link service_tag Service Tags @endlink for usage.
  851. *
  852. * @section sec_injection Overriding the default service class
  853. * Modules can override the default classes used for services. Here are the
  854. * steps:
  855. * - Define a class in the top-level namespace for your module
  856. * (Drupal\my_module), whose name is the camel-case version of your module's
  857. * machine name followed by "ServiceProvider" (for example, if your module
  858. * machine name is my_module, the class must be named
  859. * MyModuleServiceProvider).
  860. * - The class needs to implement
  861. * \Drupal\Core\DependencyInjection\ServiceModifierInterface, which is
  862. * typically done by extending
  863. * \Drupal\Core\DependencyInjection\ServiceProviderBase.
  864. * - The class needs to contain one method: alter(). This method does the
  865. * actual work of telling Drupal to use your class instead of the default.
  866. * Here's an example:
  867. * @code
  868. * public function alter(ContainerBuilder $container) {
  869. * // Override the language_manager class with a new class.
  870. * $definition = $container->getDefinition('language_manager');
  871. * $definition->setClass('Drupal\my_module\MyLanguageManager');
  872. * }
  873. * @endcode
  874. * Note that $container here is an instance of
  875. * \Drupal\Core\DependencyInjection\ContainerBuilder.
  876. *
  877. * @see https://www.drupal.org/node/2133171
  878. * @see core.services.yml
  879. * @see \Drupal
  880. * @see \Symfony\Component\DependencyInjection\ContainerInterface
  881. * @see plugin_api
  882. * @see menu
  883. * @}
  884. */
  885. /**
  886. * @defgroup listing_page_service Page header for Services page
  887. * @{
  888. * Introduction to services
  889. *
  890. * A "service" (such as accessing the database, sending email, or translating
  891. * user interface text) can be defined by a module or Drupal core. Defining a
  892. * service means giving it a name and designating a default class to provide the
  893. * service; ideally, there should also be an interface that defines the methods
  894. * that may be called. Services are collected into the Dependency Injection
  895. * Container, and can be overridden to use different classes or different
  896. * instantiation by modules. See the
  897. * @link container Services and Dependency Injection Container topic @endlink
  898. * for details.
  899. *
  900. * Some services have tags, which are defined in the service definition. Tags
  901. * are used to define a group of related services, or to specify some aspect of
  902. * how the service behaves. See the
  903. * @link service_tag Service Tags topic @endlink for more information.
  904. *
  905. * @see container
  906. * @see service_tag
  907. *
  908. * @}
  909. */
  910. /**
  911. * @defgroup typed_data Typed Data API
  912. * @{
  913. * API for describing data based on a set of available data types.
  914. *
  915. * PHP has data types, such as int, string, float, array, etc., and it is an
  916. * object-oriented language that lets you define classes and interfaces.
  917. * However, in some cases, it is useful to be able to define an abstract
  918. * type (as in an interface, free of implementation details), that still has
  919. * properties (which an interface cannot) as well as meta-data. The Typed Data
  920. * API provides this abstraction.
  921. *
  922. * @section sec_overview Overview
  923. * Each data type in the Typed Data API is a plugin class (annotation class
  924. * example: \Drupal\Core\TypedData\Annotation\DataType); these plugins are
  925. * managed by the typed_data_manager service (by default
  926. * \Drupal\Core\TypedData\TypedDataManager). Each data object encapsulates a
  927. * single piece of data, provides access to the metadata, and provides
  928. * validation capability. Also, the typed data plugins have a shorthand
  929. * for easily accessing data values, described in @ref sec_tree.
  930. *
  931. * The metadata of a data object is defined by an object based on a class called
  932. * the definition class (see \Drupal\Core\TypedData\DataDefinitionInterface).
  933. * The class used can vary by data type and can be specified in the data type's
  934. * plugin definition, while the default is set in the $definition_class property
  935. * of the annotation class. The default class is
  936. * \Drupal\Core\TypedData\DataDefinition. For data types provided by a plugin
  937. * deriver, the plugin deriver can set the definition_class property too.
  938. * The metadata object provides information about the data, such as the data
  939. * type, whether it is translatable, the names of its properties (for complex
  940. * types), and who can access it.
  941. *
  942. * See https://www.drupal.org/node/1794140 for more information about the Typed
  943. * Data API.
  944. *
  945. * @section sec_varieties Varieties of typed data
  946. * There are three kinds of typed data: primitive, complex, and list.
  947. *
  948. * @subsection sub_primitive Primitive data types
  949. * Primitive data types wrap PHP data types and also serve as building blocks
  950. * for complex and list typed data. Each primitive data type has an interface
  951. * that extends \Drupal\Core\TypedData\PrimitiveInterface, with getValue()
  952. * and setValue() methods for accessing the data value, and a default plugin
  953. * implementation. Here's a list:
  954. * - \Drupal\Core\TypedData\Type\IntegerInterface: Plugin ID integer,
  955. * corresponds to PHP type int.
  956. * - \Drupal\Core\TypedData\Type\StringInterface: Plugin ID string,
  957. * corresponds to PHP type string.
  958. * - \Drupal\Core\TypedData\Type\FloatInterface: Plugin ID float,
  959. * corresponds to PHP type float.
  960. * - \Drupal\Core\TypedData\Type\BooleanInterface: Plugin ID bool,
  961. * corresponds to PHP type bool.
  962. * - \Drupal\Core\TypedData\Type\BinaryInterface: Plugin ID binary,
  963. * corresponds to a PHP file resource.
  964. * - \Drupal\Core\TypedData\Type\UriInterface: Plugin ID uri.
  965. *
  966. * @subsection sec_complex Complex data
  967. * Complex data types, with interface
  968. * \Drupal\Core\TypedData\ComplexDataInterface, represent data with named
  969. * properties; the properties can be accessed with get() and set() methods.
  970. * The value of each property is itself a typed data object, which can be
  971. * primitive, complex, or list data.
  972. *
  973. * The base type for most complex data is the
  974. * \Drupal\Core\TypedData\Plugin\DataType\Map class, which represents an
  975. * associative array. Map provides its own definition class in the annotation,
  976. * \Drupal\Core\TypedData\MapDataDefinition, and most complex data classes
  977. * extend this class. The getValue() and setValue() methods on the Map class
  978. * enforce the data definition and its property structure.
  979. *
  980. * The Drupal Field API uses complex typed data for its field items, with
  981. * definition class \Drupal\Core\Field\TypedData\FieldItemDataDefinition.
  982. *
  983. * @section sec_list Lists
  984. * List data types, with interface \Drupal\Core\TypedData\ListInterface,
  985. * represent data that is an ordered list of typed data, all of the same type.
  986. * More precisely, the plugins in the list must have the same base plugin ID;
  987. * however, some types (for example field items and entities) are provided by
  988. * plugin derivatives and the sub IDs can be different.
  989. *
  990. * @section sec_tree Tree handling
  991. * Typed data allows you to use shorthand to get data values nested in the
  992. * implicit tree structure of the data. For example, to get the value from
  993. * an entity field item, the Entity Field API allows you to call:
  994. * @code
  995. * $value = $entity->fieldName->propertyName;
  996. * @endcode
  997. * T…

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