src/Drupal/functions ranked.php

Functions

drupal_strlen()

drupal_strlen(mixed  $text) : int

Returns the number of characters in a string, as Drupal 7's drupal_strlen() did.

Always uses mb_strlen(). Drupal 7's byte-counting fallback for sites without mbstring is gone.

Parameters

mixed $text

The string to measure. Null counts as zero characters.

Returns

int —

The length of the string in characters.

drupal_strtolower()

drupal_strtolower(mixed  $text) : string

Lowercases a UTF-8 string, as Drupal 7's drupal_strtolower() did with mbstring.

Parameters

mixed $text

The string to convert.

Returns

string —

The lowercased string.

drupal_strtoupper()

drupal_strtoupper(mixed  $text) : string

Uppercases a UTF-8 string, as Drupal 7's drupal_strtoupper() did with mbstring.

Parameters

mixed $text

The string to convert.

Returns

string —

The uppercased string.

drupal_substr()

drupal_substr(mixed  $text, int  $start, int|null  $length = null) : string

Cuts a piece out of a string by character index, as Drupal 7's drupal_substr() did.

Parameters

mixed $text

The input string.

int $start

The character position at which to start reading.

int|null $length

The number of characters to read, or null to read to the end.

Returns

string —

The shortened string.

drupal_ucfirst()

drupal_ucfirst(mixed  $text) : string

Uppercases the first character of a string, as Drupal 7's drupal_ucfirst() did.

Parameters

mixed $text

The string to convert.

Returns

string —

The string with its first character uppercased.

decode_entities()

decode_entities(mixed  $text) : string

Decodes HTML entities to UTF-8 characters, as Drupal 7's decode_entities() did.

Parameters

mixed $text

The text to decode.

Returns

string —

The decoded text, with single and double quote entities decoded too.

parse_size()

parse_size(mixed  $size) : float

Converts a size string such as "2M" to bytes, as Drupal 7's parse_size() did.

Parameters

mixed $size

The size string. A unit letter from "bkmgtpezy" scales the number by powers of 1024.

Returns

float —

The size in bytes, rounded to a whole number.

drupal_is_cli()

drupal_is_cli() : bool

Tells whether the code runs from the command line, as Drupal 7's drupal_is_cli() did.

Reads a missing argc as not numeric instead of raising a notice.

Returns

bool —

True when running under the CLI.

drupal_http_build_query()

drupal_http_build_query(array  $query, string  $parent = '') : string

Builds a URL-encoded query string from an array, as Drupal 7's did.

Keys without a value are emitted bare, and slashes in values stay unencoded.

Parameters

array $query

The query parameters, which may be nested.

string $parent

The encoded parent key for nested values. Internal use.

Returns

string —

The query string, without a leading question mark.

drupal_get_query_parameters()

drupal_get_query_parameters(array|null  $query = null, array  $exclude = ['q'], string  $parent = '') : array

Returns the query parameters minus the excluded keys, as Drupal 7's did.

The default source is the current Symfony request instead of Drupal 7's $_GET.

Parameters

array|null $query

The parameters to filter, or null for the current request's query.

array $exclude

The keys to drop. Nested keys use the "a[b]" form.

string $parent

The parent key for nested values. Internal use.

Returns

array —

The filtered parameters.

format_plural()

format_plural(int|float|string  $count, string  $singular, string  $plural, array  $args = [], array  $options = []) : string

Formats a string containing a count, as Drupal 7's format_plural() did.

Delegates to the translation service's formatPlural().

Parameters

int|float|string $count

The item count.

string $singular

The text for a count of 1.

string $plural

The text for any other count, with "@count" as the placeholder.

array $args

Placeholder replacements.

array $options

Translation options, such as "langcode".

Returns

string —

The translated string.

format_size()

format_size(int|float  $size, string|null  $langcode = null) : string

Formats a byte count as a human-readable string, as Drupal 7's format_size() did.

Parameters

int|float $size

The size in bytes.

string|null $langcode

The language code for the units, or null for the current language.

Returns

string —

The size with its unit, such as "1.5 MB".

format_interval()

format_interval(int  $interval, int  $granularity = 2, string|null  $langcode = null) : string

Formats a time interval with the requested granularity, as Drupal 7's did.

Parameters

int $interval

The length of the interval in seconds.

int $granularity

The maximum number of units to show.

string|null $langcode

The language code for the units, or null for the current language.

Returns

string —

The interval, such as "1 week 2 days", or "0 sec" for an empty interval.

format_date()

format_date(int|string  $timestamp, string  $type = 'medium', string  $format = '', string|null  $timezone = null, string|null  $langcode = null) : string

