Modules / $modules

The $modules API variable loads, manages, installs and configures modules in ProcessWire.

Getting modules
// Get a module by name — returns null if not found
$m = $modules->get('MarkupPagerNav');
$m = $modules->MarkupPagerNav; // alternate property-access form

// Get with options
$m = $modules->getModule('ModuleName', [
    'noInit'             => true,  // don't call module init()
    'noInstall'          => true,  // don't auto-install if uninstalled
    'noPermissionCheck'  => true,  // skip permission check
    'noThrow'            => true,  // return null instead of throwing
    'configData'         => [...], // extra config data to merge in
]);

getModule() options

OptionDefaultDescription
noPermissionCheckfalseSkip module permission check (and resulting exception)
noInstallfalseDon't auto-install uninstalled modules
noInitfalseDon't call module init() — see also configOnly
configOnlyfalsePopulate config data but don't call init()
configData[]Extra config data merged with module's stored config
noSubstitutefalseDon't fall back to a substitute module
noCachefalseDon't cache the resolved module instance
noThrowfalseReturn null instead of throwing on permission/fatal errors
returnErrorfalseReturn error string instead of null on failure
Finding modules

Return value is an array indexed by module class name and the contents of the returned array is dictated by the $load argument. All three find methods accept the same $load argument:

$load valueWhat is returned
falseArray of module names (default)
trueArray of instantiated module objects
1Array of module info arrays
2Array of verbose module info arrays
// Find modules whose class name starts with a prefix
$inputfields = $modules->findByPrefix('Inputfield');
$inputfields = $modules->findByPrefix('Inputfield', true);  // load=true: get instances

// Find modules that have a given flag set (fastest method)
$cliModules  = $modules->findByFlag(Modules::flagsCli);
$autoloaders = $modules->findByFlag(Modules::flagsAutoload, 1); // load=1: get info arrays

// Find modules by a module info property or selector
$autoloads = $modules->findByInfo('autoload');             // non-empty 'autoload'
$matches   = $modules->findByInfo('autoload=1, core=1');   // selector string
$matches   = $modules->findByInfo('author*=Ryan, core=0'); // partial match 'Ryan'
$matches   = $modules->findByInfo(['autoload' => 1]);      // array match
Module status checks
// Is module installed?
if($modules->isInstalled('ModuleName')) { ... }

// Is module installable (file on disk, not yet installed)?
if($modules->isInstallable('ModuleName')) { ... }
if($modules->isInstallable('ModuleName', true)) { ... } // true = all deps also available now

// Does the module load automatically at boot?
if($modules->isAutoload($module)) { ... }

// Does the module support only a single instance at runtime?
if($modules->isSingular($module)) { ... }

// Is the module interactively configurable?
if($modules->isConfigurable('ModuleName')) { ... }
Installing and uninstalling
// Install a module — also installs dependencies by default
$module = $modules->install('ModuleName');
$module = $modules->install('ModuleName', [
    'dependencies' => true,  // also install uninstalled dependencies (default=true)
    'resetCache'   => true,  // reset module info cache after install (default=true)
    'force'        => false, // install even if dependencies can't be met (default=false)
]);

// Uninstall a module (returns bool)
$modules->uninstall('ModuleName');

// Physically delete a module's files from disk (must be uninstalled first)
$modules->delete('ModuleName');

// Refresh modules list — picks up new, moved, or changed module files on disk
$modules->refresh();
$modules->refresh(true); // show admin notice messages about what changed
Module info

getModuleInfo() returns an associative array with at least these properties:

KeyTypeDescription
idintDatabase ID
namestringModule class name
titlestringModule title
versionintModule version integer
iconstringOptional icon name (Font Awesome, without "fa-" prefix)
requiresarrayModule class names required by this module
requiresVersionsarrayRequired modules with operator+version, keyed by name
installsarrayModule class names this module auto-installs
permissionstringPermission name required to execute this module
autoloadboolDoes the module load at boot?
singularboolSingle instance at runtime?
createdintUnix timestamp of when module was installed
installedboolIs the module currently installed?
configurablebool or intIs the module configurable? (see isConfigurable())
namespacestringPHP namespace the module class lives in

