common.inc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. <?php
  2. /**
  3. * @file
  4. * Common functions that many Drupal modules will need to reference.
  5. *
  6. * The functions that are critical and need to be available even when serving
  7. * a cached page are instead located in bootstrap.inc.
  8. */
  9. use Drupal\Component\Gettext\PoItem;
  10. use Drupal\Component\Utility\Bytes;
  11. use Drupal\Component\Utility\Environment;
  12. use Drupal\Component\Utility\Html;
  13. use Drupal\Component\Utility\SortArray;
  14. use Drupal\Component\Utility\UrlHelper;
  15. use Drupal\Core\Cache\Cache;
  16. use Drupal\Core\Form\FormHelper;
  17. use Drupal\Core\Render\Element;
  18. use Drupal\Core\Render\Element\Link;
  19. use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
  20. use Drupal\Core\Render\Markup;
  21. use Drupal\Core\StringTranslation\TranslatableMarkup;
  22. /**
  23. * @defgroup php_wrappers PHP wrapper functions
  24. * @{
  25. * Functions that are wrappers or custom implementations of PHP functions.
  26. *
  27. * Certain PHP functions should not be used in Drupal. Instead, Drupal's
  28. * replacement functions should be used.
  29. *
  30. * For example, for improved or more secure UTF8-handling, or RFC-compliant
  31. * handling of URLs in Drupal.
  32. *
  33. * For ease of use and memorizing, all these wrapper functions use the same name
  34. * as the original PHP function, but prefixed with "drupal_". Beware, however,
  35. * that not all wrapper functions support the same arguments as the original
  36. * functions.
  37. *
  38. * You should always use these wrapper functions in your code.
  39. *
  40. * Wrong:
  41. * @code
  42. * $my_substring = substr($original_string, 0, 5);
  43. * @endcode
  44. *
  45. * Correct:
  46. * @code
  47. * $my_substring = mb_substr($original_string, 0, 5);
  48. * @endcode
  49. *
  50. * @}
  51. */
  52. /**
  53. * Return status for saving which involved creating a new item.
  54. */
  55. const SAVED_NEW = 1;
  56. /**
  57. * Return status for saving which involved an update to an existing item.
  58. */
  59. const SAVED_UPDATED = 2;
  60. /**
  61. * Return status for saving which deleted an existing item.
  62. */
  63. const SAVED_DELETED = 3;
  64. /**
  65. * The default aggregation group for CSS files added to the page.
  66. */
  67. const CSS_AGGREGATE_DEFAULT = 0;
  68. /**
  69. * The default aggregation group for theme CSS files added to the page.
  70. */
  71. const CSS_AGGREGATE_THEME = 100;
  72. /**
  73. * The default weight for CSS rules that style HTML elements ("base" styles).
  74. */
  75. const CSS_BASE = -200;
  76. /**
  77. * The default weight for CSS rules that layout a page.
  78. */
  79. const CSS_LAYOUT = -100;
  80. /**
  81. * The default weight for CSS rules that style design components (and their associated states and themes.)
  82. */
  83. const CSS_COMPONENT = 0;
  84. /**
  85. * The default weight for CSS rules that style states and are not included with components.
  86. */
  87. const CSS_STATE = 100;
  88. /**
  89. * The default weight for CSS rules that style themes and are not included with components.
  90. */
  91. const CSS_THEME = 200;
  92. /**
  93. * The default group for JavaScript settings added to the page.
  94. */
  95. const JS_SETTING = -200;
  96. /**
  97. * The default group for JavaScript and jQuery libraries added to the page.
  98. */
  99. const JS_LIBRARY = -100;
  100. /**
  101. * The default group for module JavaScript code added to the page.
  102. */
  103. const JS_DEFAULT = 0;
  104. /**
  105. * The default group for theme JavaScript code added to the page.
  106. */
  107. const JS_THEME = 100;
  108. /**
  109. * The delimiter used to split plural strings.
  110. *
  111. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  112. * Use Drupal\Component\Gettext\PoItem::DELIMITER instead.
  113. */
  114. const LOCALE_PLURAL_DELIMITER = PoItem::DELIMITER;
  115. /**
  116. * Prepares a 'destination' URL query parameter.
  117. *
  118. * Used to direct the user back to the referring page after completing a form.
  119. * By default the current URL is returned. If a destination exists in the
  120. * previous request, that destination is returned. As such, a destination can
  121. * persist across multiple pages.
  122. *
  123. * @return array
  124. * An associative array containing the key:
  125. * - destination: The value of the current request's 'destination' query
  126. * parameter, if present. This can be either a relative or absolute URL.
  127. * However, for security, redirection to external URLs is not performed.
  128. * If the query parameter isn't present, then the URL of the current
  129. * request is returned.
  130. *
  131. * @ingroup form_api
  132. *
  133. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  134. * Use the redirect.destination service.
  135. *
  136. * @see \Drupal\Core\EventSubscriber\RedirectResponseSubscriber::checkRedirectUrl()
  137. * @see https://www.drupal.org/node/2448603
  138. */
  139. function drupal_get_destination() {
  140. return \Drupal::destination()->getAsArray();
  141. }
  142. /**
  143. * @defgroup validation Input validation
  144. * @{
  145. * Functions to validate user input.
  146. */
  147. /**
  148. * Verifies the syntax of the given email address.
  149. *
  150. * @param string $mail
  151. * A string containing an email address.
  152. *
  153. * @return bool
  154. * TRUE if the address is in a valid format.
  155. *
  156. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  157. * Use \Drupal::service('email.validator')->isValid().
  158. *
  159. * @see https://www.drupal.org/node/2912661
  160. */
  161. function valid_email_address($mail) {
  162. return \Drupal::service('email.validator')->isValid($mail);
  163. }
  164. /**
  165. * @} End of "defgroup validation".
  166. */
  167. /**
  168. * @defgroup sanitization Sanitization functions
  169. * @{
  170. * Functions to sanitize values.
  171. *
  172. * See https://www.drupal.org/writing-secure-code for information
  173. * on writing secure code.
  174. */
  175. /**
  176. * Strips dangerous protocols from a URI and encodes it for output to HTML.
  177. *
  178. * @param $uri
  179. * A plain-text URI that might contain dangerous protocols.
  180. *
  181. * @return string
  182. * A URI stripped of dangerous protocols and encoded for output to an HTML
  183. * attribute value. Because it is already encoded, it should not be set as a
  184. * value within a $attributes array passed to Drupal\Core\Template\Attribute,
  185. * because Drupal\Core\Template\Attribute expects those values to be
  186. * plain-text strings. To pass a filtered URI to
  187. * Drupal\Core\Template\Attribute, call
  188. * \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() instead.
  189. *
  190. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  191. * Use UrlHelper::stripDangerousProtocols() or UrlHelper::filterBadProtocol()
  192. * instead. UrlHelper::stripDangerousProtocols() can be used in conjunction
  193. * with \Drupal\Component\Render\FormattableMarkup and an @variable
  194. * placeholder which will perform the necessary escaping.
  195. * UrlHelper::filterBadProtocol() is functionality equivalent to check_url()
  196. * apart from the fact it is protected from double escaping bugs. Note that
  197. * this method no longer marks its output as safe.
  198. *
  199. * @see \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols()
  200. * @see \Drupal\Component\Utility\UrlHelper::filterBadProtocol()
  201. * @see https://www.drupal.org/node/2560027
  202. */
  203. function check_url($uri) {
  204. @trigger_error(__FUNCTION__ . '() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use UrlHelper::stripDangerousProtocols() or UrlHelper::filterBadProtocol() instead. See https://www.drupal.org/node/2560027', E_USER_DEPRECATED);
  205. return Html::escape(UrlHelper::stripDangerousProtocols($uri));
  206. }
  207. /**
  208. * @} End of "defgroup sanitization".
  209. */
  210. /**
  211. * @defgroup format Formatting
  212. * @{
  213. * Functions to format numbers, strings, dates, etc.
  214. */
  215. /**
  216. * Generates a string representation for the given byte count.
  217. *
  218. * @param $size
  219. * A size in bytes.
  220. * @param $langcode
  221. * Optional language code to translate to a language other than what is used
  222. * to display the page.
  223. *
  224. * @return \Drupal\Core\StringTranslation\TranslatableMarkup
  225. * A translated string representation of the size.
  226. */
  227. function format_size($size, $langcode = NULL) {
  228. $absolute_size = abs($size);
  229. if ($absolute_size < Bytes::KILOBYTE) {
  230. return \Drupal::translation()->formatPlural($size, '1 byte', '@count bytes', [], ['langcode' => $langcode]);
  231. }
  232. // Create a multiplier to preserve the sign of $size.
  233. $sign = $absolute_size / $size;
  234. foreach (['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] as $unit) {
  235. $absolute_size /= Bytes::KILOBYTE;
  236. $rounded_size = round($absolute_size, 2);
  237. if ($rounded_size < Bytes::KILOBYTE) {
  238. break;
  239. }
  240. }
  241. $args = ['@size' => $rounded_size * $sign];
  242. $options = ['langcode' => $langcode];
  243. switch ($unit) {
  244. case 'KB':
  245. return new TranslatableMarkup('@size KB', $args, $options);
  246. case 'MB':
  247. return new TranslatableMarkup('@size MB', $args, $options);
  248. case 'GB':
  249. return new TranslatableMarkup('@size GB', $args, $options);
  250. case 'TB':
  251. return new TranslatableMarkup('@size TB', $args, $options);
  252. case 'PB':
  253. return new TranslatableMarkup('@size PB', $args, $options);
  254. case 'EB':
  255. return new TranslatableMarkup('@size EB', $args, $options);
  256. case 'ZB':
  257. return new TranslatableMarkup('@size ZB', $args, $options);
  258. case 'YB':
  259. return new TranslatableMarkup('@size YB', $args, $options);
  260. }
  261. }
  262. /**
  263. * Formats a date, using a date type or a custom date format string.
  264. *
  265. * @param $timestamp
  266. * A UNIX timestamp to format.
  267. * @param $type
  268. * (optional) The format to use, one of:
  269. * - One of the built-in formats: 'short', 'medium',
  270. * 'long', 'html_datetime', 'html_date', 'html_time',
  271. * 'html_yearless_date', 'html_week', 'html_month', 'html_year'.
  272. * - The name of a date type defined by a date format config entity.
  273. * - The machine name of an administrator-defined date format.
  274. * - 'custom', to use $format.
  275. * Defaults to 'medium'.
  276. * @param $format
  277. * (optional) If $type is 'custom', a PHP date format string suitable for
  278. * input to date(). Use a backslash to escape ordinary text, so it does not
  279. * get interpreted as date format characters.
  280. * @param $timezone
  281. * (optional) Time zone identifier, as described at
  282. * http://php.net/manual/timezones.php Defaults to the time zone used to
  283. * display the page.
  284. * @param $langcode
  285. * (optional) Language code to translate to. Defaults to the language used to
  286. * display the page.
  287. *
  288. * @return
  289. * A translated date string in the requested format.
  290. *
  291. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  292. * Use \Drupal::service('date.formatter')->format().
  293. *
  294. * @see \Drupal\Core\Datetime\DateFormatter::format()
  295. * @see https://www.drupal.org/node/1876852
  296. */
  297. function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
  298. @trigger_error("format_date() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::service('date.formatter')->format() instead. See https://www.drupal.org/node/1876852", E_USER_DEPRECATED);
  299. return \Drupal::service('date.formatter')->format($timestamp, $type, $format, $timezone, $langcode);
  300. }
  301. /**
  302. * Returns an ISO8601 formatted date based on the given date.
  303. *
  304. * @param string $date
  305. * A UNIX timestamp.
  306. *
  307. * @return string
  308. * An ISO8601 formatted date.
  309. *
  310. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
  311. * date('c', $date) instead.
  312. *
  313. * @see https://www.drupal.org/node/2999991
  314. */
  315. function date_iso8601($date) {
  316. @trigger_error('date_iso8601() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use date("c", $date) instead. See https://www.drupal.org/node/2999991.', E_USER_DEPRECATED);
  317. // The DATE_ISO8601 constant cannot be used here because it does not match
  318. // date('c') and produces invalid RDF markup.
  319. return date('c', $date);
  320. }
  321. /**
  322. * @} End of "defgroup format".
  323. */
  324. /**
  325. * Formats an attribute string for an HTTP header.
  326. *
  327. * @param array $attributes
  328. * An associative array of attributes such as 'rel'.
  329. *
  330. * @return string
  331. * A ; separated string ready for insertion in a HTTP header. No escaping is
  332. * performed for HTML entities, so this string is not safe to be printed.
  333. *
  334. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
  335. * \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes()
  336. * instead.
  337. *
  338. * @see https://www.drupal.org/node/3000051
  339. */
  340. function drupal_http_header_attributes(array $attributes = []) {
  341. @trigger_error("drupal_http_header_attributes() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes() instead. See https://www.drupal.org/node/3000051", E_USER_DEPRECATED);
  342. return HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes($attributes);
  343. }
  344. /**
  345. * Attempts to set the PHP maximum execution time.
  346. *
  347. * This function is a wrapper around the PHP function set_time_limit().
  348. * When called, set_time_limit() restarts the timeout counter from zero.
  349. * In other words, if the timeout is the default 30 seconds, and 25 seconds
  350. * into script execution a call such as set_time_limit(20) is made, the
  351. * script will run for a total of 45 seconds before timing out.
  352. *
  353. * If the current time limit is not unlimited it is possible to decrease the
  354. * total time limit if the sum of the new time limit and the current time spent
  355. * running the script is inferior to the original time limit. It is inherent to
  356. * the way set_time_limit() works, it should rather be called with an
  357. * appropriate value every time you need to allocate a certain amount of time
  358. * to execute a task than only once at the beginning of the script.
  359. *
  360. * Before calling set_time_limit(), we check if this function is available
  361. * because it could be disabled by the server administrator. We also hide all
  362. * the errors that could occur when calling set_time_limit(), because it is
  363. * not possible to reliably ensure that PHP or a security extension will
  364. * not issue a warning/error if they prevent the use of this function.
  365. *
  366. * @param $time_limit
  367. * An integer specifying the new time limit, in seconds. A value of 0
  368. * indicates unlimited execution time.
  369. *
  370. * @ingroup php_wrappers
  371. *
  372. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
  373. * \Drupal\Component\Utility\Environment::setTimeLimit() instead.
  374. *
  375. * @see https://www.drupal.org/node/3000058
  376. */
  377. function drupal_set_time_limit($time_limit) {
  378. @trigger_error('drupal_set_time_limit() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Component\Utility\Environment::setTimeLimit() instead. See https://www.drupal.org/node/3000058.', E_USER_DEPRECATED);
  379. Environment::setTimeLimit($time_limit);
  380. }
  381. /**
  382. * Returns the base URL path (i.e., directory) of the Drupal installation.
  383. *
  384. * Function base_path() adds a "/" to the beginning and end of the returned path
  385. * if the path is not empty. At the very least, this will return "/".
  386. *
  387. * Examples:
  388. * - http://example.com returns "/" because the path is empty.
  389. * - http://example.com/drupal/folder returns "/drupal/folder/".
  390. */
  391. function base_path() {
  392. return $GLOBALS['base_path'];
  393. }
  394. /**
  395. * Deletes old cached CSS files.
  396. *
  397. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  398. * Use \Drupal\Core\Asset\AssetCollectionOptimizerInterface::deleteAll().
  399. *
  400. * @see https://www.drupal.org/node/2317841
  401. */
  402. function drupal_clear_css_cache() {
  403. \Drupal::service('asset.css.collection_optimizer')->deleteAll();
  404. }
  405. /**
  406. * Constructs an array of the defaults that are used for JavaScript assets.
  407. *
  408. * @param $data
  409. * (optional) The default data parameter for the JavaScript asset array.
  410. *
  411. * @see hook_js_alter()
  412. */
  413. function drupal_js_defaults($data = NULL) {
  414. return [
  415. 'type' => 'file',
  416. 'group' => JS_DEFAULT,
  417. 'weight' => 0,
  418. 'scope' => 'header',
  419. 'cache' => TRUE,
  420. 'preprocess' => TRUE,
  421. 'attributes' => [],
  422. 'version' => NULL,
  423. 'data' => $data,
  424. 'browsers' => [],
  425. ];
  426. }
  427. /**
  428. * Adds JavaScript to change the state of an element based on another element.
  429. *
  430. * A "state" means a certain property on a DOM element, such as "visible" or
  431. * "checked". A state can be applied to an element, depending on the state of
  432. * another element on the page. In general, states depend on HTML attributes and
  433. * DOM element properties, which change due to user interaction.
  434. *
  435. * Since states are driven by JavaScript only, it is important to understand
  436. * that all states are applied on presentation only, none of the states force
  437. * any server-side logic, and that they will not be applied for site visitors
  438. * without JavaScript support. All modules implementing states have to make
  439. * sure that the intended logic also works without JavaScript being enabled.
  440. *
  441. * #states is an associative array in the form of:
  442. * @code
  443. * array(
  444. * STATE1 => CONDITIONS_ARRAY1,
  445. * STATE2 => CONDITIONS_ARRAY2,
  446. * ...
  447. * )
  448. * @endcode
  449. * Each key is the name of a state to apply to the element, such as 'visible'.
  450. * Each value is a list of conditions that denote when the state should be
  451. * applied.
  452. *
  453. * Multiple different states may be specified to act on complex conditions:
  454. * @code
  455. * array(
  456. * 'visible' => CONDITIONS,
  457. * 'checked' => OTHER_CONDITIONS,
  458. * )
  459. * @endcode
  460. *
  461. * Every condition is a key/value pair, whose key is a jQuery selector that
  462. * denotes another element on the page, and whose value is an array of
  463. * conditions, which must be met on that element:
  464. * @code
  465. * array(
  466. * 'visible' => array(
  467. * JQUERY_SELECTOR => REMOTE_CONDITIONS,
  468. * JQUERY_SELECTOR => REMOTE_CONDITIONS,
  469. * ...
  470. * ),
  471. * )
  472. * @endcode
  473. * All conditions must be met for the state to be applied.
  474. *
  475. * Each remote condition is a key/value pair specifying conditions on the other
  476. * element that need to be met to apply the state to the element:
  477. * @code
  478. * array(
  479. * 'visible' => array(
  480. * ':input[name="remote_checkbox"]' => array('checked' => TRUE),
  481. * ),
  482. * )
  483. * @endcode
  484. *
  485. * For example, to show a textfield only when a checkbox is checked:
  486. * @code
  487. * $form['toggle_me'] = array(
  488. * '#type' => 'checkbox',
  489. * '#title' => t('Tick this box to type'),
  490. * );
  491. * $form['settings'] = array(
  492. * '#type' => 'textfield',
  493. * '#states' => array(
  494. * // Only show this field when the 'toggle_me' checkbox is enabled.
  495. * 'visible' => array(
  496. * ':input[name="toggle_me"]' => array('checked' => TRUE),
  497. * ),
  498. * ),
  499. * );
  500. * @endcode
  501. *
  502. * The following states may be applied to an element:
  503. * - enabled
  504. * - disabled
  505. * - required
  506. * - optional
  507. * - visible
  508. * - invisible
  509. * - checked
  510. * - unchecked
  511. * - expanded
  512. * - collapsed
  513. *
  514. * The following states may be used in remote conditions:
  515. * - empty
  516. * - filled
  517. * - checked
  518. * - unchecked
  519. * - expanded
  520. * - collapsed
  521. * - value
  522. *
  523. * The following states exist for both elements and remote conditions, but are
  524. * not fully implemented and may not change anything on the element:
  525. * - relevant
  526. * - irrelevant
  527. * - valid
  528. * - invalid
  529. * - touched
  530. * - untouched
  531. * - readwrite
  532. * - readonly
  533. *
  534. * When referencing select lists and radio buttons in remote conditions, a
  535. * 'value' condition must be used:
  536. * @code
  537. * '#states' => array(
  538. * // Show the settings if 'bar' has been selected for 'foo'.
  539. * 'visible' => array(
  540. * ':input[name="foo"]' => array('value' => 'bar'),
  541. * ),
  542. * ),
  543. * @endcode
  544. *
  545. * @param $elements
  546. * A renderable array element having a #states property as described above.
  547. *
  548. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  549. * \Drupal\Core\Form\FormHelper::processStates() instead.
  550. *
  551. * @see https://www.drupal.org/node/3000069
  552. * @see \Drupal\Core\Form\FormHelper::processStates()
  553. */
  554. function drupal_process_states(&$elements) {
  555. @trigger_error('drupal_process_states() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Form\FormHelper::processStates() instead. See https://www.drupal.org/node/3000069', E_USER_DEPRECATED);
  556. FormHelper::processStates($elements);
  557. }
  558. /**
  559. * Assists in attaching the tableDrag JavaScript behavior to a themed table.
  560. *
  561. * Draggable tables should be used wherever an outline or list of sortable items
  562. * needs to be arranged by an end-user. Draggable tables are very flexible and
  563. * can manipulate the value of form elements placed within individual columns.
  564. *
  565. * To set up a table to use drag and drop in place of weight select-lists or in
  566. * place of a form that contains parent relationships, the form must be themed
  567. * into a table. The table must have an ID attribute set and it
  568. * may be set as follows:
  569. * @code
  570. * $table = array(
  571. * '#type' => 'table',
  572. * '#header' => $header,
  573. * '#rows' => $rows,
  574. * '#attributes' => array(
  575. * 'id' => 'my-module-table',
  576. * ),
  577. * );
  578. * return \Drupal::service('renderer')->render($table);
  579. * @endcode
  580. *
  581. * In the theme function for the form, a special class must be added to each
  582. * form element within the same column, "grouping" them together.
  583. *
  584. * In a situation where a single weight column is being sorted in the table, the
  585. * classes could be added like this (in the theme function):
  586. * @code
  587. * $form['my_elements'][$delta]['weight']['#attributes']['class'] = array('my-elements-weight');
  588. * @endcode
  589. *
  590. * Each row of the table must also have a class of "draggable" in order to
  591. * enable the drag handles:
  592. * @code
  593. * $row = array(...);
  594. * $rows[] = array(
  595. * 'data' => $row,
  596. * 'class' => array('draggable'),
  597. * );
  598. * @endcode
  599. *
  600. * When tree relationships are present, the two additional classes
  601. * 'tabledrag-leaf' and 'tabledrag-root' can be used to refine the behavior:
  602. * - Rows with the 'tabledrag-leaf' class cannot have child rows.
  603. * - Rows with the 'tabledrag-root' class cannot be nested under a parent row.
  604. *
  605. * Calling drupal_attach_tabledrag() would then be written as such:
  606. * @code
  607. * drupal_attach_tabledrag('my-module-table', array(
  608. * 'action' => 'order',
  609. * 'relationship' => 'sibling',
  610. * 'group' => 'my-elements-weight',
  611. * );
  612. * @endcode
  613. *
  614. * In a more complex case where there are several groups in one column (such as
  615. * the block regions on the admin/structure/block page), a separate subgroup
  616. * class must also be added to differentiate the groups.
  617. * @code
  618. * $form['my_elements'][$region][$delta]['weight']['#attributes']['class'] = array('my-elements-weight', 'my-elements-weight-' . $region);
  619. * @endcode
  620. *
  621. * The 'group' option is still 'my-element-weight', and the additional
  622. * 'subgroup' option will be passed in as 'my-elements-weight-' . $region. This
  623. * also means that you'll need to call drupal_attach_tabledrag() once for every
  624. * region added.
  625. *
  626. * @code
  627. * foreach ($regions as $region) {
  628. * drupal_attach_tabledrag('my-module-table', array(
  629. * 'action' => 'order',
  630. * 'relationship' => 'sibling',
  631. * 'group' => 'my-elements-weight',
  632. * 'subgroup' => 'my-elements-weight-' . $region,
  633. * ));
  634. * }
  635. * @endcode
  636. *
  637. * In a situation where tree relationships are present, adding multiple
  638. * subgroups is not necessary, because the table will contain indentations that
  639. * provide enough information about the sibling and parent relationships. See
  640. * MenuForm::BuildOverviewForm for an example creating a table
  641. * containing parent relationships.
  642. *
  643. * @param $element
  644. * A form element to attach the tableDrag behavior to.
  645. * @param array $options
  646. * These options are used to generate JavaScript settings necessary to
  647. * configure the tableDrag behavior appropriately for this particular table.
  648. * An associative array containing the following keys:
  649. * - 'table_id': String containing the target table's id attribute.
  650. * If the table does not have an id, one will need to be set,
  651. * such as <table id="my-module-table">.
  652. * - 'action': String describing the action to be done on the form item.
  653. * Either 'match' 'depth', or 'order':
  654. * - 'match' is typically used for parent relationships.
  655. * - 'order' is typically used to set weights on other form elements with
  656. * the same group.
  657. * - 'depth' updates the target element with the current indentation.
  658. * - 'relationship': String describing where the "action" option
  659. * should be performed. Either 'parent', 'sibling', 'group', or 'self':
  660. * - 'parent' will only look for fields up the tree.
  661. * - 'sibling' will look for fields in the same group in rows above and
  662. * below it.
  663. * - 'self' affects the dragged row itself.
  664. * - 'group' affects the dragged row, plus any children below it (the entire
  665. * dragged group).
  666. * - 'group': A class name applied on all related form elements for this action.
  667. * - 'subgroup': (optional) If the group has several subgroups within it, this
  668. * string should contain the class name identifying fields in the same
  669. * subgroup.
  670. * - 'source': (optional) If the $action is 'match', this string should contain
  671. * the classname identifying what field will be used as the source value
  672. * when matching the value in $subgroup.
  673. * - 'hidden': (optional) The column containing the field elements may be
  674. * entirely hidden from view dynamically when the JavaScript is loaded. Set
  675. * to FALSE if the column should not be hidden.
  676. * - 'limit': (optional) Limit the maximum amount of parenting in this table.
  677. *
  678. * @see MenuForm::BuildOverviewForm()
  679. */
  680. function drupal_attach_tabledrag(&$element, array $options) {
  681. // Add default values to elements.
  682. $options = $options + [
  683. 'subgroup' => NULL,
  684. 'source' => NULL,
  685. 'hidden' => TRUE,
  686. 'limit' => 0,
  687. ];
  688. $group = $options['group'];
  689. $tabledrag_id = &drupal_static(__FUNCTION__);
  690. $tabledrag_id = (!isset($tabledrag_id)) ? 0 : $tabledrag_id + 1;
  691. // If a subgroup or source isn't set, assume it is the same as the group.
  692. $target = isset($options['subgroup']) ? $options['subgroup'] : $group;
  693. $source = isset($options['source']) ? $options['source'] : $target;
  694. $element['#attached']['drupalSettings']['tableDrag'][$options['table_id']][$group][$tabledrag_id] = [
  695. 'target' => $target,
  696. 'source' => $source,
  697. 'relationship' => $options['relationship'],
  698. 'action' => $options['action'],
  699. 'hidden' => $options['hidden'],
  700. 'limit' => $options['limit'],
  701. ];
  702. $element['#attached']['library'][] = 'core/drupal.tabledrag';
  703. }
  704. /**
  705. * Deletes old cached JavaScript files and variables.
  706. *
  707. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  708. * Use \Drupal\Core\Asset\AssetCollectionOptimizerInterface::deleteAll().
  709. *
  710. * @see https://www.drupal.org/node/2317841
  711. */
  712. function drupal_clear_js_cache() {
  713. \Drupal::service('asset.js.collection_optimizer')->deleteAll();
  714. }
  715. /**
  716. * Pre-render callback: Renders a link into #markup.
  717. *
  718. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  719. * Use \Drupal\Core\Render\Element\Link::preRenderLink().
  720. */
  721. function drupal_pre_render_link($element) {
  722. return Link::preRenderLink($element);
  723. }
  724. /**
  725. * Pre-render callback: Collects child links into a single array.
  726. *
  727. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  728. * \Drupal\Core\Render\Element\Link::preRenderLinks() instead.
  729. *
  730. * @see https://www.drupal.org/node/2966725
  731. */
  732. function drupal_pre_render_links($element) {
  733. @trigger_error('drupal_pre_render_links() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Render\Element\Link::preRenderLinks() instead. See https://www.drupal.org/node/2966725', E_USER_DEPRECATED);
  734. return Link::preRenderLinks($element);
  735. }
  736. /**
  737. * Renders final HTML given a structured array tree.
  738. *
  739. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  740. * \Drupal\Core\Render\RendererInterface::renderRoot() instead.
  741. *
  742. * @see \Drupal\Core\Render\RendererInterface::renderRoot()
  743. * @see https://www.drupal.org/node/2912696
  744. */
  745. function drupal_render_root(&$elements) {
  746. @trigger_error('drupal_render_root() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Render\RendererInterface::renderRoot() instead. See https://www.drupal.org/node/2912696', E_USER_DEPRECATED);
  747. return \Drupal::service('renderer')->renderRoot($elements);
  748. }
  749. /**
  750. * Renders HTML given a structured array tree.
  751. *
  752. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use the
  753. * 'renderer' service instead.
  754. *
  755. * @see \Drupal\Core\Render\RendererInterface::render()
  756. * @see https://www.drupal.org/node/2912696
  757. */
  758. function drupal_render(&$elements, $is_recursive_call = FALSE) {
  759. return \Drupal::service('renderer')->render($elements, $is_recursive_call);
  760. }
  761. /**
  762. * Renders children of an element and concatenates them.
  763. *
  764. * @param array $element
  765. * The structured array whose children shall be rendered.
  766. * @param array $children_keys
  767. * (optional) If the keys of the element's children are already known, they
  768. * can be passed in to save another run of
  769. * \Drupal\Core\Render\Element::children().
  770. *
  771. * @return string|\Drupal\Component\Render\MarkupInterface
  772. * The rendered HTML of all children of the element.
  773. *
  774. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Avoid early
  775. * rendering when possible or loop through the elements and render them as
  776. * they are available.
  777. *
  778. * @see \Drupal\Core\Render\RendererInterface::render()
  779. * @see https://www.drupal.org/node/2912757
  780. */
  781. function drupal_render_children(&$element, $children_keys = NULL) {
  782. if ($children_keys === NULL) {
  783. $children_keys = Element::children($element);
  784. }
  785. $output = '';
  786. foreach ($children_keys as $key) {
  787. if (!empty($element[$key])) {
  788. $output .= \Drupal::service('renderer')->render($element[$key]);
  789. }
  790. }
  791. return Markup::create($output);
  792. }
  793. /**
  794. * Renders an element.
  795. *
  796. * This function renders an element. The top level element is shown with show()
  797. * before rendering, so it will always be rendered even if hide() had been
  798. * previously used on it.
  799. *
  800. * @param $element
  801. * The element to be rendered.
  802. *
  803. * @return
  804. * The rendered element.
  805. *
  806. * @see \Drupal\Core\Render\RendererInterface
  807. * @see show()
  808. * @see hide()
  809. */
  810. function render(&$element) {
  811. if (!$element && $element !== 0) {
  812. return NULL;
  813. }
  814. if (is_array($element)) {
  815. // Early return if this element was pre-rendered (no need to re-render).
  816. if (isset($element['#printed']) && $element['#printed'] == TRUE && isset($element['#markup']) && strlen($element['#markup']) > 0) {
  817. return $element['#markup'];
  818. }
  819. show($element);
  820. return \Drupal::service('renderer')->render($element);
  821. }
  822. else {
  823. // Safe-guard for inappropriate use of render() on flat variables: return
  824. // the variable as-is.
  825. return $element;
  826. }
  827. }
  828. /**
  829. * Hides an element from later rendering.
  830. *
  831. * The first time render() or drupal_render() is called on an element tree,
  832. * as each element in the tree is rendered, it is marked with a #printed flag
  833. * and the rendered children of the element are cached. Subsequent calls to
  834. * render() or drupal_render() will not traverse the child tree of this element
  835. * again: they will just use the cached children. So if you want to hide an
  836. * element, be sure to call hide() on the element before its parent tree is
  837. * rendered for the first time, as it will have no effect on subsequent
  838. * renderings of the parent tree.
  839. *
  840. * @param $element
  841. * The element to be hidden.
  842. *
  843. * @return
  844. * The element.
  845. *
  846. * @see render()
  847. * @see show()
  848. */
  849. function hide(&$element) {
  850. $element['#printed'] = TRUE;
  851. return $element;
  852. }
  853. /**
  854. * Shows a hidden element for later rendering.
  855. *
  856. * You can also use render($element), which shows the element while rendering
  857. * it.
  858. *
  859. * The first time render() or drupal_render() is called on an element tree,
  860. * as each element in the tree is rendered, it is marked with a #printed flag
  861. * and the rendered children of the element are cached. Subsequent calls to
  862. * render() or drupal_render() will not traverse the child tree of this element
  863. * again: they will just use the cached children. So if you want to show an
  864. * element, be sure to call show() on the element before its parent tree is
  865. * rendered for the first time, as it will have no effect on subsequent
  866. * renderings of the parent tree.
  867. *
  868. * @param $element
  869. * The element to be shown.
  870. *
  871. * @return
  872. * The element.
  873. *
  874. * @see render()
  875. * @see hide()
  876. */
  877. function show(&$element) {
  878. $element['#printed'] = FALSE;
  879. return $element;
  880. }
  881. /**
  882. * Retrieves the default properties for the defined element type.
  883. *
  884. * @param $type
  885. * An element type as defined by an element plugin.
  886. *
  887. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  888. * Use \Drupal::service('element_info')->getInfo() instead.
  889. *
  890. * @see https://www.drupal.org/node/2235461
  891. */
  892. function element_info($type) {
  893. return \Drupal::service('element_info')->getInfo($type);
  894. }
  895. /**
  896. * Retrieves a single property for the defined element type.
  897. *
  898. * @param $type
  899. * An element type as defined by an element plugin.
  900. * @param $property_name
  901. * The property within the element type that should be returned.
  902. * @param $default
  903. * (Optional) The value to return if the element type does not specify a
  904. * value for the property. Defaults to NULL.
  905. *
  906. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  907. * Use \Drupal::service('element_info')->getInfoProperty() instead.
  908. *
  909. * @see https://www.drupal.org/node/2235461
  910. */
  911. function element_info_property($type, $property_name, $default = NULL) {
  912. return \Drupal::service('element_info')->getInfoProperty($type, $property_name, $default);
  913. }
  914. /**
  915. * Flushes all persistent caches, resets all variables, and rebuilds all data structures.
  916. *
  917. * At times, it is necessary to re-initialize the entire system to account for
  918. * changed or new code. This function:
  919. * - Clears all persistent caches:
  920. * - The bootstrap cache bin containing base system, module system, and theme
  921. * system information.
  922. * - The common 'default' cache bin containing arbitrary caches.
  923. * - The page cache.
  924. * - The URL alias path cache.
  925. * - Resets all static variables that have been defined via drupal_static().
  926. * - Clears asset (JS/CSS) file caches.
  927. * - Updates the system with latest information about extensions (modules and
  928. * themes).
  929. * - Updates the bootstrap flag for modules implementing bootstrap_hooks().
  930. * - Rebuilds the full database schema information (invoking hook_schema()).
  931. * - Rebuilds data structures of all modules (invoking hook_rebuild()). In
  932. * core this means
  933. * - blocks, node types, date formats and actions are synchronized with the
  934. * database
  935. * - The 'active' status of fields is refreshed.
  936. * - Rebuilds the menu router.
  937. *
  938. * This means the entire system is reset so all caches and static variables are
  939. * effectively empty. After that is guaranteed, information about the currently
  940. * active code is updated, and rebuild operations are successively called in
  941. * order to synchronize the active system according to the current information
  942. * defined in code.
  943. *
  944. * All modules need to ensure that all of their caches are flushed when
  945. * hook_cache_flush() is invoked; any previously known information must no
  946. * longer exist. All following hook_rebuild() operations must be based on fresh
  947. * and current system data. All modules must be able to rely on this contract.
  948. *
  949. * @see \Drupal\Core\Cache\CacheHelper::getBins()
  950. * @see hook_cache_flush()
  951. * @see hook_rebuild()
  952. *
  953. * This function also resets the theme, which means it is not initialized
  954. * anymore and all previously added JavaScript and CSS is gone. Normally, this
  955. * function is called as an end-of-POST-request operation that is followed by a
  956. * redirect, so this effect is not visible. Since the full reset is the whole
  957. * point of this function, callers need to take care for backing up all needed
  958. * variables and properly restoring or re-initializing them on their own. For
  959. * convenience, this function automatically re-initializes the maintenance theme
  960. * if it was initialized before.
  961. *
  962. * @todo Try to clear page/JS/CSS caches last, so cached pages can still be
  963. * served during this possibly long-running operation. (Conflict on bootstrap
  964. * cache though.)
  965. * @todo Add a global lock to ensure that caches are not primed in concurrent
  966. * requests.
  967. */
  968. function drupal_flush_all_caches() {
  969. $module_handler = \Drupal::moduleHandler();
  970. // Flush all persistent caches.
  971. // This is executed based on old/previously known information, which is
  972. // sufficient, since new extensions cannot have any primed caches yet.
  973. $module_handler->invokeAll('cache_flush');
  974. foreach (Cache::getBins() as $service_id => $cache_backend) {
  975. $cache_backend->deleteAll();
  976. }
  977. // Flush asset file caches.
  978. \Drupal::service('asset.css.collection_optimizer')->deleteAll();
  979. \Drupal::service('asset.js.collection_optimizer')->deleteAll();
  980. _drupal_flush_css_js();
  981. // Reset all static caches.
  982. drupal_static_reset();
  983. // Invalidate the container.
  984. \Drupal::service('kernel')->invalidateContainer();
  985. // Wipe the Twig PHP Storage cache.
  986. \Drupal::service('twig')->invalidate();
  987. // Rebuild module and theme data.
  988. $module_data = \Drupal::service('extension.list.module')->reset()->getList();
  989. /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
  990. $theme_handler = \Drupal::service('theme_handler');
  991. $theme_handler->refreshInfo();
  992. // In case the active theme gets requested later in the same request we need
  993. // to reset the theme manager.
  994. \Drupal::theme()->resetActiveTheme();
  995. // Rebuild and reboot a new kernel. A simple DrupalKernel reboot is not
  996. // sufficient, since the list of enabled modules might have been adjusted
  997. // above due to changed code.
  998. $files = [];
  999. $modules = [];
  1000. foreach ($module_data as $name => $extension) {
  1001. if ($extension->status) {
  1002. $files[$name] = $extension;
  1003. $modules[$name] = $extension->weight;
  1004. }
  1005. }
  1006. $modules = module_config_sort($modules);
  1007. \Drupal::service('kernel')->updateModules($modules, $files);
  1008. // New container, new module handler.
  1009. $module_handler = \Drupal::moduleHandler();
  1010. // Ensure that all modules that are currently supposed to be enabled are
  1011. // actually loaded.
  1012. $module_handler->loadAll();
  1013. // Rebuild all information based on new module data.
  1014. $module_handler->invokeAll('rebuild');
  1015. // Clear all plugin caches.
  1016. \Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
  1017. // Rebuild the menu router based on all rebuilt data.
  1018. // Important: This rebuild must happen last, so the menu router is guaranteed
  1019. // to be based on up to date information.
  1020. \Drupal::service('router.builder')->rebuild();
  1021. // Re-initialize the maintenance theme, if the current request attempted to
  1022. // use it. Unlike regular usages of this function, the installer and update
  1023. // scripts need to flush all caches during GET requests/page building.
  1024. if (function_exists('_drupal_maintenance_theme')) {
  1025. \Drupal::theme()->resetActiveTheme();
  1026. drupal_maintenance_theme();
  1027. }
  1028. }
  1029. /**
  1030. * Changes the dummy query string added to all CSS and JavaScript files.
  1031. *
  1032. * Changing the dummy query string appended to CSS and JavaScript files forces
  1033. * all browsers to reload fresh files.
  1034. */
  1035. function _drupal_flush_css_js() {
  1036. // The timestamp is converted to base 36 in order to make it more compact.
  1037. Drupal::state()->set('system.css_js_query_string', base_convert(REQUEST_TIME, 10, 36));
  1038. }
  1039. /**
  1040. * Outputs debug information.
  1041. *
  1042. * The debug information is passed on to trigger_error() after being converted
  1043. * to a string using _drupal_debug_message().
  1044. *
  1045. * @param $data
  1046. * Data to be output.
  1047. * @param $label
  1048. * Label to prefix the data.
  1049. * @param $print_r
  1050. * Flag to switch between print_r() and var_export() for data conversion to
  1051. * string. Set $print_r to FALSE to use var_export() instead of print_r().
  1052. * Passing recursive data structures to var_export() will generate an error.
  1053. */
  1054. function debug($data, $label = NULL, $print_r = TRUE) {
  1055. // Print $data contents to string.
  1056. $string = Html::escape($print_r ? print_r($data, TRUE) : var_export($data, TRUE));
  1057. // Display values with pre-formatting to increase readability.
  1058. $string = '<pre>' . $string . '</pre>';
  1059. trigger_error(trim($label ? "$label: $string" : $string));
  1060. }
  1061. /**
  1062. * Checks whether a version is compatible with a given dependency.
  1063. *
  1064. * @param $v
  1065. * A parsed dependency structure e.g. from ModuleHandler::parseDependency().
  1066. * @param $current_version
  1067. * The version to check against (like 4.2).
  1068. *
  1069. * @return
  1070. * NULL if compatible, otherwise the original dependency version string that
  1071. * caused the incompatibility.
  1072. *
  1073. * @deprecated in drupal:8.7.0 and is removed from drupal:9.0.0. Use
  1074. * \Drupal\Core\Extension\Dependency::isCompatible() instead.
  1075. *
  1076. * @see https://www.drupal.org/node/2756875
  1077. */
  1078. function drupal_check_incompatibility($v, $current_version) {
  1079. @trigger_error(__FUNCTION__ . '() is deprecated. Use \Drupal\Core\Extension\Dependency::isCompatible() instead. See https://www.drupal.org/node/2756875', E_USER_DEPRECATED);
  1080. if (!empty($v['versions'])) {
  1081. foreach ($v['versions'] as $required_version) {
  1082. if ((isset($required_version['op']) && !version_compare($current_version, $required_version['version'], $required_version['op']))) {
  1083. return $v['original_version'];
  1084. }
  1085. }
  1086. }
  1087. }
  1088. /**
  1089. * Returns a string of supported archive extensions.
  1090. *
  1091. * @return string
  1092. * A space-separated string of extensions suitable for use by the file
  1093. * validation system.
  1094. *
  1095. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  1096. * \Drupal\Core\Archiver\ArchiverManager::getExtensions() instead.
  1097. *
  1098. * @see https://www.drupal.org/node/2999951
  1099. */
  1100. function archiver_get_extensions() {
  1101. @trigger_error('archiver_get_extensions() is deprecated in Drupal 8.8.0 and will be removed in Drupal 9.0.0. Use \Drupal\Core\Archiver\ArchiverManager::getExtensions() instead. See https://www.drupal.org/node/2999951', E_USER_DEPRECATED);
  1102. return \Drupal::service('plugin.manager.archiver')->getExtensions();
  1103. }
  1104. /**
  1105. * Creates the appropriate archiver for the specified file.
  1106. *
  1107. * @param string $file
  1108. * The full path of the archive file. Note that stream wrapper paths are
  1109. * supported, but not remote ones.
  1110. *
  1111. * @return \Drupal\Core\Archiver\ArchiverInterface|false
  1112. * A newly created instance of the archiver class appropriate for the
  1113. * specified file, already bound to that file. If no appropriate archiver
  1114. * class was found, will return FALSE.
  1115. *
  1116. * @throws \Exception
  1117. * If a remote stream wrapper path was passed.
  1118. *
  1119. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Instead,
  1120. * get plugin.manager.archiver service from container and call getInstance()
  1121. * method on it. For example $archiver->getInstance(['filepath' => $file]);
  1122. *
  1123. * @see https://www.drupal.org/node/2999951
  1124. */
  1125. function archiver_get_archiver($file) {
  1126. @trigger_error('archiver_get_archiver() is deprecated in Drupal 8.8.0 and will be removed in Drupal 9.0.x. Instead, get plugin.manager.archiver service from container and call getInstance() method on it. For example $archiver->getInstance(["filepath" => $file]); See https://www.drupal.org/node/2999951', E_USER_DEPRECATED);
  1127. // Archivers can only work on local paths
  1128. $filepath = \Drupal::service('file_system')->realpath($file);
  1129. if (!is_file($filepath)) {
  1130. throw new Exception("Archivers can only operate on local files: '$file' not supported");
  1131. }
  1132. return \Drupal::service('plugin.manager.archiver')->getInstance(['filepath' => $filepath]);
  1133. }
  1134. /**
  1135. * Assembles the Drupal Updater registry.
  1136. *
  1137. * An Updater is a class that knows how to update various parts of the Drupal
  1138. * file system, for example to update modules that have newer releases, or to
  1139. * install a new theme.
  1140. *
  1141. * @return array
  1142. * The Drupal Updater class registry.
  1143. *
  1144. * @see \Drupal\Core\Updater\Updater
  1145. * @see hook_updater_info()
  1146. * @see hook_updater_info_alter()
  1147. */
  1148. function drupal_get_updaters() {
  1149. $updaters = &drupal_static(__FUNCTION__);
  1150. if (!isset($updaters)) {
  1151. $updaters = \Drupal::moduleHandler()->invokeAll('updater_info');
  1152. \Drupal::moduleHandler()->alter('updater_info', $updaters);
  1153. uasort($updaters, [SortArray::class, 'sortByWeightElement']);
  1154. }
  1155. return $updaters;
  1156. }
  1157. /**
  1158. * Assembles the Drupal FileTransfer registry.
  1159. *
  1160. * @return
  1161. * The Drupal FileTransfer class registry.
  1162. *
  1163. * @see \Drupal\Core\FileTransfer\FileTransfer
  1164. * @see hook_filetransfer_info()
  1165. * @see hook_filetransfer_info_alter()
  1166. */
  1167. function drupal_get_filetransfer_info() {
  1168. $info = &drupal_static(__FUNCTION__);
  1169. if (!isset($info)) {
  1170. $info = \Drupal::moduleHandler()->invokeAll('filetransfer_info');
  1171. \Drupal::moduleHandler()->alter('filetransfer_info', $info);
  1172. uasort($info, [SortArray::class, 'sortByWeightElement']);
  1173. }
  1174. return $info;
  1175. }