2002-01-13 13:18:48 +00:00
< ? php
2004-07-22 16:06:54 +00:00
/**
* @ file
* API for loading and interacting with Drupal modules .
*/
2014-02-28 11:51:38 +00:00
use Drupal\Core\Extension\ExtensionDiscovery ;
2012-04-09 18:02:59 +00:00
2009-11-08 09:29:07 +00:00
/**
2014-09-19 09:25:26 +00:00
* Builds a list of installed themes .
2009-11-08 09:29:07 +00:00
*
* @ param $type
2009-11-09 19:00:03 +00:00
* The type of list to return :
2014-09-19 09:25:26 +00:00
* - theme : All installed themes .
2009-11-08 09:29:07 +00:00
*
Issue #2659940 by almaudoh, alexpott, phenaproxima, Jo Fitzgerald, markcarver, oriol_e9g, dawehner, dsnopek, jibran, larowlan: Extension System, Part III: ThemeExtensionList and ThemeEngineExtensionList
2018-11-29 12:15:17 +00:00
* @ return array
2014-04-22 18:37:12 +00:00
* An associative array of themes , keyed by name .
2012-08-01 17:40:47 +00:00
* For $type 'theme' , the array values are objects representing the
* respective database row , with the 'info' property already unserialized .
2009-11-08 09:29:07 +00:00
*
Issue #2659940 by almaudoh, alexpott, phenaproxima, Jo Fitzgerald, markcarver, oriol_e9g, dawehner, dsnopek, jibran, larowlan: Extension System, Part III: ThemeExtensionList and ThemeEngineExtensionList
2018-11-29 12:15:17 +00:00
* @ deprecated in Drupal 8.7 . 0 and will be removed before Drupal 9.0 . 0. Use
* \Drupal :: service ( 'theme_handler' ) -> listInfo () instead .
*
* @ see https :// www . drupal . org / node / 2709919
2015-01-13 10:17:01 +00:00
* @ see \Drupal\Core\Extension\ThemeHandler :: listInfo ()
2009-11-08 09:29:07 +00:00
*/
function system_list ( $type ) {
Issue #2659940 by almaudoh, alexpott, phenaproxima, Jo Fitzgerald, markcarver, oriol_e9g, dawehner, dsnopek, jibran, larowlan: Extension System, Part III: ThemeExtensionList and ThemeEngineExtensionList
2018-11-29 12:15:17 +00:00
@ trigger_error ( 'system_list() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal::service(\'theme_handler\')->listInfo() instead. See https://www.drupal.org/node/2709919' , E_USER_DEPRECATED );
$lists = [
'theme' => \Drupal :: service ( 'theme_handler' ) -> listInfo (),
'filepaths' => [],
];
foreach ( $lists [ 'theme' ] as $name => $theme ) {
$lists [ 'filepaths' ][] = [
'type' => 'theme' ,
'name' => $name ,
'filepath' => $theme -> getPathname (),
2017-03-04 01:20:24 +00:00
];
2009-11-08 09:29:07 +00:00
}
return $lists [ $type ];
}
2009-11-11 17:04:47 +00:00
/**
2012-08-31 15:56:36 +00:00
* Resets all system_list () caches .
2018-11-30 10:48:27 +00:00
*
* @ deprecated in Drupal 8.7 . 0 and will be removed before Drupal 9.0 . 0. There
* is no direct replacement . Call each
* \Drupal :: service ( 'extension.list.TYPE' ) -> reset () as necessary .
*
* @ see https :// www . drupal . org / node / 2709919
2009-11-11 17:04:47 +00:00
*/
function system_list_reset () {
2018-11-30 10:48:27 +00:00
@ trigger_error ( " system_list_reset() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. There is no direct replacement. Call each \ Drupal::service('extension.list.TYPE')->reset() as necessary. See https://www.drupal.org/node/2709919. " , E_USER_DEPRECATED );
Issue #2659940 by almaudoh, alexpott, phenaproxima, Jo Fitzgerald, markcarver, oriol_e9g, dawehner, dsnopek, jibran, larowlan: Extension System, Part III: ThemeExtensionList and ThemeEngineExtensionList
2018-11-29 12:15:17 +00:00
\Drupal :: service ( 'extension.list.profile' ) -> reset ();
Issue #2208429 by dawehner, almaudoh, alexpott, jibran, Xano, donquixote, amitgoyal, bircher, stefan.r, Mile23, alvar0hurtad0, Berdir, andypost, larowlan: Extension System, Part III: ExtensionList, ModuleExtensionList and ProfileExtensionList
2018-01-24 15:21:31 +00:00
\Drupal :: service ( 'extension.list.module' ) -> reset ();
Issue #2659940 by almaudoh, alexpott, phenaproxima, Jo Fitzgerald, markcarver, oriol_e9g, dawehner, dsnopek, jibran, larowlan: Extension System, Part III: ThemeExtensionList and ThemeEngineExtensionList
2018-11-29 12:15:17 +00:00
\Drupal :: service ( 'extension.list.theme_engine' ) -> reset ();
\Drupal :: service ( 'extension.list.theme' ) -> reset ();
2009-11-11 17:04:47 +00:00
}
2012-11-22 11:04:32 +00:00
/**
* Registers an extension in runtime registries for execution .
*
* @ param string $type
* The extension type ; e . g . , 'module' or 'theme' .
* @ param string $name
* The internal name of the extension ; e . g . , 'node' .
* @ param string $uri
* The relative URI of the primary extension file ; e . g . ,
* 'core/modules/node/node.module' .
2019-04-11 03:08:45 +00:00
*
* @ deprecated in Drupal 8.8 . 0 and will be removed before Drupal 9.0 . 0. There is
* no replacement for this function . Use the following sequence of code to
* achieve the same functionality :
* @ code
* $path = \Drupal :: service ( " extension.list. $type " ) -> getPath ( $name );
* \Drupal :: service ( 'class_loader' ) -> addPsr4 ( 'Drupal\\' . $name . '\\' , \Drupal :: root () . '/' . $path . '/src' );
* @ endcode
2012-11-22 11:04:32 +00:00
*/
function system_register ( $type , $name , $uri ) {
2019-04-11 03:08:45 +00:00
@ trigger_error ( 'system_register() is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. There is no replacement for this function. To achieve the same functionality use this snippet: $path = \Drupal::service("extension.list.$type")->getPath($name); ' . " \\ Drupal::service('class_loader')->addPsr4('Drupal \\ \\ ' . \$ name . ' \\ \\ ', \\ Drupal::root() . '/' . \$ path . '/src'); See https://www.drupal.org/node/3035275. " , E_USER_DEPRECATED );
\Drupal :: service ( 'class_loader' ) -> addPsr4 ( 'Drupal\\' . $name . '\\' , \Drupal :: root () . '/' . dirname ( $uri ) . '/src' );
2012-11-22 11:04:32 +00:00
}
2006-08-03 01:02:51 +00:00
/**
2012-08-31 15:56:36 +00:00
* Loads a module ' s installation hooks .
2010-11-21 10:19:48 +00:00
*
* @ param $module
* The name of the module ( without the . module extension ) .
*
* @ return
* The name of the module ' s install file , if successful ; FALSE otherwise .
2006-08-03 01:02:51 +00:00
*/
function module_load_install ( $module ) {
// Make sure the installation API is available
2013-05-09 09:25:10 +00:00
include_once __DIR__ . '/install.inc' ;
2006-08-03 01:02:51 +00:00
2010-11-21 10:19:48 +00:00
return module_load_include ( 'install' , $module );
2007-05-25 12:46:46 +00:00
}
/**
2012-08-31 15:56:36 +00:00
* Loads a module include file .
2009-07-28 19:06:16 +00:00
*
2009-07-01 17:34:13 +00:00
* Examples :
* @ code
2009-07-10 04:58:08 +00:00
* // Load node.admin.inc from the node module.
2009-07-01 17:34:13 +00:00
* module_load_include ( 'inc' , 'node' , 'node.admin' );
2009-07-10 04:58:08 +00:00
* // Load content_types.inc from the node module.
2009-07-28 19:06:16 +00:00
* module_load_include ( 'inc' , 'node' , 'content_types' );
2009-07-01 17:34:13 +00:00
* @ endcode
2007-05-25 12:46:46 +00:00
*
2010-01-28 13:56:25 +00:00
* Do not use this function to load an install file , use module_load_install ()
* instead . Do not use this function in a global context since it requires
* Drupal to be fully bootstrapped , use require_once DRUPAL_ROOT . '/path/file'
2009-07-10 04:58:08 +00:00
* instead .
*
2007-05-25 12:46:46 +00:00
* @ param $type
* The include file ' s type ( file extension ) .
* @ param $module
* The module to which the include file belongs .
* @ param $name
2010-11-27 19:12:56 +00:00
* ( optional ) The base file name ( without the $type extension ) . If omitted ,
* $module is used ; i . e . , resulting in " $module . $type " by default .
2010-11-21 10:19:48 +00:00
*
* @ return
* The name of the included file , if successful ; FALSE otherwise .
2013-01-21 19:21:34 +00:00
*
* @ todo The module_handler service has a loadInclude () method which performs
* this same task but only for enabled modules . Figure out a way to move this
* functionality entirely into the module_handler while keeping the ability to
* load the files of disabled modules .
2007-05-25 12:46:46 +00:00
*/
function module_load_include ( $type , $module , $name = NULL ) {
2010-11-27 19:12:56 +00:00
if ( ! isset ( $name )) {
2007-05-25 12:46:46 +00:00
$name = $module ;
}
2009-08-24 00:14:23 +00:00
if ( function_exists ( 'drupal_get_path' )) {
2008-09-20 20:22:25 +00:00
$file = DRUPAL_ROOT . '/' . drupal_get_path ( 'module' , $module ) . " / $name . $type " ;
if ( is_file ( $file )) {
require_once $file ;
return $file ;
}
2007-05-25 12:46:46 +00:00
}
2008-09-20 20:22:25 +00:00
return FALSE ;
2007-05-25 12:46:46 +00:00
}
2007-02-04 21:20:50 +00:00
/**
2012-08-31 15:56:36 +00:00
* Returns an array of modules required by core .
2007-02-04 21:20:50 +00:00
*/
function drupal_required_modules () {
2014-11-17 12:20:57 +00:00
$listing = new ExtensionDiscovery ( \Drupal :: root ());
2014-02-28 11:51:38 +00:00
$files = $listing -> scan ( 'module' );
2017-03-04 01:20:24 +00:00
$required = [];
2009-08-21 07:50:08 +00:00
2014-02-28 11:51:38 +00:00
// Unless called by the installer, an installation profile is required and
2019-07-22 08:31:06 +00:00
// must always be loaded.
if ( $profile = \Drupal :: installProfile ()) {
2014-02-28 11:51:38 +00:00
$required [] = $profile ;
}
2009-08-21 07:50:08 +00:00
2008-10-12 01:23:07 +00:00
foreach ( $files as $name => $file ) {
2014-02-28 11:51:38 +00:00
$info = \Drupal :: service ( 'info_parser' ) -> parse ( $file -> getPathname ());
2008-10-12 01:23:07 +00:00
if ( ! empty ( $info ) && ! empty ( $info [ 'required' ]) && $info [ 'required' ]) {
$required [] = $name ;
}
}
2009-08-21 07:50:08 +00:00
2008-10-12 01:23:07 +00:00
return $required ;
2007-02-04 21:20:50 +00:00
}
2010-04-22 22:36:01 +00:00
2012-10-09 20:32:40 +00:00
/**
* Sets weight of a particular module .
*
* The weight of uninstalled modules cannot be changed .
*
* @ param string $module
* The name of the module ( without the . module extension ) .
* @ param int $weight
* An integer representing the weight of the module .
*/
function module_set_weight ( $module , $weight ) {
2015-01-16 10:43:35 +00:00
$extension_config = \Drupal :: configFactory () -> getEditable ( 'core.extension' );
2014-04-02 07:05:28 +00:00
if ( $extension_config -> get ( " module. $module " ) !== NULL ) {
2015-05-05 10:47:16 +00:00
// Pre-cast the $weight to an integer so that we can save this without using
// schema. This is a performance improvement for module installation.
2014-04-02 07:05:28 +00:00
$extension_config
2015-05-05 10:47:16 +00:00
-> set ( " module. $module " , ( int ) $weight )
2014-04-02 07:05:28 +00:00
-> set ( 'module' , module_config_sort ( $extension_config -> get ( 'module' )))
2015-05-05 10:47:16 +00:00
-> save ( TRUE );
2013-01-21 19:21:34 +00:00
// Prepare the new module list, sorted by weight, including filenames.
2016-07-27 17:09:51 +00:00
// @see \Drupal\Core\Extension\ModuleInstaller::install()
2013-09-16 03:58:06 +00:00
$module_handler = \Drupal :: moduleHandler ();
2013-01-21 19:21:34 +00:00
$current_module_filenames = $module_handler -> getModuleList ();
$current_modules = array_fill_keys ( array_keys ( $current_module_filenames ), 0 );
2014-04-02 07:05:28 +00:00
$current_modules = module_config_sort ( array_merge ( $current_modules , $extension_config -> get ( 'module' )));
2017-03-04 01:20:24 +00:00
$module_filenames = [];
2013-01-21 19:21:34 +00:00
foreach ( $current_modules as $name => $weight ) {
$module_filenames [ $name ] = $current_module_filenames [ $name ];
}
// Update the module list in the extension handler.
$module_handler -> setModuleList ( $module_filenames );
2012-10-09 20:32:40 +00:00
return ;
}
}
/**
* Sorts the configured list of enabled modules .
*
* The list of enabled modules is expected to be ordered by weight and name .
* The list is always sorted on write to avoid the overhead on read .
*
* @ param array $data
* An array of module configuration data .
*
* @ return array
* An array of module configuration data sorted by weight and name .
*/
function module_config_sort ( $data ) {
// PHP array sorting functions such as uasort() do not work with both keys and
// values at the same time, so we achieve weight and name sorting by computing
// strings with both information concatenated (weight first, name second) and
// use that as a regular string sort reference list via array_multisort(),
// compound of "[sign-as-integer][padded-integer-weight][name]"; e.g., given
// two modules and weights (spaces added for clarity):
// - Block with weight -5: 0 0000000000000000005 block
// - Node with weight 0: 1 0000000000000000000 node
2017-03-04 01:20:24 +00:00
$sort = [];
2012-10-09 20:32:40 +00:00
foreach ( $data as $name => $weight ) {
// Prefix negative weights with 0, positive weights with 1.
// +/- signs cannot be used, since + (ASCII 43) is before - (ASCII 45).
$prefix = ( int ) ( $weight >= 0 );
// The maximum weight is PHP_INT_MAX, so pad all weights to 19 digits.
$sort [] = $prefix . sprintf ( '%019d' , abs ( $weight )) . $name ;
}
array_multisort ( $sort , SORT_STRING , $data );
return $data ;
}