MediaLibraryState
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
{@inheritdoc}
Creates a new MediaLibraryState object.
Get the media library state from a request.
Validates the required parameters for a new MediaLibraryState object.
Get the hash for the state object.
Validate a hash for the state object.
Returns the ID of the media library opener service.
Returns the media type IDs which can be selected.
Returns the selected media type.
Determines if additional media items can be selected.
Returns the number of additional media items that can be selected.
Returns all opener-specific parameter values.
The cache contexts associated with this object.
The maximum age for which this object may be cached.
The cache tags associated with this object.
Details
__construct(array $parameters = [])
{@inheritdoc}
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.
static MediaLibraryState
fromRequest(Request $request)
Get the media library state from a request.
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.
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.
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.
array
getOpenerParameters()
Returns 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.
int
getCacheMaxAge()
The maximum age for which 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.