Skip to content

Utility

Provides various utility helpers.


  • Full name: \Drupal\helper\Utility

Methods

registerUniqueShutdownFunction

Registers a unique function call for execution on shutdown.

public static registerUniqueShutdownFunction(callable $callback, mixed $args): bool

Wrapper for drupal_register_shutdown_function() that does not add the function call if it already exists in the shutdown function stack.

  • This method is static. Parameters:
Parameter Type Description
$callback callable
The shutdown function to register.
$args mixed
Additional arguments to pass to the shutdown function.

Return Value:

TRUE if the function was added, or FALSE if it was already in the stack.

See Also:

  • \Drupal\helper\drupal_register_shutdown_function()

runBatch

Runs a batch even if another batch is currently running.

public static runBatch(array $batch): mixed

This is useful for running a batch inside another batch process.

  • This method is static. Parameters:
Parameter Type Description
$batch array
A batch array that would normally get passed to batch_set().

runSandboxBatch

Run a callback that requires a sandbox to keep track of progress.

public static runSandboxBatch(callable $callback, mixed $args): mixed

This is typically useful for testing update, post_update, or deploy hooks. This also makes it so your update hook can call another service or callback without having to worry about the sandbox.

  • This method is static. Parameters:
Parameter Type Description
$callback callable
The callback to run.
$args mixed
The arguments to pass to the callback.

Return Value:

The returned value from the callback.


execCommand

Runs a composer command.

public static execCommand(string $command, string $additional, mixed& $result_code = null): void

Note: USE THIS AT YOUR OWN RISK. YOU ARE RESPONSIBLE FOR ESCAPING THE INPUT TO THE $command PARAMETER.

  • This method is static. Parameters:
Parameter Type Description
$command string
The composer command to run, without the "composer" part.
$additional string
The additional command parameters and arguments.
$result_code mixed
The result code from running the command, modified by reference.

Throws:

  • RuntimeException

camelCase

Convert a string to CamelCase.

public static camelCase(string $string, bool $lower = false): string
  • This method is static. Parameters:
Parameter Type Description
$string string
The string.
$lower bool
If the first character should be lower case (i.e. lowerCamelCase).

Return Value:

The string.


matchString

Checks to see if a string matches a pattern.

public static matchString(string $value, string $pattern): bool
  • This method is static. Parameters:
Parameter Type Description
$value string
The string to check if it is a match.
$pattern string
The pattern to match against. Usually, the pattern will contain
wildcards like '?' and '*'.

Return Value:

If the string matches the pattern.


clamp

Clamp a number to between a minimum and maximum value.

public static clamp(int|float $num, int|float $min, int|float $max): int|float
  • This method is static. Parameters:
Parameter Type Description
$num int|float
The number value.
$min int|float
The minimum value allowed.
$max int|float
The maximum value allowed.

Return Value:

The clamped value.

See Also:

  • https://wiki.php.net/rfc/clamp

callableToString

Convert a callable to a string representation.

public static callableToString(callable|array|string $callable): string|null

The definition is resolved with the callable_resolver service first, so a 'service:method' definition returns the class of the service, such as 'Drupal\Component\Datetime\Time::getRequestTime'. Closures and other invokable objects return their class with '::__invoke'.

  • This method is static. Parameters:
Parameter Type Description
$callable callable|array|string
A callable definition.

Return Value:

The string representation of the callable, or NULL if the definition is not a valid callable.