getModuleInfoVerbose() additionally returns the following:

KeyTypeDescription
versionStrstringVersion in string format, i.e. 0.1.7
summarystringShort summary of what the module does.
authorstringThe module author name(s).
hrefstringURL for more information.
filestringFull disk path/file for the module.
corebool or intNon-empty if this is a core module.
permissionsarrayPermissions the module installs/uninstalls (example below).
searchablebool or nullTrue when module implements SearchableModule
  • permissions value example: ['permission-name' => 'Description description']

getModuleInfo usage examples

// Get common module info
$info = $modules->getModuleInfo('ModuleName');
echo $info['title'];
echo $modules->formatVersion($info['version']); // e.g. "1.2.3"

// Get verbose module info (includes summary, author, file, core, etc.)
$info = $modules->getModuleInfoVerbose('ModuleName');

// Get a single module info property (uses cache, fast)
$version = $modules->getModuleInfoProperty('ModuleName', 'version');

// Get abbreviated info for all installed modules, indexed by module ID
$all = $modules->getModuleInfo('*');

// Get a blank info template array (all keys with default values)
$template = $modules->getModuleInfo('info');

getModuleInfo for Process modules

Modules of type Process also may optionally include the following in their verbose module info. Please see the Process module interface wire/core/Module/Process/Process.php for details.

KeyTypeDescription
pagearrayInfo for page to create on install (and remove on uninstall).†
navarrayAdmin navigation definition.
useNavJSONboolWhether the module implements an ___executeNavJSON() method for AJAX JSON navigation.

page value example:

'page' => [ 'name' => 'foo', 'parent' => 'setup', 'title' => 'Foo' ],

nav value example:

'nav' => [ 
    [ 'url' => '', 'label' => 'Foo', 'icon' => 'smile-o' ],
    [ 'url' => 'bar/', 'label' => 'Bar', 'icon' => 'home' ],
    [ 'url' => 'baz/', 'label' => 'Baz', 'icon' => 'sliders' ]
],
Module configuration
// Get all config data for a module
$data = $modules->getConfig('HelloWorld');

// Get a single config property
$apiKey = $modules->getConfig('HelloWorld', 'apiKey');

// Save all config data
$data = $modules->getConfig('HelloWorld');
$data['greeting'] = 'Hello!';
$modules->saveConfig('HelloWorld', $data);

// Save a single config property (key, value form)
$modules->saveConfig('HelloWorld', 'greeting', 'Hello!');

// Get URL to the module's config/edit screen in the admin
$url = $modules->getModuleEditUrl('ModuleName');

// Get URL to install a module (or its edit screen if already installed)
$url = $modules->getModuleInstallUrl('ModuleName');
Hooks

Hook before or after any of the following methods:

HookWhen fired
Modules::installWhen a module is installed
Modules::uninstallWhen a module is uninstalled
Modules::deleteWhen a module's files are deleted from disk
Modules::refreshWhen the modules list is refreshed
Modules::saveConfigWhen module config data is saved
Modules::moduleVersionChangedWhen a module's version changes on load
// Example: log after a module is installed
$wire->addHookAfter('Modules::install', function(HookEvent $e) {
    $class  = $e->arguments(0); // module class name (string)
    $module = $e->return;       // installed Module object
    $this->log("Installed module: $class");
});

