class StringDatabaseStorage implements StringStorageInterface (View source)

Defines a class to store localized strings in the database.

Properties

protected Connection $connection

The database connection.

protected array $options

Additional database connection options to use in queries.

Methods

__construct(Connection $connection, array $options = [])

Constructs a new StringDatabaseStorage class.

array
getStrings(array $conditions = [], array $options = [])

Loads multiple source string objects.

getTranslations(array $conditions = [], array $options = [])

Loads multiple string translation objects.

findString(array $conditions)

Loads a string source object, fast query.

findTranslation(array $conditions)

Loads a string translation object, fast query.

getLocations(array $conditions = [])

Loads string location information.

int
countStrings()

Counts source strings.

array
countTranslations()

Counts translations.

$this
save(StringInterface $string)

Save string object to storage.

updateLocation(StringInterface $string)

Update locations for string.

checkVersion(StringInterface $string, string $version)

Checks whether the string version matches a given version, fix it if not.

$this
delete(StringInterface $string)

Delete string from storage.

deleteStrings(array $conditions)

Deletes source strings and translations using conditions.

deleteTranslations(array $conditions)

Deletes translations using conditions.

createString(array $values = [])

Creates a source string object bound to this storage but not saved.

createTranslation(array $values = [])

Creates a string translation object bound to this storage but not saved.

string
dbFieldTable(string $field)

Gets table alias for field.

string
dbStringTable(StringInterface $string)

Gets table name for storing string object.

array
dbStringKeys(StringInterface $string)

Gets keys values that are in a database table.

dbStringLoad(array $conditions, array $options, string $class)

Loads multiple string objects.

dbStringSelect(array $conditions, array $options = [])

Builds a SELECT query with multiple conditions and fields.

bool|int
dbStringInsert(StringInterface $string)

Creates a database record for a string object.

bool|int
dbStringUpdate(StringInterface $string)

Updates string object in the database.

dbDelete(string $table, array $keys)

Creates delete query.

dbExecute($query, array $args = [])

Executes an arbitrary SELECT query string with the injected options.

Details

__construct(Connection $connection, array $options = [])

Constructs a new StringDatabaseStorage class.

Parameters

Connection $connection

A Database connection to use for reading and writing configuration data.

array $options

(optional) Any additional database connection options to use in queries.

array getStrings(array $conditions = [], array $options = [])

Loads multiple source string objects.

Parameters

array $conditions

(optional) Array with conditions that will be used to filter the strings returned and may include any of the following elements:

  • Any simple field value indexed by field name.
  • 'translated', TRUE to get only translated strings or FALSE to get only untranslated strings. If not set it returns both translated and untranslated strings that fit the other conditions. Defaults to no conditions which means that it will load all strings.
array $options

(optional) An associative array of additional options. It may contain any of the following optional keys:

  • 'filters': Array of string filters indexed by field name.
  • 'pager limit': Use pager and set this limit value.

Return Value

array

Array of \Drupal\locale\StringInterface objects matching the conditions.

StringInterface[] getTranslations(array $conditions = [], array $options = [])

Loads multiple string translation objects.

Parameters

array $conditions

(optional) Array with conditions that will be used to filter the strings returned and may include all of the conditions defined by getStrings().

array $options

(optional) An associative array of additional options. It may contain any of the options defined by getStrings().

Return Value

StringInterface[]

Array of \Drupal\locale\StringInterface objects matching the conditions.

SourceString|null findString(array $conditions)

Loads a string source object, fast query.

These 'fast query' methods are the ones in the critical path and their implementation must be optimized for speed, as they may run many times in a single page request.

Parameters

array $conditions

(optional) Array with conditions that will be used to filter the strings returned and may include all of the conditions defined by getStrings().

Return Value

SourceString|null

Minimal TranslationString object if found, NULL otherwise.

TranslationString|null findTranslation(array $conditions)

Loads a string translation object, fast query.

This function must only be used when actually translating strings as it will have the effect of updating the string version. For other purposes the getTranslations() method should be used instead.

Parameters

array $conditions

(optional) Array with conditions that will be used to filter the strings returned and may include all of the conditions defined by getStrings().

Return Value

TranslationString|null

Minimal TranslationString object if found, NULL otherwise.

StringInterface[] getLocations(array $conditions = [])

Loads string location information.

Parameters

array $conditions

