Drupal  8.3.5
APl Documentation
Drupal Class Reference

Static Public Member Functions

static setContainer (ContainerInterface $container)
 
static unsetContainer ()
 
static getContainer ()
 
static hasContainer ()
 
static service ($id)
 
static hasService ($id)
 
static root ()
 
static installProfile ()
 
static hasRequest ()
 
static request ()
 
static requestStack ()
 
static routeMatch ()
 
static currentUser ()
 
static entityManager ()
 
static entityTypeManager ()
 
static database ()
 
static cache ($bin='default')
 
static classResolver ()
 
static keyValueExpirable ($collection)
 
static lock ()
 
static config ($name)
 
static configFactory ()
 
static queue ($name, $reliable=FALSE)
 
static keyValue ($collection)
 
static state ()
 
static httpClient ()
 
static entityQuery ($entity_type, $conjunction='AND')
 
static entityQueryAggregate ($entity_type, $conjunction='AND')
 
static flood ()
 
static moduleHandler ()
 
static typedDataManager ()
 
static token ()
 
static urlGenerator ()
 
static url ($route_name, $route_parameters=[], $options=[], $collect_bubbleable_metadata=FALSE)
 
static linkGenerator ()
 
static l ($text, Url $url)
 
static translation ()
 
static languageManager ()
 
static csrfToken ()
 
static transliteration ()
 
static formBuilder ()
 
static theme ()
 
static isConfigSyncing ()
 
static logger ($channel)
 
static menuTree ()
 
static pathValidator ()
 
static accessManager ()
 
static destination ()
 
static entityDefinitionUpdateManager ()
 
static time ()
 

Data Fields

const VERSION = '8.3.5'
 
const CORE_COMPATIBILITY = '8.x'
 
const CORE_MINIMUM_SCHEMA_VERSION = 8000
 

Static Protected Attributes

static $container
 

Detailed Description

Static Service Container wrapper.

Generally, code in Drupal should accept its dependencies via either constructor injection or setter method injection. However, there are cases, particularly in legacy procedural code, where that is infeasible. This class acts as a unified global accessor to arbitrary services within the system in order to ease the transition from procedural code to injected OO code.

The container is built by the kernel and passed in to this class which stores it statically. The container always contains the services from , the service providers of enabled modules and any other service providers defined in $GLOBALS['conf']['container_service_providers'].

This class exists only to support legacy code that cannot be dependency injected. If your code needs it, consider refactoring it to be object oriented, if possible. When this is not possible, for instance in the case of hook implementations, and your code is more than a few non-reusable lines, it is recommended to instantiate an object implementing the actual logic.

// Legacy procedural code.
function hook_do_stuff() {
$lock = lock()->acquire('stuff_lock');
// ...
}
// Correct procedural code.
function hook_do_stuff() {
$lock = \Drupal::lock()->acquire('stuff_lock');
// ...
}
// The preferred way: dependency injected code.
function hook_do_stuff() {
// Move the actual implementation to a class and instantiate it.
$instance = new StuffDoingClass(\Drupal::lock());
$instance->doStuff();
// Or, even better, rely on the service container to avoid hard coding a
// specific interface implementation, so that the actual logic can be
// swapped. This might not always make sense, but in general it is a good
// practice.
\Drupal::service('stuff.doing')->doStuff();
}
interface StuffDoingInterface {
public function doStuff();
}
class StuffDoingClass implements StuffDoingInterface {
protected $lockBackend;
public function __construct(LockBackendInterface $lock_backend) {
$this->lockBackend = $lock_backend;
}
public function doStuff() {
$lock = $this->lockBackend->acquire('stuff_lock');
// ...
}
}
See also

Definition at line 79 of file Drupal.php.

Member Function Documentation

◆ accessManager()

static accessManager ( )
static

Returns the access manager service.

Returns
The access manager service.

Definition at line 726 of file Drupal.php.

◆ classResolver()

static classResolver ( )
static

Retrieves the class resolver.

This is to be used in procedural code such as module files to instantiate an object of a class that implements .

One common usecase is to provide a class which contains the actual code of a hook implementation, without having to create a service.

Returns
The class resolver.