// Example: hook directly to $modules, before config is saved
// and display an notification of what is being saved
$modules->addHookBefore('saveConfig', function(HookEvent $e) {
    $module = $e->arguments(0); // Module class or instance
    $class = $module instanceof Module ? $module->className() : $module;
    $data = $e->arguments(1); // array of data, or property name string
    if(is_array($data)) {
        // saving entire module config
        $e->message([ "Saving $class config data" => $data ]);
    } else {
        // saving just a property of the config
        $property = $data;
        $value = $e->arguments(2);
        $e->message([ "Saving module $class $property" => $value ]);
    }
}); 
Helper classes (wire/core/Modules/)
PropertyClassPurpose
$modules->infoModulesInfoModule info cache; getModuleInfo() etc.
$modules->loaderModulesLoaderBoot loading; init/ready triggers
$modules->flagsModulesFlagsModule flags in-memory cache
$modules->filesModulesFilesFile discovery and inclusion
$modules->configsModulesConfigsConfig data get/save
$modules->installerModulesInstallerInstall, uninstall, delete (lazy-loaded)
Notes
  • $modules->get() and property access auto-install a module if its file is present but not yet installed. Pass 'noInstall' => true to getModule() to prevent this.
  • When iterating $modules directly, items may be ModulePlaceholder instances rather than real modules. Call $modules->get($name) to get the real instantiated module.
  • All write methods (install, uninstall, delete, saveConfig, refresh) are hookable via the triple-underscore ___methodName() pattern.
  • getConfig() / saveConfig() replaced the older getModuleConfigData() / saveModuleConfigData() names in ProcessWire 3.0.16. Both names still work.
More about modules and how to make them

Please see the Module class documentation for details on the Module interface, the different types of modules, and to learn more about how to create modules.

API reference: methods, properties, hooks

The $modules API variable is most commonly used for getting individual modules to use their API.

// Getting a module by name
$m = $modules->get('MarkupPagerNav');

// Getting a module by name (alternate)
$m = $modules->MarkupPagerNav;

// Getting a module by name with options
$m = $modules->getModule('MarkupPagerNav', [ 'noInit' => true ]);

// Get associative array of module information
$info = $modules->getModuleInfo('FieldtypePage');

// Get verbose associative array of module information with more details
$info = $modules->getModuleInfoVerbose('FieldtypePage');

// refresh the modules, picking up changes to module info, etc.
$modules->refresh();

Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Modules class also inherits all the methods and properties of: WireArray and Wire.

Show $var?     Show args?       Only hookable?    

Common

NameReturnSummary 
$modules->findByFlag(int $flag)
array

Find modules by matching flag

 
$modules->findByInfo($selector)
array

Find modules by matching a property or properties in their module info

 
$modules->findByPrefix(string $prefix)
array

Find modules matching the given prefix (i.e. “Inputfield”)

 
$modules->get($key)
Module _Module null

Get the requested Module

 
$modules->getDebugData()
array

Get array of debug data indexed by property

 
$modules->getModule($key)
Module _Module null string

Get the requested Module (with options)

 
$modules->getModuleFile($class)
bool string

Get the path + filename (or optionally URL) for this module

 
$modules->getModuleID($class)
int

Returns the database ID of a given module class, or 0 if not found

 
$modules->getModuleInfo($class)
array

Returns an associative array of information for a Module

 
$modules->getModuleInfoProperty($class, string $property)
mixed null

Get just a single property of module info

 
$modules->getModuleInfoVerbose($class)
array

Returns a verbose array of information for a Module

 
$modules->getModuleLanguageFiles($module)
array

Get module language translation files

 
$modules->isInstalled(string $class)
bool

Is the given module name installed?

 
$modules->memcache(string $name)
bool array mixed null

Set a runtime memory cache

 

Configuration

NameReturnSummary 
$modules->getConfig($class)
array string int float

Given a module name, return an associative array of configuration data for it

 
$modules->getModuleEditUrl($className)
string

Return the URL where the module can be edited, configured or uninstalled

 
$modules->getModuleInstallUrl(string $className)
string

Get URL where an administrator can install given module name

 
$modules->isConfigurable($class)
bool string int

Is the given module interactively configurable?

 
$modules->saveConfig($class, $data)
bool

Save provided configuration data for the given module

Properties

NameReturnSummary 
$modules->configs ModulesConfigs 
$modules->coreModulesDir string 
$modules->coreModulesPath string 
$modules->files ModulesFiles 
$modules->flags ModulesFlags 
$modules->info ModulesInfo 
$modules->installableFiles array 
$modules->installer ModulesInstaller 
$modules->loader ModulesLoader 
$modules->moduleIDs array 
$modules->moduleNames array 
$modules->refreshing bool 
$modules->siteModulesPath string 

Additional methods and properties

In addition to the methods and properties above, Modules also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.260