(optional) Array with conditions to filter the locations that may be any of the following elements:

  • 'sid', The string identifier.
  • 'type', The location type.
  • 'name', The location name.

Return Value

StringInterface[]

Array of \Drupal\locale\StringInterface objects matching the conditions.

int countStrings()

Counts source strings.

Return Value

int

The number of source strings contained in the storage.

array countTranslations()

Counts translations.

Return Value

array

The number of translations for each language indexed by language code.

$this save(StringInterface $string)

Save string object to storage.

Parameters

StringInterface $string

The string object.

Return Value

$this

The called object.

Exceptions

StringStorageException

protected updateLocation(StringInterface $string)

Update locations for string.

Parameters

StringInterface $string

The string object.

protected checkVersion(StringInterface $string, string $version)

Checks whether the string version matches a given version, fix it if not.

Parameters

StringInterface $string

The string object.

string $version

Drupal version to check against.

$this delete(StringInterface $string)

Delete string from storage.

Parameters

StringInterface $string

The string object.

Return Value

$this

The called object.

Exceptions

StringStorageException

deleteStrings(array $conditions)

Deletes source strings and translations using conditions.

Parameters

array $conditions

Array with simple field conditions for source strings.

deleteTranslations(array $conditions)

Deletes translations using conditions.

Parameters

array $conditions

Array with simple field conditions for string translations.

SourceString createString(array $values = [])

Creates a source string object bound to this storage but not saved.

Parameters

array $values

(optional) Array with initial values. Defaults to empty array.

Return Value

SourceString

New source string object.

TranslationString createTranslation(array $values = [])

Creates a string translation object bound to this storage but not saved.

Parameters

array $values

(optional) Array with initial values. Defaults to empty array.

Return Value

TranslationString

New string translation object.

protected string dbFieldTable(string $field)

Gets table alias for field.

Parameters

string $field

One of the field names of the locales_source, locates_location, locales_target tables to find the table alias for.

Return Value

string

One of the following values:

  • 's' for "source", "context", "version" (locales_source table fields).
  • 'l' for "type", "name" (locales_location table fields)
  • 't' for "language", "translation", "customized" (locales_target table fields)

protected string dbStringTable(StringInterface $string)

Gets table name for storing string object.

Parameters

StringInterface $string

The string object.

Return Value

string

The table name.

protected array dbStringKeys(StringInterface $string)

Gets keys values that are in a database table.

Parameters

StringInterface $string

The string object.

Return Value

array

Array with key fields if the string has all keys, or empty array if not.

protected StringInterface[] dbStringLoad(array $conditions, array $options, string $class)

Loads multiple string objects.

Parameters

array $conditions

Any of the conditions used by dbStringSelect().

array $options

Any of the options used by dbStringSelect().

string $class

Class name to use for fetching returned objects.

Return Value

StringInterface[]

Array of objects of the class requested.

protected Select dbStringSelect(array $conditions, array $options = [])

Builds a SELECT query with multiple conditions and fields.

The query uses both 'locales_source' and 'locales_target' tables. Note that by default, as we are selecting both translated and untranslated strings target field's conditions will be modified to match NULL rows too.

Parameters

array $conditions

An associative array with field => value conditions that may include NULL values. If a language condition is included it will be used for joining the 'locales_target' table.

array $options

An associative array of additional options. It may contain any of the options used by Drupal\locale\StringStorageInterface::getStrings() and these additional ones:

  • 'translation', Whether to include translation fields too. Defaults to FALSE.

Return Value

Select

Query object with all the tables, fields and conditions.

protected bool|int dbStringInsert(StringInterface $string)

Creates a database record for a string object.

Parameters

StringInterface $string

The string object.

Return Value

bool|int

If the operation failed, returns FALSE. If it succeeded returns the last insert ID of the query, if one exists.

Exceptions

StringStorageException

protected bool|int dbStringUpdate(StringInterface $string)

Updates string object in the database.

Parameters

StringInterface $string

The string object.

Return Value

bool|int

If the record update failed, returns FALSE. If it succeeded, returns SAVED_NEW or SAVED_UPDATED.

Exceptions

StringStorageException

protected Delete dbDelete(string $table, array $keys)

Creates delete query.

Parameters

string $table

The table name.

array $keys

Array with object keys indexed by field name.

Return Value

Delete

Returns a new Delete object for the injected database connection.

protected dbExecute($query, array $args = [])

Executes an arbitrary SELECT query string with the injected options.

Parameters

$query
array $args