Definition at line 325 of file Drupal.php.

◆ config()

static config (   $name)
static

Retrieves a configuration object.

This is the main entry point to the configuration API. Calling

\Drupal::config('book.admin')

will return a configuration object in which the book module can store its administrative settings.

Parameters
string$nameThe name of the configuration object to retrieve. The name corresponds to a configuration file. For
\Drupal::config('book.admin')
, the config object returned will contain the contents of book.admin configuration file.
Returns
An immutable configuration object.

Definition at line 368 of file Drupal.php.

◆ configFactory()

static configFactory ( )
static

Retrieves the configuration factory.

This is mostly used to change the override settings on the configuration factory. For example, changing the language, or turning all overrides on or off.

Returns
The configuration factory service.

Definition at line 382 of file Drupal.php.

◆ csrfToken()

static csrfToken ( )
static

Returns the CSRF token manager service.

The generated token is based on the session ID of the current user. Normally, anonymous users do not have a session, so the generated token will be different on every page request. To generate a token for users without a session, manually start a session prior to calling this function.

Returns
The CSRF token manager.
See also
::start()

Definition at line 644 of file Drupal.php.

◆ currentUser()

static currentUser ( )
static

Gets the current active user.

Returns

Definition at line 256 of file Drupal.php.

◆ database()

static database ( )
static

Returns the current primary database.

Returns
The current active database's master connection.

Definition at line 292 of file Drupal.php.

◆ destination()

static destination ( )
static

Returns the redirect destination helper.

Returns
The redirect destination helper.

Definition at line 736 of file Drupal.php.

◆ entityDefinitionUpdateManager()

static entityDefinitionUpdateManager ( )
static

Returns the entity definition update manager.

Returns
The entity definition update manager.

Definition at line 746 of file Drupal.php.

◆ entityManager()

static entityManager ( )
static

Retrieves the entity manager service.

Returns
The entity manager service.
Deprecated:
in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use ::entityTypeManager() instead in most cases. If the needed method is not on , see the deprecated to find the correct interface or service.

Definition at line 272 of file Drupal.php.

◆ entityQuery()

static entityQuery (   $entity_type,
  $conjunction = 'AND' 
)
static

Returns the entity query object for this entity type.

Parameters
string$entity_typeThe entity type (for example, node) for which the query object should be returned.
string$conjunction(optional) Either 'AND' if all conditions in the query need to apply, or 'OR' if any of them is sufficient. Defaults to 'AND'.
Returns
The query object that can query the given entity type.

Definition at line 462 of file Drupal.php.

◆ entityQueryAggregate()

static entityQueryAggregate (   $entity_type,
  $conjunction = 'AND' 
)
static

Returns the entity query aggregate object for this entity type.

Parameters
string$entity_typeThe entity type (for example, node) for which the query object should be returned.
string$conjunction(optional) Either 'AND' if all conditions in the query need to apply, or 'OR' if any of them is sufficient. Defaults to 'AND'.
Returns
The query object that can query the given entity type.

Definition at line 479 of file Drupal.php.

◆ entityTypeManager()

static entityTypeManager ( )
static

Retrieves the entity type manager.

Returns
The entity type manager.

Definition at line 282 of file Drupal.php.

◆ flood()

static flood ( )
static

Returns the flood instance.

Returns

Definition at line 488 of file Drupal.php.

◆ formBuilder()

static formBuilder ( )
static

Returns the form builder service.

Returns
The form builder.

Definition at line 664 of file Drupal.php.

◆ getContainer()

static getContainer ( )
static

Returns the currently active global container.

Returns
|null
Exceptions

Definition at line 127 of file Drupal.php.

◆ hasContainer()

static hasContainer ( )
static

Returns TRUE if the container has been initialized, FALSE otherwise.

Returns
bool

Definition at line 139 of file Drupal.php.

◆ hasRequest()

static hasRequest ( )
static

Indicates if there is a currently active request object.

Returns
bool TRUE if there is a currently active request object, FALSE otherwise.

Definition at line 200 of file Drupal.php.

◆ hasService()

static hasService (   $id)
static

Indicates if a service is defined in the container.