Formats a timestamp with the date formatter service, as Drupal 7's format_date() did.

Parameters

int|string $timestamp

The Unix timestamp.

string $type

The date format type, such as "short", "medium", "long" or "custom".

string $format

The PHP date format, used when the type is "custom".

string|null $timezone

The timezone name, or null for the user's timezone.

string|null $langcode

The language code, or null for the current language.

Returns

string —

The formatted date.

arg()

arg(int|null  $index = null, string|null  $path = null) : string|array|null

Returns a component of the current path, as Drupal 7's arg() did.

Reads the path from the path.current service instead of Drupal 7's $_GET['q'].

Parameters

int|null $index

The component to return, or null for all components.

string|null $path

The path to split, or null for the current path.

Returns

string|array|null —

The component, the full array when no index is given, or null when it is missing.

drupal_get_destination()

drupal_get_destination() : array

Returns the destination query parameter for the current page, as Drupal 7's did.

Unlike Drupal 7, the result is not cached for the request.

Returns

array —

An array with a single "destination" key.

user_load()

user_load(int|string  $uid, bool  $reset = false) : \Drupal\user\UserInterface|false

Loads a user entity by ID, as Drupal 7's user_load() did.

Parameters

int|string $uid

The user ID.

bool $reset

Whether to reset the entity cache first.

Returns

\Drupal\user\UserInterface|false —

The user entity, or false when none exists.

user_roles()

user_roles(bool  $membersonly = false, string|null  $permission = null) : string[]

Returns role labels keyed by role ID, as Drupal 7's user_roles() did.

Roles are sorted by weight, then label. Role IDs are machine names, where Drupal 7 used numeric role IDs.

Parameters

bool $membersonly

Whether to leave out the anonymous role.

string|null $permission

When set, only roles holding this permission are returned.

Returns

string[] —

Role labels keyed by role ID.

node_access()

node_access(string  $op, \Drupal\node\NodeInterface|string|null  $node, \Drupal\Core\Session\AccountInterface|object|null  $account = null) : bool

Checks node access for an operation, as Drupal 7's node_access() did.

Fails closed: a missing node, an unsupported operation, an unresolvable account or a node type string with any operation other than "create" returns false. Access is decided by the node entity access handler, not by Drupal 7's hook_node_access() and node_access table.

Parameters

string $op

The operation: "view", "update", "delete" or "create".

\Drupal\node\NodeInterface|string|null $node

The node, or a node type ID for "create".

\Drupal\Core\Session\AccountInterface|object|null $account

The account to check, or null for the current user.

Returns

bool —

True when access is granted.

entity_label()

entity_label(string  $entity_type, mixed  $entity) : string|false

Returns the label of an entity, as Drupal 7's entity_label() did.

Parameters

string $entity_type

The entity type ID. Unused; the entity supplies its own label.

mixed $entity

The entity.

Returns

string|false —

The label, or false when the value is not an entity or has no label.

taxonomy_term_load()

taxonomy_term_load(int|string  $tid) : \Drupal\taxonomy\TermInterface|false

Loads a taxonomy term by ID, as Drupal 7's taxonomy_term_load() did.

Parameters

int|string $tid

The term ID.

Returns

\Drupal\taxonomy\TermInterface|false —

The term, or false when the ID is not numeric or no term exists.

file_load()

file_load(int|string  $fid) : \Drupal\file\FileInterface|false

Loads a file entity by ID, as Drupal 7's file_load() did.

Parameters

int|string $fid

The file ID.

Returns

\Drupal\file\FileInterface|false —

The file entity, or false when none exists.

file_create_url()

file_create_url(mixed  $uri) : string|false

Returns an absolute URL for a file URI, as Drupal 7's file_create_url() did.

Uses the file_url_generator service, so Drupal 7's hook_file_url_alter() is not called.

Parameters

mixed $uri

The URI or path of the file.

Returns

string|false —

The absolute URL, or false when the stream wrapper is invalid.

drupal_get_token()

drupal_get_token(string  $value = '') : string

Returns a CSRF token for a value, as Drupal 7's drupal_get_token() did.

Delegates to the csrf_token service.

Parameters

string $value

An additional value to base the token on.

Returns

string —

The token.

drupal_valid_token()

drupal_valid_token(string  $token, string  $value = '', bool  $skip_anonymous = false) : bool

Validates a CSRF token, as Drupal 7's drupal_valid_token() did.

Parameters

string $token

The token to validate.

string $value

The additional value the token was based on.

bool $skip_anonymous

Whether to accept any token for anonymous users.

Returns

bool —

True for a valid token, or always true for anonymous users when $skip_anonymous is set.