function MediaLibraryState::fromRequest

Same name in other branches
  1. 9 core/modules/media_library/src/MediaLibraryState.php \Drupal\media_library\MediaLibraryState::fromRequest()
  2. 8.9.x core/modules/media_library/src/MediaLibraryState.php \Drupal\media_library\MediaLibraryState::fromRequest()
  3. 11.x core/modules/media_library/src/MediaLibraryState.php \Drupal\media_library\MediaLibraryState::fromRequest()

Get the media library state from a request.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

static A state object.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException Thrown when the hash query parameter is invalid.

5 calls to MediaLibraryState::fromRequest()
MediaLibrarySelectForm::updateWidget in core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php
Submit handler for the media library select form.
MediaLibraryStateTest::testFromRequest in core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php
Tests the hash validation when the state is created from a request.
MediaLibraryStateTest::testFromRequestQueryLess in core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php
@covers ::fromRequest
MediaLibraryUiBuilder::buildUi in core/modules/media_library/src/MediaLibraryUiBuilder.php
Build the media library UI.
MediaLibraryUiBuilder::checkAccess in core/modules/media_library/src/MediaLibraryUiBuilder.php
Check access to the media library.

File

core/modules/media_library/src/MediaLibraryState.php, line 97

Class

MediaLibraryState
A value object for the media library state.

Namespace

Drupal\media_library

Code

public static function fromRequest(Request $request) {
    $query = $request->query;
    // Create a MediaLibraryState object through the create method to make sure
    // all validation runs.
    $state = static::create($query->get('media_library_opener_id'), $query->all('media_library_allowed_types'), $query->get('media_library_selected_type'), $query->get('media_library_remaining'), $query->all('media_library_opener_parameters'));
    // The request parameters need to contain a valid hash to prevent a
    // malicious user modifying the query string to attempt to access
    // inaccessible information.
    if (!$state->isValidHash($query->get('hash'))) {
        throw new BadRequestHttpException("Invalid media library parameters specified.");
    }
    // @todo Review parameters passed and remove irrelevant ones in
    //   https://www.drupal.org/i/3396650
    // Once we have validated the required parameters, we restore the parameters
    // from the request since there might be additional values.
    $state->replace($query->all());
    return $state;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.