2001-12-01 15:20:48 +00:00
< ? php
2014-04-15 21:14:29 +00:00
/**
* @ file
* Common functions that many Drupal modules will need to reference .
*
* The functions that are critical and need to be available even when serving
* a cached page are instead located in bootstrap . inc .
*/
2014-05-23 19:25:52 +00:00
use Drupal\Component\Utility\Bytes ;
2014-08-29 04:34:31 +00:00
use Drupal\Component\Utility\Html ;
2013-08-05 22:24:38 +00:00
use Drupal\Component\Utility\SortArray ;
2013-03-22 09:36:55 +00:00
use Drupal\Core\Cache\Cache ;
2021-04-01 18:56:13 +00:00
use Drupal\Core\DrupalKernel ;
2015-10-01 23:25:03 +00:00
use Drupal\Core\StringTranslation\TranslatableMarkup ;
2011-12-27 21:17:19 +00:00
2009-09-28 22:22:54 +00:00
/**
* @ defgroup php_wrappers PHP wrapper functions
* @ {
* Functions that are wrappers or custom implementations of PHP functions .
*
* Certain PHP functions should not be used in Drupal . Instead , Drupal ' s
* replacement functions should be used .
*
* For example , for improved or more secure UTF8 - handling , or RFC - compliant
* handling of URLs in Drupal .
*
* For ease of use and memorizing , all these wrapper functions use the same name
* as the original PHP function , but prefixed with " drupal_ " . Beware , however ,
* that not all wrapper functions support the same arguments as the original
* functions .
*
* You should always use these wrapper functions in your code .
*
* Wrong :
* @ code
* $my_substring = substr ( $original_string , 0 , 5 );
* @ endcode
*
* Correct :
* @ code
2018-05-08 16:23:11 +00:00
* $my_substring = mb_substr ( $original_string , 0 , 5 );
2009-09-28 22:22:54 +00:00
* @ endcode
*
2010-12-15 03:00:40 +00:00
* @ }
2009-09-28 22:22:54 +00:00
*/
2005-05-07 01:48:06 +00:00
/**
* Return status for saving which involved creating a new item .
*/
2011-11-29 09:56:53 +00:00
const SAVED_NEW = 1 ;
2005-05-07 01:48:06 +00:00
/**
* Return status for saving which involved an update to an existing item .
*/
2011-11-29 09:56:53 +00:00
const SAVED_UPDATED = 2 ;
2005-05-07 01:48:06 +00:00
/**
* Return status for saving which deleted an existing item .
*/
2011-11-29 09:56:53 +00:00
const SAVED_DELETED = 3 ;
2005-05-07 01:48:06 +00:00
2009-07-30 19:57:10 +00:00
/**
2013-04-04 02:14:52 +00:00
* The default aggregation group for CSS files added to the page .
2009-07-30 19:57:10 +00:00
*/
2013-04-04 02:14:52 +00:00
const CSS_AGGREGATE_DEFAULT = 0 ;
2009-07-30 19:57:10 +00:00
/**
2013-04-04 02:14:52 +00:00
* The default aggregation group for theme CSS files added to the page .
2009-07-30 19:57:10 +00:00
*/
2013-04-04 02:14:52 +00:00
const CSS_AGGREGATE_THEME = 100 ;
/**
* The default weight for CSS rules that style HTML elements ( " base " styles ) .
*/
const CSS_BASE = - 200 ;
/**
* The default weight for CSS rules that layout a page .
*/
const CSS_LAYOUT = - 100 ;
/**
2014-03-04 18:04:01 +00:00
* The default weight for CSS rules that style design components ( and their associated states and themes . )
2013-04-04 02:14:52 +00:00
*/
const CSS_COMPONENT = 0 ;
/**
* The default weight for CSS rules that style states and are not included with components .
*/
const CSS_STATE = 100 ;
/**
2014-03-04 18:04:01 +00:00
* The default weight for CSS rules that style themes and are not included with components .
2013-04-04 02:14:52 +00:00
*/
2014-03-04 18:04:01 +00:00
const CSS_THEME = 200 ;
2009-07-30 19:57:10 +00:00
2012-09-24 13:12:09 +00:00
/**
* The default group for JavaScript settings added to the page .
*/
const JS_SETTING = - 200 ;
2008-11-10 05:23:01 +00:00
/**
2011-12-05 12:52:27 +00:00
* The default group for JavaScript and jQuery libraries added to the page .
2008-11-10 05:23:01 +00:00
*/
2011-11-29 09:56:53 +00:00
const JS_LIBRARY = - 100 ;
2008-11-10 05:23:01 +00:00
/**
2010-10-05 19:59:10 +00:00
* The default group for module JavaScript code added to the page .
2008-11-10 05:23:01 +00:00
*/
2011-11-29 09:56:53 +00:00
const JS_DEFAULT = 0 ;
2008-11-10 05:23:01 +00:00
/**
2010-10-05 19:59:10 +00:00
* The default group for theme JavaScript code added to the page .
2008-11-10 05:23:01 +00:00
*/
2011-11-29 09:56:53 +00:00
const JS_THEME = 100 ;
2008-11-10 05:23:01 +00:00
2004-02-08 17:12:44 +00:00
/**
2004-09-09 05:51:08 +00:00
* @ defgroup format Formatting
2004-02-08 17:12:44 +00:00
* @ {
2004-09-09 05:51:08 +00:00
* Functions to format numbers , strings , dates , etc .
2004-02-08 17:12:44 +00:00
*/
/**
2011-12-05 12:52:27 +00:00
* Generates a string representation for the given byte count .
2004-02-08 17:12:44 +00:00
*
2004-07-13 07:21:14 +00:00
* @ param $size
2007-10-08 14:08:19 +00:00
* A size in bytes .
2007-05-29 14:37:49 +00:00
* @ param $langcode
2007-10-08 14:08:19 +00:00
* Optional language code to translate to a language other than what is used
* to display the page .
2011-12-05 12:52:27 +00:00
*
2015-10-01 23:25:03 +00:00
* @ return \Drupal\Core\StringTranslation\TranslatableMarkup
2004-07-13 07:21:14 +00:00
* A translated string representation of the size .
2004-02-08 17:12:44 +00:00
*/
2007-05-29 14:37:49 +00:00
function format_size ( $size , $langcode = NULL ) {
2018-11-19 09:55:34 +00:00
$absolute_size = abs ( $size );
if ( $absolute_size < Bytes :: KILOBYTE ) {
2017-03-04 01:20:24 +00:00
return \Drupal :: translation () -> formatPlural ( $size , '1 byte' , '@count bytes' , [], [ 'langcode' => $langcode ]);
2001-12-01 15:20:48 +00:00
}
2018-11-19 09:55:34 +00:00
// Create a multiplier to preserve the sign of $size.
$sign = $absolute_size / $size ;
foreach ([ 'KB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ] as $unit ) {
$absolute_size /= Bytes :: KILOBYTE ;
$rounded_size = round ( $absolute_size , 2 );
if ( $rounded_size < Bytes :: KILOBYTE ) {
break ;
Issue #2557113 by stefan.r, alexpott, dawehner, joelpittet, cilefen, lauriii, plach, catch, chx, xjm: Make t() return a TranslationWrapper object to remove reliance on a static, unpredictable safe list
2015-09-19 04:33:21 +00:00
}
2001-12-01 15:20:48 +00:00
}
2018-11-19 09:55:34 +00:00
$args = [ '@size' => $rounded_size * $sign ];
$options = [ 'langcode' => $langcode ];
switch ( $unit ) {
case 'KB' :
return new TranslatableMarkup ( '@size KB' , $args , $options );
case 'MB' :
return new TranslatableMarkup ( '@size MB' , $args , $options );
case 'GB' :
return new TranslatableMarkup ( '@size GB' , $args , $options );
case 'TB' :
return new TranslatableMarkup ( '@size TB' , $args , $options );
case 'PB' :
return new TranslatableMarkup ( '@size PB' , $args , $options );
case 'EB' :
return new TranslatableMarkup ( '@size EB' , $args , $options );
case 'ZB' :
return new TranslatableMarkup ( '@size ZB' , $args , $options );
case 'YB' :
return new TranslatableMarkup ( '@size YB' , $args , $options );
}
2001-12-01 15:20:48 +00:00
}
2004-09-09 05:51:08 +00:00
/**
* @ } End of " defgroup format " .
*/
2001-12-01 15:20:48 +00:00
2006-02-02 12:44:57 +00:00
/**
2011-12-05 12:52:27 +00:00
* Returns the base URL path ( i . e . , directory ) of the Drupal installation .
2009-04-26 07:50:51 +00:00
*
2018-05-11 09:46:46 +00:00
* Function base_path () adds a " / " to the beginning and end of the returned path
* if the path is not empty . At the very least , this will return " / " .
2009-04-26 07:50:51 +00:00
*
2009-05-01 16:45:52 +00:00
* Examples :
* - http :// example . com returns " / " because the path is empty .
* - http :// example . com / drupal / folder returns " /drupal/folder/ " .
2006-02-02 12:44:57 +00:00
*/
function base_path () {
2012-04-01 01:17:44 +00:00
return $GLOBALS [ 'base_path' ];
2006-02-02 12:44:57 +00:00
}
2005-05-24 06:00:22 +00:00
/**
2015-01-21 15:21:06 +00:00
* Constructs an array of the defaults that are used for JavaScript assets .
2008-11-23 16:00:08 +00:00
*
* @ param $data
2015-01-21 15:21:06 +00:00
* ( optional ) The default data parameter for the JavaScript asset array .
2011-12-05 12:52:27 +00:00
*
2015-01-21 15:21:06 +00:00
* @ see hook_js_alter ()
2008-11-23 16:00:08 +00:00
*/
function drupal_js_defaults ( $data = NULL ) {
2017-03-04 01:20:24 +00:00
return [
2008-11-23 16:00:08 +00:00
'type' => 'file' ,
2010-10-05 19:59:10 +00:00
'group' => JS_DEFAULT ,
'weight' => 0 ,
2008-11-23 16:00:08 +00:00
'scope' => 'header' ,
'cache' => TRUE ,
2010-10-05 19:59:10 +00:00
'preprocess' => TRUE ,
2017-03-04 01:20:24 +00:00
'attributes' => [],
2010-03-09 03:39:44 +00:00
'version' => NULL ,
2008-11-23 16:00:08 +00:00
'data' => $data ,
2017-03-04 01:20:24 +00:00
'browsers' => [],
];
2008-11-23 16:00:08 +00:00
}
2007-11-14 09:50:00 +00:00
/**
2013-12-20 12:05:47 +00:00
* Assists in attaching the tableDrag JavaScript behavior to a themed table .
2007-11-14 09:50:00 +00:00
*
* Draggable tables should be used wherever an outline or list of sortable items
* needs to be arranged by an end - user . Draggable tables are very flexible and
* can manipulate the value of form elements placed within individual columns .
*
2011-12-05 12:52:27 +00:00
* To set up a table to use drag and drop in place of weight select - lists or in
* place of a form that contains parent relationships , the form must be themed
2014-12-11 10:35:52 +00:00
* into a table . The table must have an ID attribute set and it
* may be set as follows :
2007-11-14 09:50:00 +00:00
* @ code
2014-02-18 10:54:10 +00:00
* $table = array (
* '#type' => 'table' ,
* '#header' => $header ,
* '#rows' => $rows ,
* '#attributes' => array (
* 'id' => 'my-module-table' ,
* ),
* );
2018-04-30 16:36:16 +00:00
* return \Drupal :: service ( 'renderer' ) -> render ( $table );
2007-11-14 09:50:00 +00:00
* @ endcode
*
* In the theme function for the form , a special class must be added to each
* form element within the same column , " grouping " them together .
*
* In a situation where a single weight column is being sorted in the table , the
* classes could be added like this ( in the theme function ) :
* @ code
2009-08-22 14:34:23 +00:00
* $form [ 'my_elements' ][ $delta ][ 'weight' ][ '#attributes' ][ 'class' ] = array ( 'my-elements-weight' );
2007-11-14 09:50:00 +00:00
* @ endcode
*
2011-12-05 12:52:27 +00:00
* Each row of the table must also have a class of " draggable " in order to
* enable the drag handles :
2007-11-20 20:13:04 +00:00
* @ code
* $row = array ( ... );
* $rows [] = array (
* 'data' => $row ,
2009-08-22 14:34:23 +00:00
* 'class' => array ( 'draggable' ),
2007-11-20 20:13:04 +00:00
* );
* @ endcode
2007-11-23 13:34:55 +00:00
*
2007-12-19 10:58:35 +00:00
* When tree relationships are present , the two additional classes
* 'tabledrag-leaf' and 'tabledrag-root' can be used to refine the behavior :
* - Rows with the 'tabledrag-leaf' class cannot have child rows .
* - Rows with the 'tabledrag-root' class cannot be nested under a parent row .
*
2013-12-20 12:05:47 +00:00
* Calling drupal_attach_tabledrag () would then be written as such :
2007-11-14 09:50:00 +00:00
* @ code
2013-12-20 12:05:47 +00:00
* drupal_attach_tabledrag ( 'my-module-table' , array (
* 'action' => 'order' ,
* 'relationship' => 'sibling' ,
* 'group' => 'my-elements-weight' ,
* );
2007-11-14 09:50:00 +00:00
* @ endcode
*
* In a more complex case where there are several groups in one column ( such as
2011-12-05 12:52:27 +00:00
* the block regions on the admin / structure / block page ), a separate subgroup
* class must also be added to differentiate the groups .
2007-11-14 09:50:00 +00:00
* @ code
2009-08-22 14:34:23 +00:00
* $form [ 'my_elements' ][ $region ][ $delta ][ 'weight' ][ '#attributes' ][ 'class' ] = array ( 'my-elements-weight' , 'my-elements-weight-' . $region );
2007-11-14 09:50:00 +00:00
* @ endcode
*
2013-12-20 12:05:47 +00:00
* The 'group' option is still 'my-element-weight' , and the additional
* 'subgroup' option will be passed in as 'my-elements-weight-' . $region . This
* also means that you ' ll need to call drupal_attach_tabledrag () once for every
* region added .
2007-11-14 09:50:00 +00:00
*
* @ code
* foreach ( $regions as $region ) {
2013-12-20 12:05:47 +00:00
* drupal_attach_tabledrag ( 'my-module-table' , array (
* 'action' => 'order' ,
2015-07-04 14:19:14 +00:00
* 'relationship' => 'sibling' ,
2013-12-20 12:05:47 +00:00
* 'group' => 'my-elements-weight' ,
2015-07-04 14:19:14 +00:00
* 'subgroup' => 'my-elements-weight-' . $region ,
2013-12-20 12:05:47 +00:00
* ));
2007-11-14 09:50:00 +00:00
* }
* @ endcode
*
* In a situation where tree relationships are present , adding multiple
* subgroups is not necessary , because the table will contain indentations that
2011-12-05 12:52:27 +00:00
* provide enough information about the sibling and parent relationships . See
2014-11-04 15:19:27 +00:00
* MenuForm :: BuildOverviewForm for an example creating a table
* containing parent relationships .
2011-12-05 12:52:27 +00:00
*
2013-12-20 12:05:47 +00:00
* @ param $element
* A form element to attach the tableDrag behavior to .
* @ param array $options
* These options are used to generate JavaScript settings necessary to
* configure the tableDrag behavior appropriately for this particular table .
* An associative array containing the following keys :
* - 'table_id' : String containing the target table ' s id attribute .
* If the table does not have an id , one will need to be set ,
* such as < table id = " my-module-table " >.
* - 'action' : String describing the action to be done on the form item .
* Either 'match' 'depth' , or 'order' :
* - 'match' is typically used for parent relationships .
* - 'order' is typically used to set weights on other form elements with
* the same group .
* - 'depth' updates the target element with the current indentation .
* - 'relationship' : String describing where the " action " option
* should be performed . Either 'parent' , 'sibling' , 'group' , or 'self' :
* - 'parent' will only look for fields up the tree .
* - 'sibling' will look for fields in the same group in rows above and
* below it .
* - 'self' affects the dragged row itself .
* - 'group' affects the dragged row , plus any children below it ( the entire
* dragged group ) .
* - 'group' : A class name applied on all related form elements for this action .
* - 'subgroup' : ( optional ) If the group has several subgroups within it , this
* string should contain the class name identifying fields in the same
* subgroup .
* - 'source' : ( optional ) If the $action is 'match' , this string should contain
* the classname identifying what field will be used as the source value
* when matching the value in $subgroup .
* - 'hidden' : ( optional ) The column containing the field elements may be
* entirely hidden from view dynamically when the JavaScript is loaded . Set
* to FALSE if the column should not be hidden .
* - 'limit' : ( optional ) Limit the maximum amount of parenting in this table .
*
2014-11-04 15:19:27 +00:00
* @ see MenuForm :: BuildOverviewForm ()
2007-11-14 09:50:00 +00:00
*/
2013-12-20 12:05:47 +00:00
function drupal_attach_tabledrag ( & $element , array $options ) {
// Add default values to elements.
2017-03-04 01:20:24 +00:00
$options = $options + [
2013-12-20 12:05:47 +00:00
'subgroup' => NULL ,
'source' => NULL ,
'hidden' => TRUE ,
2018-05-11 09:40:33 +00:00
'limit' => 0 ,
2017-03-04 01:20:24 +00:00
];
2013-01-18 18:00:29 +00:00
2013-12-20 12:05:47 +00:00
$group = $options [ 'group' ];
$tabledrag_id = & drupal_static ( __FUNCTION__ );
$tabledrag_id = ( ! isset ( $tabledrag_id )) ? 0 : $tabledrag_id + 1 ;
2007-11-14 09:50:00 +00:00
// If a subgroup or source isn't set, assume it is the same as the group.
2013-12-20 12:05:47 +00:00
$target = isset ( $options [ 'subgroup' ]) ? $options [ 'subgroup' ] : $group ;
$source = isset ( $options [ 'source' ]) ? $options [ 'source' ] : $target ;
2017-03-04 01:20:24 +00:00
$element [ '#attached' ][ 'drupalSettings' ][ 'tableDrag' ][ $options [ 'table_id' ]][ $group ][ $tabledrag_id ] = [
2007-11-14 09:50:00 +00:00
'target' => $target ,
'source' => $source ,
2013-12-20 12:05:47 +00:00
'relationship' => $options [ 'relationship' ],
'action' => $options [ 'action' ],
'hidden' => $options [ 'hidden' ],
'limit' => $options [ 'limit' ],
2017-03-04 01:20:24 +00:00
];
2013-12-20 12:05:47 +00:00
2014-03-09 19:59:45 +00:00
$element [ '#attached' ][ 'library' ][] = 'core/drupal.tabledrag' ;
2007-11-14 09:50:00 +00:00
}
2009-06-18 21:19:02 +00:00
/**
2011-12-05 12:52:27 +00:00
* Renders an element .
2009-06-18 21:19:02 +00:00
*
2015-04-15 16:56:41 +00:00
* This function renders an element . The top level element is shown with show ()
* before rendering , so it will always be rendered even if hide () had been
* previously used on it .
2009-06-18 21:19:02 +00:00
*
2011-01-12 23:15:26 +00:00
* @ param $element
* The element to be rendered .
*
* @ return
* The rendered element .
2009-06-18 21:19:02 +00:00
*
2015-04-15 16:56:41 +00:00
* @ see \Drupal\Core\Render\RendererInterface
2009-06-18 21:19:02 +00:00
* @ see show ()
* @ see hide ()
*/
function render ( & $element ) {
2012-11-03 17:36:10 +00:00
if ( ! $element && $element !== 0 ) {
return NULL ;
}
2009-06-18 21:19:02 +00:00
if ( is_array ( $element )) {
2014-05-02 21:43:59 +00:00
// Early return if this element was pre-rendered (no need to re-render).
if ( isset ( $element [ '#printed' ]) && $element [ '#printed' ] == TRUE && isset ( $element [ '#markup' ]) && strlen ( $element [ '#markup' ]) > 0 ) {
return $element [ '#markup' ];
}
2009-06-18 21:19:02 +00:00
show ( $element );
2015-04-15 16:56:41 +00:00
return \Drupal :: service ( 'renderer' ) -> render ( $element );
2009-06-18 21:19:02 +00:00
}
else {
2009-07-13 21:09:54 +00:00
// Safe-guard for inappropriate use of render() on flat variables: return
// the variable as-is.
return $element ;
2009-06-18 21:19:02 +00:00
}
}
/**
2011-12-05 12:52:27 +00:00
* Hides an element from later rendering .
2009-06-18 21:19:02 +00:00
*
2011-01-12 23:15:26 +00:00
* The first time render () or drupal_render () is called on an element tree ,
* as each element in the tree is rendered , it is marked with a #printed flag
* and the rendered children of the element are cached . Subsequent calls to
* render () or drupal_render () will not traverse the child tree of this element
* again : they will just use the cached children . So if you want to hide an
* element , be sure to call hide () on the element before its parent tree is
* rendered for the first time , as it will have no effect on subsequent
* renderings of the parent tree .
*
* @ param $element
* The element to be hidden .
*
* @ return
* The element .
*
2009-06-18 21:19:02 +00:00
* @ see render ()
* @ see show ()
*/
function hide ( & $element ) {
$element [ '#printed' ] = TRUE ;
return $element ;
}
/**
2011-12-05 12:52:27 +00:00
* Shows a hidden element for later rendering .
2011-01-12 23:15:26 +00:00
*
* You can also use render ( $element ), which shows the element while rendering
* it .
*
* The first time render () or drupal_render () is called on an element tree ,
* as each element in the tree is rendered , it is marked with a #printed flag
* and the rendered children of the element are cached . Subsequent calls to
* render () or drupal_render () will not traverse the child tree of this element
* again : they will just use the cached children . So if you want to show an
* element , be sure to call show () on the element before its parent tree is
* rendered for the first time , as it will have no effect on subsequent
* renderings of the parent tree .
2009-06-18 21:19:02 +00:00
*
2011-01-12 23:15:26 +00:00
* @ param $element
* The element to be shown .
*
* @ return
* The element .
2009-06-18 21:19:02 +00:00
*
* @ see render ()
* @ see hide ()
*/
function show ( & $element ) {
$element [ '#printed' ] = FALSE ;
return $element ;
}
2007-11-26 16:25:14 +00:00
/**
2021-04-01 18:56:13 +00:00
* Rebuilds the container , flushes all persistent caches , resets all variables , and rebuilds all data structures .
2012-05-03 15:09:39 +00:00
*
* At times , it is necessary to re - initialize the entire system to account for
* changed or new code . This function :
2021-04-01 18:56:13 +00:00
* - Rebuilds the container if $kernel is not passed in .
2013-03-22 09:36:55 +00:00
* - Clears all persistent caches :
2012-05-03 15:09:39 +00:00
* - The bootstrap cache bin containing base system , module system , and theme
* system information .
2014-03-31 09:49:28 +00:00
* - The common 'default' cache bin containing arbitrary caches .
2012-05-03 15:09:39 +00:00
* - The page cache .
* - The URL alias path cache .
* - Resets all static variables that have been defined via drupal_static () .
* - Clears asset ( JS / CSS ) file caches .
* - Updates the system with latest information about extensions ( modules and
* themes ) .
* - Updates the bootstrap flag for modules implementing bootstrap_hooks () .
* - Rebuilds the full database schema information ( invoking hook_schema ()) .
* - Rebuilds data structures of all modules ( invoking hook_rebuild ()) . In
* core this means
* - blocks , node types , date formats and actions are synchronized with the
* database
* - The 'active' status of fields is refreshed .
* - Rebuilds the menu router .
*
2021-04-01 18:56:13 +00:00
* It ' s discouraged to call this during a regular page request .
* If you call this function in tests , every code afterwards should use the new
* container .
*
2012-05-03 15:09:39 +00:00
* This means the entire system is reset so all caches and static variables are
* effectively empty . After that is guaranteed , information about the currently
* active code is updated , and rebuild operations are successively called in
* order to synchronize the active system according to the current information
* defined in code .
*
* All modules need to ensure that all of their caches are flushed when
* hook_cache_flush () is invoked ; any previously known information must no
* longer exist . All following hook_rebuild () operations must be based on fresh
* and current system data . All modules must be able to rely on this contract .
*
2013-03-22 09:36:55 +00:00
* @ see \Drupal\Core\Cache\CacheHelper :: getBins ()
2012-05-03 15:09:39 +00:00
* @ see hook_cache_flush ()
* @ see hook_rebuild ()
*
* This function also resets the theme , which means it is not initialized
* anymore and all previously added JavaScript and CSS is gone . Normally , this
* function is called as an end - of - POST - request operation that is followed by a
* redirect , so this effect is not visible . Since the full reset is the whole
* point of this function , callers need to take care for backing up all needed
* variables and properly restoring or re - initializing them on their own . For
* convenience , this function automatically re - initializes the maintenance theme
* if it was initialized before .
*
* @ todo Try to clear page / JS / CSS caches last , so cached pages can still be
* served during this possibly long - running operation . ( Conflict on bootstrap
* cache though . )
* @ todo Add a global lock to ensure that caches are not primed in concurrent
* requests .
2021-04-01 18:56:13 +00:00
*
* @ param \Drupal\Core\DrupalKernel | array $kernel
* ( optional ) The Drupal Kernel . It is the caller ' s responsibility to rebuild
* the container if this is passed in . Sometimes drupal_flush_all_caches is
* used as a batch operation so $kernel will be an array , in this instance it
* will be treated as if it it NULL .
2007-11-26 16:25:14 +00:00
*/
2021-04-01 18:56:13 +00:00
function drupal_flush_all_caches ( $kernel = NULL ) {
// This is executed based on old/previously known information if $kernel is
// not passed in, which is sufficient, since new extensions cannot have any
// primed caches yet.
2013-09-16 03:58:06 +00:00
$module_handler = \Drupal :: moduleHandler ();
2012-05-03 15:09:39 +00:00
// Flush all persistent caches.
2013-05-19 19:32:09 +00:00
$module_handler -> invokeAll ( 'cache_flush' );
2013-03-22 09:36:55 +00:00
foreach ( Cache :: getBins () as $service_id => $cache_backend ) {
2014-03-26 13:19:28 +00:00
$cache_backend -> deleteAll ();
2012-05-03 15:09:39 +00:00
}
2008-01-07 19:43:29 +00:00
2012-05-03 15:09:39 +00:00
// Flush asset file caches.
2014-08-08 16:52:12 +00:00
\Drupal :: service ( 'asset.css.collection_optimizer' ) -> deleteAll ();
\Drupal :: service ( 'asset.js.collection_optimizer' ) -> deleteAll ();
2012-05-03 15:09:39 +00:00
_drupal_flush_css_js ();
2010-04-29 05:33:43 +00:00
2012-05-03 15:09:39 +00:00
// Reset all static caches.
drupal_static_reset ();
2012-01-18 04:02:34 +00:00
2015-07-07 16:35:44 +00:00
// Wipe the Twig PHP Storage cache.
Issue #2752961 by dawehner, Wim Leers, Jo Fitzgerald, chr.fritsch, bradjones1, anavarre, greg.1.anderson, Berdir, moshe weitzman, alexpott, bkosborne, Crell, msonnabaum, joelpittet, catch, deviantintegral, lauriii, cilefen, effulgentsia, xjm: No reliable method exists for clearing the Twig cache
2018-05-05 00:28:31 +00:00
\Drupal :: service ( 'twig' ) -> invalidate ();
2013-05-19 19:32:09 +00:00
2021-04-01 18:56:13 +00:00
// Rebuild theme data that is stored in state.
\Drupal :: service ( 'theme_handler' ) -> refreshInfo ();
2014-09-05 10:11:16 +00:00
// In case the active theme gets requested later in the same request we need
// to reset the theme manager.
\Drupal :: theme () -> resetActiveTheme ();
2021-04-01 18:56:13 +00:00
if ( ! $kernel instanceof DrupalKernel ) {
$kernel = \Drupal :: service ( 'kernel' );
$kernel -> invalidateContainer ();
$kernel -> rebuildContainer ();
2013-05-19 19:32:09 +00:00
}
2021-04-01 18:56:13 +00:00
// Rebuild module data that is stored in state.
\Drupal :: service ( 'extension.list.module' ) -> reset ();
2010-07-29 02:00:27 +00:00
2012-05-03 15:09:39 +00:00
// Rebuild all information based on new module data.
2021-04-01 18:56:13 +00:00
\Drupal :: moduleHandler () -> invokeAll ( 'rebuild' );
2012-05-03 15:09:39 +00:00
2014-10-05 09:31:26 +00:00
// Clear all plugin caches.
\Drupal :: service ( 'plugin.cache_clearer' ) -> clearCachedDefinitions ();
2012-05-03 15:09:39 +00:00
// Rebuild the menu router based on all rebuilt data.
// Important: This rebuild must happen last, so the menu router is guaranteed
// to be based on up to date information.
2013-09-16 03:58:06 +00:00
\Drupal :: service ( 'router.builder' ) -> rebuild ();
2012-05-03 15:09:39 +00:00
// Re-initialize the maintenance theme, if the current request attempted to
// use it. Unlike regular usages of this function, the installer and update
// scripts need to flush all caches during GET requests/page building.
if ( function_exists ( '_drupal_maintenance_theme' )) {
2014-08-21 16:53:03 +00:00
\Drupal :: theme () -> resetActiveTheme ();
2012-05-03 15:09:39 +00:00
drupal_maintenance_theme ();
}
2007-11-26 16:25:14 +00:00
}
2008-01-07 19:43:29 +00:00
/**
2011-12-05 12:52:27 +00:00
* Changes the dummy query string added to all CSS and JavaScript files .
2008-01-07 19:43:29 +00:00
*
2011-12-05 12:52:27 +00:00
* Changing the dummy query string appended to CSS and JavaScript files forces
* all browsers to reload fresh files .
2008-01-07 19:43:29 +00:00
*/
function _drupal_flush_css_js () {
2010-03-23 21:44:10 +00:00
// The timestamp is converted to base 36 in order to make it more compact.
2013-09-27 12:20:32 +00:00
Drupal :: state () -> set ( 'system.css_js_query_string' , base_convert ( REQUEST_TIME , 10 , 36 ));
2008-01-07 19:43:29 +00:00
}
2009-08-13 03:03:04 +00:00
2009-08-15 06:20:20 +00:00
/**
2011-12-05 12:52:27 +00:00
* Outputs debug information .
2009-08-15 06:20:20 +00:00
*
* The debug information is passed on to trigger_error () after being converted
Issue #2002514 by dawehner, mondrake, damiankloip, RobLoach, ParisLiakos, joachim, Krzysztof Domański, gaurav.kapoor, chx, alexpott, tim.plunkett, Mile23, daffie: Deprecate debug(); remove references to _drupal_debug_message()
2021-02-15 09:51:17 +00:00
* to a string using print_r () or var_export () .
2009-08-15 06:20:20 +00:00
*
* @ param $data
* Data to be output .
* @ param $label
* Label to prefix the data .
* @ param $print_r
* Flag to switch between print_r () and var_export () for data conversion to
2015-02-18 11:45:45 +00:00
* string . Set $print_r to FALSE to use var_export () instead of print_r () .
* Passing recursive data structures to var_export () will generate an error .
Issue #2002514 by dawehner, mondrake, damiankloip, RobLoach, ParisLiakos, joachim, Krzysztof Domański, gaurav.kapoor, chx, alexpott, tim.plunkett, Mile23, daffie: Deprecate debug(); remove references to _drupal_debug_message()
2021-02-15 09:51:17 +00:00
*
* @ deprecated in drupal : 9.2 . 0 and is removed from drupal : 10.0 . 0. Use dump ()
* instead .
*
* @ see https :// www . drupal . org / node / 3192283
2009-08-15 06:20:20 +00:00
*/
2015-02-18 11:45:45 +00:00
function debug ( $data , $label = NULL , $print_r = TRUE ) {
Issue #2002514 by dawehner, mondrake, damiankloip, RobLoach, ParisLiakos, joachim, Krzysztof Domański, gaurav.kapoor, chx, alexpott, tim.plunkett, Mile23, daffie: Deprecate debug(); remove references to _drupal_debug_message()
2021-02-15 09:51:17 +00:00
@ trigger_error ( 'debug() is deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use dump() instead. See https://www.drupal.org/node/3192283' , E_USER_DEPRECATED );
2009-08-15 06:20:20 +00:00
// Print $data contents to string.
2015-08-26 10:34:29 +00:00
$string = Html :: escape ( $print_r ? print_r ( $data , TRUE ) : var_export ( $data , TRUE ));
2010-10-16 00:00:17 +00:00
// Display values with pre-formatting to increase readability.
$string = '<pre>' . $string . '</pre>' ;
2009-08-15 06:20:20 +00:00
trigger_error ( trim ( $label ? " $label : $string " : $string ));
}
2009-10-15 21:19:31 +00:00
/**
2011-12-05 12:52:27 +00:00
* Assembles the Drupal Updater registry .
2009-10-15 21:19:31 +00:00
*
* An Updater is a class that knows how to update various parts of the Drupal
* file system , for example to update modules that have newer releases , or to
* install a new theme .
*
2012-03-11 00:23:05 +00:00
* @ return array
2011-12-05 12:52:27 +00:00
* The Drupal Updater class registry .
2009-10-15 21:19:31 +00:00
*
2013-10-03 11:26:25 +00:00
* @ see \Drupal\Core\Updater\Updater
2009-10-15 21:19:31 +00:00
* @ see hook_updater_info ()
* @ see hook_updater_info_alter ()
*/
function drupal_get_updaters () {
$updaters = & drupal_static ( __FUNCTION__ );
if ( ! isset ( $updaters )) {
2013-09-16 03:58:06 +00:00
$updaters = \Drupal :: moduleHandler () -> invokeAll ( 'updater_info' );
2014-02-24 10:10:52 +00:00
\Drupal :: moduleHandler () -> alter ( 'updater_info' , $updaters );
2017-03-04 01:20:24 +00:00
uasort ( $updaters , [ SortArray :: class , 'sortByWeightElement' ]);
2009-10-15 21:19:31 +00:00
}
return $updaters ;
}
2010-12-01 00:23:36 +00:00
/**
2011-12-05 12:52:27 +00:00
* Assembles the Drupal FileTransfer registry .
2010-12-01 00:23:36 +00:00
*
* @ return
2011-12-05 12:52:27 +00:00
* The Drupal FileTransfer class registry .
2010-12-01 00:23:36 +00:00
*
2013-10-03 11:26:25 +00:00
* @ see \Drupal\Core\FileTransfer\FileTransfer
2010-12-01 00:23:36 +00:00
* @ see hook_filetransfer_info ()
* @ see hook_filetransfer_info_alter ()
*/
function drupal_get_filetransfer_info () {
$info = & drupal_static ( __FUNCTION__ );
if ( ! isset ( $info )) {
2013-09-16 03:58:06 +00:00
$info = \Drupal :: moduleHandler () -> invokeAll ( 'filetransfer_info' );
2014-02-24 10:10:52 +00:00
\Drupal :: moduleHandler () -> alter ( 'filetransfer_info' , $info );
2017-03-04 01:20:24 +00:00
uasort ( $info , [ SortArray :: class , 'sortByWeightElement' ]);
2010-12-01 00:23:36 +00:00
}
return $info ;
}