class MediaLibraryState extends ParameterBag implements CacheableDependencyInterface (View source)

A value object for the media library state.

When the media library is opened it needs several parameters to work properly. These parameters are normally extracted from the current URL, then retrieved from and managed by the MediaLibraryState value object. The following parameters are required in order to open the media library:

  • media_library_opener_id: The ID of a container service which implements \Drupal\media_library\MediaLibraryOpenerInterface and is responsible for interacting with the media library on behalf of the "thing" (e.g., a field widget or text editor button) which opened it.
  • media_library_allowed_types: The media types available in the library can be restricted to a list of allowed types. This should be an array of media type IDs.
  • media_library_selected_type: The media library contains tabs to navigate between the different media types. The selected type contains the ID of the media type whose tab that should be opened.
  • media_library_remaining: When the opener wants to limit the amount of media items that can be selected, it can pass the number of remaining slots. When the number of remaining slots is a negative number, an unlimited amount of items can be selected.

This object can also carry an optional opener-specific array of arbitrary values, under the media_library_opener_parameters key. These values are included in the hash generated by ::getHash(), so the end user cannot tamper with them either.

Methods

__construct(array $parameters = [])

{@inheritdoc}

create(string $opener_id, array $allowed_media_type_ids, string $selected_type_id, int $remaining_slots, array $opener_parameters = [])

Creates a new MediaLibraryState object.

fromRequest(Request $request)

Get the media library state from a request.

validateRequiredParameters(string $opener_id, array $allowed_media_type_ids, string $selected_type_id, int $remaining_slots)

Validates the required parameters for a new MediaLibraryState object.

string
getHash()

Get the hash for the state object.

string
isValidHash(string $hash)

Validate a hash for the state object.

string
getOpenerId()

Returns the ID of the media library opener service.

string[]
getAllowedTypeIds()

Returns the media type IDs which can be selected.

string
getSelectedTypeId()

Returns the selected media type.

bool
hasSlotsAvailable()

Determines if additional media items can be selected.

int
getAvailableSlots()

Returns the number of additional media items that can be selected.

array
getOpenerParameters()

Returns all opener-specific parameter values.

string[]
getCacheContexts()

The cache contexts associated with this object.

int
getCacheMaxAge()

The maximum age for which this object may be cached.

string[]
getCacheTags()

The cache tags associated with this object.

Details

__construct(array $parameters = [])

{@inheritdoc}

Parameters

array $parameters

static MediaLibraryState create(string $opener_id, array $allowed_media_type_ids, string $selected_type_id, int $remaining_slots, array $opener_parameters = [])

Creates a new MediaLibraryState object.

Parameters

string $opener_id

The opener ID.

array $allowed_media_type_ids

The allowed media type IDs.

string $selected_type_id

The selected media type ID.

int $remaining_slots

The number of remaining items the user is allowed to select or add in the library.

array $opener_parameters

(optional) Any additional opener-specific parameter values.

Return Value

MediaLibraryState

A state object.

static MediaLibraryState fromRequest(Request $request)

Get the media library state from a request.

Parameters

Request $request

The request.

Return Value

MediaLibraryState

A state object.

Exceptions

BadRequestHttpException

protected validateRequiredParameters(string $opener_id, array $allowed_media_type_ids, string $selected_type_id, int $remaining_slots)

Validates the required parameters for a new MediaLibraryState object.

Parameters

string $opener_id

The media library opener service ID.

array $allowed_media_type_ids

The allowed media type IDs.

string $selected_type_id

The selected media type ID.

int $remaining_slots

The number of remaining items the user is allowed to select or add in the library.

Exceptions

InvalidArgumentException

string getHash()

Get the hash for the state object.

Return Value

string

The hashed parameters.

string isValidHash(string $hash)

Validate a hash for the state object.

Parameters

string $hash

The hash to validate.

Return Value

string

The hashed parameters.

string getOpenerId()

Returns the ID of the media library opener service.

Return Value

string

The media library opener service ID.

string[] getAllowedTypeIds()

Returns the media type IDs which can be selected.

Return Value

string[]

The media type IDs.

string getSelectedTypeId()

Returns the selected media type.

Return Value

string

The selected media type.

bool hasSlotsAvailable()

Determines if additional media items can be selected.

Return Value

bool

TRUE if additional items can be selected, otherwise FALSE.

int getAvailableSlots()

Returns the number of additional media items that can be selected.

When the value is not available in the URL the default is 0. When a negative integer is passed, an unlimited amount of media items can be selected.

Return Value

int

The number of additional media items that can be selected.

array getOpenerParameters()

Returns all opener-specific parameter values.

Return Value

array

An associative array of all opener-specific parameter values.

string[] getCacheContexts()

The cache contexts associated with this object.

These identify a specific variation/representation of the object.

Cache contexts are tokens: placeholders that are converted to cache keys by the @cache_contexts_manager service. The replacement value depends on the request context (the current URL, language, and so on). They're converted before storing an object in cache.

Return Value

string[]

An array of cache context tokens, used to generate a cache ID.

int getCacheMaxAge()

The maximum age for which this object may be cached.

Return Value

int

The maximum time in seconds that this object may be cached.

string[] getCacheTags()

The cache tags associated with this object.

When this object is modified, these cache tags will be invalidated.

Return Value

string[]

A set of cache tags.