Drupal.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal.
  5. */
  6. use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
  7. use Drupal\Core\Messenger\LegacyMessenger;
  8. use Drupal\Core\Url;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. /**
  11. * Static Service Container wrapper.
  12. *
  13. * Generally, code in Drupal should accept its dependencies via either
  14. * constructor injection or setter method injection. However, there are cases,
  15. * particularly in legacy procedural code, where that is infeasible. This
  16. * class acts as a unified global accessor to arbitrary services within the
  17. * system in order to ease the transition from procedural code to injected OO
  18. * code.
  19. *
  20. * The container is built by the kernel and passed in to this class which stores
  21. * it statically. The container always contains the services from
  22. * \Drupal\Core\CoreServiceProvider, the service providers of enabled modules and any other
  23. * service providers defined in $GLOBALS['conf']['container_service_providers'].
  24. *
  25. * This class exists only to support legacy code that cannot be dependency
  26. * injected. If your code needs it, consider refactoring it to be object
  27. * oriented, if possible. When this is not possible, for instance in the case of
  28. * hook implementations, and your code is more than a few non-reusable lines, it
  29. * is recommended to instantiate an object implementing the actual logic.
  30. *
  31. * @code
  32. * // Legacy procedural code.
  33. * function hook_do_stuff() {
  34. * $lock = lock()->acquire('stuff_lock');
  35. * // ...
  36. * }
  37. *
  38. * // Correct procedural code.
  39. * function hook_do_stuff() {
  40. * $lock = \Drupal::lock()->acquire('stuff_lock');
  41. * // ...
  42. * }
  43. *
  44. * // The preferred way: dependency injected code.
  45. * function hook_do_stuff() {
  46. * // Move the actual implementation to a class and instantiate it.
  47. * $instance = new StuffDoingClass(\Drupal::lock());
  48. * $instance->doStuff();
  49. *
  50. * // Or, even better, rely on the service container to avoid hard coding a
  51. * // specific interface implementation, so that the actual logic can be
  52. * // swapped. This might not always make sense, but in general it is a good
  53. * // practice.
  54. * \Drupal::service('stuff.doing')->doStuff();
  55. * }
  56. *
  57. * interface StuffDoingInterface {
  58. * public function doStuff();
  59. * }
  60. *
  61. * class StuffDoingClass implements StuffDoingInterface {
  62. * protected $lockBackend;
  63. *
  64. * public function __construct(LockBackendInterface $lock_backend) {
  65. * $this->lockBackend = $lock_backend;
  66. * }
  67. *
  68. * public function doStuff() {
  69. * $lock = $this->lockBackend->acquire('stuff_lock');
  70. * // ...
  71. * }
  72. * }
  73. * @endcode
  74. *
  75. * @see \Drupal\Core\DrupalKernel
  76. */
  77. class Drupal {
  78. /**
  79. * The current system version.
  80. */
  81. const VERSION = '8.9.7';
  82. /**
  83. * Core API compatibility.
  84. */
  85. const CORE_COMPATIBILITY = '8.x';
  86. /**
  87. * Core minimum schema version.
  88. */
  89. const CORE_MINIMUM_SCHEMA_VERSION = 8000;
  90. /**
  91. * The currently active container object, or NULL if not initialized yet.
  92. *
  93. * @var \Symfony\Component\DependencyInjection\ContainerInterface|null
  94. */
  95. protected static $container;
  96. /**
  97. * Sets a new global container.
  98. *
  99. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  100. * A new container instance to replace the current.
  101. */
  102. public static function setContainer(ContainerInterface $container) {
  103. static::$container = $container;
  104. }
  105. /**
  106. * Unsets the global container.
  107. */
  108. public static function unsetContainer() {
  109. static::$container = NULL;
  110. }
  111. /**
  112. * Returns the currently active global container.
  113. *
  114. * @return \Symfony\Component\DependencyInjection\ContainerInterface
  115. *
  116. * @throws \Drupal\Core\DependencyInjection\ContainerNotInitializedException
  117. */
  118. public static function getContainer() {
  119. if (static::$container === NULL) {
  120. throw new ContainerNotInitializedException('\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.');
  121. }
  122. return static::$container;
  123. }
  124. /**
  125. * Returns TRUE if the container has been initialized, FALSE otherwise.
  126. *
  127. * @return bool
  128. */
  129. public static function hasContainer() {
  130. return static::$container !== NULL;
  131. }
  132. /**
  133. * Retrieves a service from the container.
  134. *
  135. * Use this method if the desired service is not one of those with a dedicated
  136. * accessor method below. If it is listed below, those methods are preferred
  137. * as they can return useful type hints.
  138. *
  139. * @param string $id
  140. * The ID of the service to retrieve.
  141. *
  142. * @return mixed
  143. * The specified service.
  144. */
  145. public static function service($id) {
  146. return static::getContainer()->get($id);
  147. }
  148. /**
  149. * Indicates if a service is defined in the container.
  150. *
  151. * @param string $id
  152. * The ID of the service to check.
  153. *
  154. * @return bool
  155. * TRUE if the specified service exists, FALSE otherwise.
  156. */
  157. public static function hasService($id) {
  158. // Check hasContainer() first in order to always return a Boolean.
  159. return static::hasContainer() && static::getContainer()->has($id);
  160. }
  161. /**
  162. * Gets the app root.
  163. *
  164. * @return string
  165. */
  166. public static function root() {
  167. return static::getContainer()->get('app.root');
  168. }
  169. /**
  170. * Gets the active install profile.
  171. *
  172. * @return string|null
  173. * The name of the active install profile.
  174. */
  175. public static function installProfile() {
  176. return static::getContainer()->getParameter('install_profile');
  177. }
  178. /**
  179. * Indicates if there is a currently active request object.
  180. *
  181. * @return bool
  182. * TRUE if there is a currently active request object, FALSE otherwise.
  183. */
  184. public static function hasRequest() {
  185. // Check hasContainer() first in order to always return a Boolean.
  186. return static::hasContainer() && static::getContainer()->has('request_stack') && static::getContainer()->get('request_stack')->getCurrentRequest() !== NULL;
  187. }
  188. /**
  189. * Retrieves the currently active request object.
  190. *
  191. * Note: The use of this wrapper in particular is especially discouraged. Most
  192. * code should not need to access the request directly. Doing so means it
  193. * will only function when handling an HTTP request, and will require special
  194. * modification or wrapping when run from a command line tool, from certain
  195. * queue processors, or from automated tests.
  196. *
  197. * If code must access the request, it is considerably better to register
  198. * an object with the Service Container and give it a setRequest() method
  199. * that is configured to run when the service is created. That way, the
  200. * correct request object can always be provided by the container and the
  201. * service can still be unit tested.
  202. *
  203. * If this method must be used, never save the request object that is
  204. * returned. Doing so may lead to inconsistencies as the request object is
  205. * volatile and may change at various times, such as during a subrequest.
  206. *
  207. * @return \Symfony\Component\HttpFoundation\Request
  208. * The currently active request object.
  209. */
  210. public static function request() {
  211. return static::getContainer()->get('request_stack')->getCurrentRequest();
  212. }
  213. /**
  214. * Retrieves the request stack.
  215. *
  216. * @return \Symfony\Component\HttpFoundation\RequestStack
  217. * The request stack
  218. */
  219. public static function requestStack() {
  220. return static::getContainer()->get('request_stack');
  221. }
  222. /**
  223. * Retrieves the currently active route match object.
  224. *
  225. * @return \Drupal\Core\Routing\RouteMatchInterface
  226. * The currently active route match object.
  227. */
  228. public static function routeMatch() {
  229. return static::getContainer()->get('current_route_match');
  230. }
  231. /**
  232. * Gets the current active user.
  233. *
  234. * This method will return the \Drupal\Core\Session\AccountProxy object of the
  235. * current user. You can use the \Drupal\user\Entity\User::load() method to
  236. * load the full user entity object. For example:
  237. * @code
  238. * $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
  239. * @endcode
  240. *
  241. * @return \Drupal\Core\Session\AccountProxyInterface
  242. */
  243. public static function currentUser() {
  244. return static::getContainer()->get('current_user');
  245. }
  246. /**
  247. * Retrieves the entity manager service.
  248. *
  249. * @return \Drupal\Core\Entity\EntityManagerInterface
  250. * The entity manager service.
  251. *
  252. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  253. * Use \Drupal::entityTypeManager() instead in most cases. If the needed
  254. * method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the
  255. * deprecated \Drupal\Core\Entity\EntityManager to find the
  256. * correct interface or service.
  257. */
  258. public static function entityManager() {
  259. @trigger_error("\Drupal::entityManager() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager() instead in most cases. If the needed method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the deprecated \Drupal\Core\Entity\EntityManager to find the correct interface or service. See https://www.drupal.org/node/2549139", E_USER_DEPRECATED);
  260. return static::getContainer()->get('entity.manager');
  261. }
  262. /**
  263. * Retrieves the entity type manager.
  264. *
  265. * @return \Drupal\Core\Entity\EntityTypeManagerInterface
  266. * The entity type manager.
  267. */
  268. public static function entityTypeManager() {
  269. return static::getContainer()->get('entity_type.manager');
  270. }
  271. /**
  272. * Returns the current primary database.
  273. *
  274. * @return \Drupal\Core\Database\Connection
  275. * The current active database's master connection.
  276. */
  277. public static function database() {
  278. return static::getContainer()->get('database');
  279. }
  280. /**
  281. * Returns the requested cache bin.
  282. *
  283. * @param string $bin
  284. * (optional) The cache bin for which the cache object should be returned,
  285. * defaults to 'default'.
  286. *
  287. * @return \Drupal\Core\Cache\CacheBackendInterface
  288. * The cache object associated with the specified bin.
  289. *
  290. * @ingroup cache
  291. */
  292. public static function cache($bin = 'default') {
  293. return static::getContainer()->get('cache.' . $bin);
  294. }
  295. /**
  296. * Retrieves the class resolver.
  297. *
  298. * This is to be used in procedural code such as module files to instantiate
  299. * an object of a class that implements
  300. * \Drupal\Core\DependencyInjection\ContainerInjectionInterface.
  301. *
  302. * One common usecase is to provide a class which contains the actual code
  303. * of a hook implementation, without having to create a service.
  304. *
  305. * @param string $class
  306. * (optional) A class name to instantiate.
  307. *
  308. * @return \Drupal\Core\DependencyInjection\ClassResolverInterface|object
  309. * The class resolver or if $class is provided, a class instance with a
  310. * given class definition.
  311. *
  312. * @throws \InvalidArgumentException
  313. * If $class does not exist.
  314. */
  315. public static function classResolver($class = NULL) {
  316. if ($class) {
  317. return static::getContainer()->get('class_resolver')->getInstanceFromDefinition($class);
  318. }
  319. return static::getContainer()->get('class_resolver');
  320. }
  321. /**
  322. * Returns an expirable key value store collection.
  323. *
  324. * @param string $collection
  325. * The name of the collection holding key and value pairs.
  326. *
  327. * @return \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
  328. * An expirable key value store collection.
  329. */
  330. public static function keyValueExpirable($collection) {
  331. return static::getContainer()->get('keyvalue.expirable')->get($collection);
  332. }
  333. /**
  334. * Returns the locking layer instance.
  335. *
  336. * @return \Drupal\Core\Lock\LockBackendInterface
  337. *
  338. * @ingroup lock
  339. */
  340. public static function lock() {
  341. return static::getContainer()->get('lock');
  342. }
  343. /**
  344. * Retrieves a configuration object.
  345. *
  346. * This is the main entry point to the configuration API. Calling
  347. * @code \Drupal::config('book.admin') @endcode will return a configuration
  348. * object the Book module can use to read its administrative settings.
  349. *
  350. * @param string $name
  351. * The name of the configuration object to retrieve, which typically
  352. * corresponds to a configuration file. For
  353. * @code \Drupal::config('book.admin') @endcode, the configuration
  354. * object returned will contain the content of the book.admin
  355. * configuration file.
  356. *
  357. * @return \Drupal\Core\Config\ImmutableConfig
  358. * An immutable configuration object.
  359. */
  360. public static function config($name) {
  361. return static::getContainer()->get('config.factory')->get($name);
  362. }
  363. /**
  364. * Retrieves the configuration factory.
  365. *
  366. * This is mostly used to change the override settings on the configuration
  367. * factory. For example, changing the language, or turning all overrides on
  368. * or off.
  369. *
  370. * @return \Drupal\Core\Config\ConfigFactoryInterface
  371. * The configuration factory service.
  372. */
  373. public static function configFactory() {
  374. return static::getContainer()->get('config.factory');
  375. }
  376. /**
  377. * Returns a queue for the given queue name.
  378. *
  379. * The following values can be set in your settings.php file's $settings
  380. * array to define which services are used for queues:
  381. * - queue_reliable_service_$name: The container service to use for the
  382. * reliable queue $name.
  383. * - queue_service_$name: The container service to use for the
  384. * queue $name.
  385. * - queue_default: The container service to use by default for queues
  386. * without overrides. This defaults to 'queue.database'.
  387. *
  388. * @param string $name
  389. * The name of the queue to work with.
  390. * @param bool $reliable
  391. * (optional) TRUE if the ordering of items and guaranteeing every item
  392. * executes at least once is important, FALSE if scalability is the main
  393. * concern. Defaults to FALSE.
  394. *
  395. * @return \Drupal\Core\Queue\QueueInterface
  396. * The queue object for a given name.
  397. */
  398. public static function queue($name, $reliable = FALSE) {
  399. return static::getContainer()->get('queue')->get($name, $reliable);
  400. }
  401. /**
  402. * Returns a key/value storage collection.
  403. *
  404. * @param string $collection
  405. * Name of the key/value collection to return.
  406. *
  407. * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
  408. */
  409. public static function keyValue($collection) {
  410. return static::getContainer()->get('keyvalue')->get($collection);
  411. }
  412. /**
  413. * Returns the state storage service.
  414. *
  415. * Use this to store machine-generated data, local to a specific environment
  416. * that does not need deploying and does not need human editing; for example,
  417. * the last time cron was run. Data which needs to be edited by humans and
  418. * needs to be the same across development, production, etc. environments
  419. * (for example, the system maintenance message) should use \Drupal::config() instead.
  420. *
  421. * @return \Drupal\Core\State\StateInterface
  422. */
  423. public static function state() {
  424. return static::getContainer()->get('state');
  425. }
  426. /**
  427. * Returns the default http client.
  428. *
  429. * @return \GuzzleHttp\Client
  430. * A guzzle http client instance.
  431. */
  432. public static function httpClient() {
  433. return static::getContainer()->get('http_client');
  434. }
  435. /**
  436. * Returns the entity query object for this entity type.
  437. *
  438. * @param string $entity_type
  439. * The entity type (for example, node) for which the query object should be
  440. * returned.
  441. * @param string $conjunction
  442. * (optional) Either 'AND' if all conditions in the query need to apply, or
  443. * 'OR' if any of them is sufficient. Defaults to 'AND'.
  444. *
  445. * @return \Drupal\Core\Entity\Query\QueryInterface
  446. * The query object that can query the given entity type.
  447. */
  448. public static function entityQuery($entity_type, $conjunction = 'AND') {
  449. return static::entityTypeManager()->getStorage($entity_type)->getQuery($conjunction);
  450. }
  451. /**
  452. * Returns the entity query aggregate object for this entity type.
  453. *
  454. * @param string $entity_type
  455. * The entity type (for example, node) for which the query object should be
  456. * returned.
  457. * @param string $conjunction
  458. * (optional) Either 'AND' if all conditions in the query need to apply, or
  459. * 'OR' if any of them is sufficient. Defaults to 'AND'.
  460. *
  461. * @return \Drupal\Core\Entity\Query\QueryAggregateInterface
  462. * The query object that can query the given entity type.
  463. */
  464. public static function entityQueryAggregate($entity_type, $conjunction = 'AND') {
  465. return static::entityTypeManager()->getStorage($entity_type)->getAggregateQuery($conjunction);
  466. }
  467. /**
  468. * Returns the flood instance.
  469. *
  470. * @return \Drupal\Core\Flood\FloodInterface
  471. */
  472. public static function flood() {
  473. return static::getContainer()->get('flood');
  474. }
  475. /**
  476. * Returns the module handler.
  477. *
  478. * @return \Drupal\Core\Extension\ModuleHandlerInterface
  479. */
  480. public static function moduleHandler() {
  481. return static::getContainer()->get('module_handler');
  482. }
  483. /**
  484. * Returns the typed data manager service.
  485. *
  486. * Use the typed data manager service for creating typed data objects.
  487. *
  488. * @return \Drupal\Core\TypedData\TypedDataManagerInterface
  489. * The typed data manager.
  490. *
  491. * @see \Drupal\Core\TypedData\TypedDataManager::create()
  492. */
  493. public static function typedDataManager() {
  494. return static::getContainer()->get('typed_data_manager');
  495. }
  496. /**
  497. * Returns the token service.
  498. *
  499. * @return \Drupal\Core\Utility\Token
  500. * The token service.
  501. */
  502. public static function token() {
  503. return static::getContainer()->get('token');
  504. }
  505. /**
  506. * Returns the url generator service.
  507. *
  508. * @return \Drupal\Core\Routing\UrlGeneratorInterface
  509. * The url generator service.
  510. */
  511. public static function urlGenerator() {
  512. return static::getContainer()->get('url_generator');
  513. }
  514. /**
  515. * Generates a URL string for a specific route based on the given parameters.
  516. *
  517. * This method is a convenience wrapper for generating URL strings for URLs
  518. * that have Drupal routes (that is, most pages generated by Drupal) using
  519. * the \Drupal\Core\Url object. See \Drupal\Core\Url::fromRoute() for
  520. * detailed documentation. For non-routed local URIs relative to
  521. * the base path (like robots.txt) use Url::fromUri()->toString() with the
  522. * base: scheme.
  523. *
  524. * @param string $route_name
  525. * The name of the route.
  526. * @param array $route_parameters
  527. * (optional) An associative array of parameter names and values.
  528. * @param array $options
  529. * (optional) An associative array of additional options.
  530. * @param bool $collect_bubbleable_metadata
  531. * (optional) Defaults to FALSE. When TRUE, both the generated URL and its
  532. * associated bubbleable metadata are returned.
  533. *
  534. * @return string|\Drupal\Core\GeneratedUrl
  535. * A string containing a URL to the given path.
  536. * When $collect_bubbleable_metadata is TRUE, a GeneratedUrl object is
  537. * returned, containing the generated URL plus bubbleable metadata.
  538. *
  539. * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
  540. * @see \Drupal\Core\Url
  541. * @see \Drupal\Core\Url::fromRoute()
  542. * @see \Drupal\Core\Url::fromUri()
  543. *
  544. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  545. * Instead create a \Drupal\Core\Url object directly, for example using
  546. * Url::fromRoute().
  547. */
  548. public static function url($route_name, $route_parameters = [], $options = [], $collect_bubbleable_metadata = FALSE) {
  549. @trigger_error('Drupal::url() is deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Instead create a \Drupal\Core\Url object directly, for example using Url::fromRoute()', E_USER_DEPRECATED);
  550. return static::getContainer()->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options, $collect_bubbleable_metadata);
  551. }
  552. /**
  553. * Returns the link generator service.
  554. *
  555. * @return \Drupal\Core\Utility\LinkGeneratorInterface
  556. */
  557. public static function linkGenerator() {
  558. return static::getContainer()->get('link_generator');
  559. }
  560. /**
  561. * Renders a link with a given link text and Url object.
  562. *
  563. * This method is a convenience wrapper for the link generator service's
  564. * generate() method.
  565. *
  566. * @param string|array|\Drupal\Component\Render\MarkupInterface $text
  567. * The link text for the anchor tag as a translated string or render array.
  568. * @param \Drupal\Core\Url $url
  569. * The URL object used for the link.
  570. *
  571. * @return \Drupal\Core\GeneratedLink
  572. * A GeneratedLink object containing a link to the given route and
  573. * parameters and bubbleable metadata.
  574. *
  575. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  576. * \Drupal\Core\Link::fromTextAndUrl() instead.
  577. *
  578. * @see https://www.drupal.org/node/2614344
  579. * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
  580. * @see \Drupal\Core\Url
  581. */
  582. public static function l($text, Url $url) {
  583. @trigger_error('\Drupal::l() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Link::fromTextAndUrl() instead. See https://www.drupal.org/node/2614344', E_USER_DEPRECATED);
  584. return static::getContainer()->get('link_generator')->generate($text, $url);
  585. }
  586. /**
  587. * Returns the string translation service.
  588. *
  589. * @return \Drupal\Core\StringTranslation\TranslationManager
  590. * The string translation manager.
  591. */
  592. public static function translation() {
  593. return static::getContainer()->get('string_translation');
  594. }
  595. /**
  596. * Returns the language manager service.
  597. *
  598. * @return \Drupal\Core\Language\LanguageManagerInterface
  599. * The language manager.
  600. */
  601. public static function languageManager() {
  602. return static::getContainer()->get('language_manager');
  603. }
  604. /**
  605. * Returns the CSRF token manager service.
  606. *
  607. * The generated token is based on the session ID of the current user. Normally,
  608. * anonymous users do not have a session, so the generated token will be
  609. * different on every page request. To generate a token for users without a
  610. * session, manually start a session prior to calling this function.
  611. *
  612. * @return \Drupal\Core\Access\CsrfTokenGenerator
  613. * The CSRF token manager.
  614. *
  615. * @see \Drupal\Core\Session\SessionManager::start()
  616. */
  617. public static function csrfToken() {
  618. return static::getContainer()->get('csrf_token');
  619. }
  620. /**
  621. * Returns the transliteration service.
  622. *
  623. * @return \Drupal\Core\Transliteration\PhpTransliteration
  624. * The transliteration manager.
  625. */
  626. public static function transliteration() {
  627. return static::getContainer()->get('transliteration');
  628. }
  629. /**
  630. * Returns the form builder service.
  631. *
  632. * @return \Drupal\Core\Form\FormBuilderInterface
  633. * The form builder.
  634. */
  635. public static function formBuilder() {
  636. return static::getContainer()->get('form_builder');
  637. }
  638. /**
  639. * Gets the theme service.
  640. *
  641. * @return \Drupal\Core\Theme\ThemeManagerInterface
  642. */
  643. public static function theme() {
  644. return static::getContainer()->get('theme.manager');
  645. }
  646. /**
  647. * Gets the syncing state.
  648. *
  649. * @return bool
  650. * Returns TRUE is syncing flag set.
  651. */
  652. public static function isConfigSyncing() {
  653. return static::getContainer()->get('config.installer')->isSyncing();
  654. }
  655. /**
  656. * Returns a channel logger object.
  657. *
  658. * @param string $channel
  659. * The name of the channel. Can be any string, but the general practice is
  660. * to use the name of the subsystem calling this.
  661. *
  662. * @return \Psr\Log\LoggerInterface
  663. * The logger for this channel.
  664. */
  665. public static function logger($channel) {
  666. return static::getContainer()->get('logger.factory')->get($channel);
  667. }
  668. /**
  669. * Returns the menu tree.
  670. *
  671. * @return \Drupal\Core\Menu\MenuLinkTreeInterface
  672. * The menu tree.
  673. */
  674. public static function menuTree() {
  675. return static::getContainer()->get('menu.link_tree');
  676. }
  677. /**
  678. * Returns the path validator.
  679. *
  680. * @return \Drupal\Core\Path\PathValidatorInterface
  681. */
  682. public static function pathValidator() {
  683. return static::getContainer()->get('path.validator');
  684. }
  685. /**
  686. * Returns the access manager service.
  687. *
  688. * @return \Drupal\Core\Access\AccessManagerInterface
  689. * The access manager service.
  690. */
  691. public static function accessManager() {
  692. return static::getContainer()->get('access_manager');
  693. }
  694. /**
  695. * Returns the redirect destination helper.
  696. *
  697. * @return \Drupal\Core\Routing\RedirectDestinationInterface
  698. * The redirect destination helper.
  699. */
  700. public static function destination() {
  701. return static::getContainer()->get('redirect.destination');
  702. }
  703. /**
  704. * Returns the entity definition update manager.
  705. *
  706. * @return \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
  707. * The entity definition update manager.
  708. */
  709. public static function entityDefinitionUpdateManager() {
  710. return static::getContainer()->get('entity.definition_update_manager');
  711. }
  712. /**
  713. * Returns the time service.
  714. *
  715. * @return \Drupal\Component\Datetime\TimeInterface
  716. * The time service.
  717. */
  718. public static function time() {
  719. return static::getContainer()->get('datetime.time');
  720. }
  721. /**
  722. * Returns the messenger.
  723. *
  724. * @return \Drupal\Core\Messenger\MessengerInterface
  725. * The messenger.
  726. */
  727. public static function messenger() {
  728. // @todo Replace with service once LegacyMessenger is removed in 9.0.0.
  729. // @see https://www.drupal.org/node/2928994
  730. return new LegacyMessenger();
  731. }
  732. }