Parameters
string$idThe ID of the service to check.
Returns
bool TRUE if the specified service exists, FALSE otherwise.

Definition at line 170 of file Drupal.php.

◆ httpClient()

static httpClient ( )
static

Returns the default http client.

Returns
A guzzle http client instance.

Definition at line 445 of file Drupal.php.

◆ installProfile()

static installProfile ( )
static

Gets the active install profile.

Returns
string|null The name of the active install profile.

Definition at line 190 of file Drupal.php.

◆ isConfigSyncing()

static isConfigSyncing ( )
static

Gets the syncing state.

Returns
bool Returns TRUE is syncing flag set.

Definition at line 683 of file Drupal.php.

◆ keyValue()

static keyValue (   $collection)
static

Returns a key/value storage collection.

Parameters
string$collectionName of the key/value collection to return.
Returns

Definition at line 420 of file Drupal.php.

◆ keyValueExpirable()

static keyValueExpirable (   $collection)
static

Returns an expirable key value store collection.

Parameters
string$collectionThe name of the collection holding key and value pairs.
Returns
An expirable key value store collection.

Definition at line 338 of file Drupal.php.

◆ l()

static l (   $text,
Url  $url 
)
static

Renders a link with a given link text and Url object.

This method is a convenience wrapper for the link generator service's generate() method.

Parameters
string$textThe link text for the anchor tag.
\Drupal\Core\Url$urlThe URL object used for the link.
Returns
A GeneratedLink object containing a link to the given route and parameters and bubbleable metadata.
See also
::generate()
Deprecated:
in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use instead. Example:
$link = Link::fromTextAndUrl($text, $url);

Definition at line 607 of file Drupal.php.

◆ languageManager()

static languageManager ( )
static

Returns the language manager service.

Returns
The language manager.

Definition at line 627 of file Drupal.php.

◆ linkGenerator()

static linkGenerator ( )
static

Returns the link generator service.

Returns

Definition at line 578 of file Drupal.php.

◆ logger()

static logger (   $channel)
static

Returns a channel logger object.

Parameters
string$channelThe name of the channel. Can be any string, but the general practice is to use the name of the subsystem calling this.
Returns
The logger for this channel.

Definition at line 697 of file Drupal.php.

◆ menuTree()

static menuTree ( )
static

Returns the menu tree.

Returns
The menu tree.

Definition at line 707 of file Drupal.php.

◆ moduleHandler()

static moduleHandler ( )
static

Returns the module handler.

Returns

Definition at line 497 of file Drupal.php.

◆ pathValidator()

static pathValidator ( )
static

Returns the path validator.

Returns

Definition at line 716 of file Drupal.php.

◆ queue()

static queue (   $name,
  $reliable = FALSE 
)
static

Returns a queue for the given queue name.

The following values can be set in your settings.php file's $settings array to define which services are used for queues:

  • queue_reliable_service_$name: The container service to use for the reliable queue $name.
  • queue_service_$name: The container service to use for the queue $name.
  • queue_default: The container service to use by default for queues without overrides. This defaults to 'queue.database'.
Parameters
string$nameThe name of the queue to work with.
bool$reliable(optional) TRUE if the ordering of items and guaranteeing every item executes at least once is important, FALSE if scalability is the main concern. Defaults to FALSE.
Returns
The queue object for a given name.

Definition at line 408 of file Drupal.php.

◆ request()

static request ( )
static

Retrieves the currently active request object.

Note: The use of this wrapper in particular is especially discouraged. Most code should not need to access the request directly. Doing so means it will only function when handling an HTTP request, and will require special modification or wrapping when run from a command line tool, from certain queue processors, or from automated tests.

If code must access the request, it is considerably better to register an object with the Service Container and give it a setRequest() method that is configured to run when the service is created. That way, the correct request object can always be provided by the container and the service can still be unit tested.

If this method must be used, never save the request object that is returned. Doing so may lead to inconsistencies as the request object is volatile and may change at various times, such as during a subrequest.

Returns
The currently active request object.

Definition at line 227 of file Drupal.php.

◆ requestStack()

static requestStack ( )
static

Retrives the request stack.

Returns
The request stack

Definition at line 237 of file Drupal.php.

◆ root()

