You are here

public static function MediaLibraryState::fromRequest in Drupal 9

Same name and namespace in other branches
  1. 8 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
    ->get('media_library_allowed_types', []), $query
    ->get('media_library_selected_type'), $query
    ->get('media_library_remaining'), $query
    ->get('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.");
  }

  // 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;
}