static root ( )
static

Gets the app root.

Returns
string

Definition at line 180 of file Drupal.php.

◆ routeMatch()

static routeMatch ( )
static

Retrieves the currently active route match object.

Returns
The currently active route match object.

Definition at line 247 of file Drupal.php.

◆ service()

static service (   $id)
static

Retrieves a service from the container.

Use this method if the desired service is not one of those with a dedicated accessor method below. If it is listed below, those methods are preferred as they can return useful type hints.

Parameters
string$idThe ID of the service to retrieve.
Returns
mixed The specified service.

Definition at line 157 of file Drupal.php.

◆ setContainer()

static setContainer ( ContainerInterface  $container)
static

Sets a new global container.

Parameters
\Symfony\Component\DependencyInjection\ContainerInterface$containerA new container instance to replace the current.

Definition at line 109 of file Drupal.php.

◆ state()

static state ( )
static

Returns the state storage service.

Use this to store machine-generated data, local to a specific environment that does not need deploying and does not need human editing; for example, the last time cron was run. Data which needs to be edited by humans and needs to be the same across development, production, etc. environments (for example, the system maintenance message) should use ::config() instead.

Returns

Definition at line 435 of file Drupal.php.

◆ theme()

static theme ( )
static

Gets the theme service.

Returns

Definition at line 673 of file Drupal.php.

◆ time()

static time ( )
static

Returns the time service.

Returns
The time service.

Definition at line 756 of file Drupal.php.

◆ token()

static token ( )
static

Returns the token service.

Returns
The token service.

Definition at line 521 of file Drupal.php.

◆ translation()

static translation ( )
static

Returns the string translation service.

Returns
The string translation manager.

Definition at line 617 of file Drupal.php.

◆ transliteration()

static transliteration ( )
static

Returns the transliteration service.

Returns
The transliteration manager.

Definition at line 654 of file Drupal.php.

◆ typedDataManager()

static typedDataManager ( )
static

Returns the typed data manager service.

Use the typed data manager service for creating typed data objects.

Returns
The typed data manager.
See also
create()

Definition at line 511 of file Drupal.php.

◆ unsetContainer()

static unsetContainer ( )
static

Unsets the global container.

Definition at line 116 of file Drupal.php.

◆ url()

static url (   $route_name,
  $route_parameters = [],
  $options = [],
  $collect_bubbleable_metadata = FALSE 
)
static

Generates a URL string for a specific route based on the given parameters.

This method is a convenience wrapper for generating URL strings for URLs that have Drupal routes (that is, most pages generated by Drupal) using the object. See ::fromRoute() for detailed documentation. For non-routed local URIs relative to the base path (like robots.txt) use Url::fromUri()->toString() with the base: scheme.

Parameters
string$route_nameThe name of the route.
array$route_parameters(optional) An associative array of parameter names and values.
array$options(optional) An associative array of additional options.
bool$collect_bubbleable_metadata(optional) Defaults to FALSE. When TRUE, both the generated URL and its associated bubbleable metadata are returned.
Returns
string| A string containing a URL to the given path. When $collect_bubbleable_metadata is TRUE, a GeneratedUrl object is returned, containing the generated URL plus bubbleable metadata.
See also
::generateFromRoute()
::fromRoute()
::fromUri()
Deprecated:
as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Instead create a object directly, for example using Url::fromRoute().

Definition at line 569 of file Drupal.php.

◆ urlGenerator()

static urlGenerator ( )
static

Returns the url generator service.

Returns
The url generator service.

Definition at line 531 of file Drupal.php.

Field Documentation

◆ $container

$container
staticprotected

Definition at line 101 of file Drupal.php.

◆ CORE_COMPATIBILITY

const CORE_COMPATIBILITY = '8.x'

Core API compatibility.

Definition at line 89 of file Drupal.php.

◆ CORE_MINIMUM_SCHEMA_VERSION

const CORE_MINIMUM_SCHEMA_VERSION = 8000

Core minimum schema version.

Definition at line 94 of file Drupal.php.

◆ VERSION

const VERSION = '8.3.5'

The current system version.

Definition at line 84 of file Drupal.php.


The documentation for this class was generated from the following file: