Allow 3rd party to easily add new params to media_library_opener_parameters

Created on 19 June 2023, over 1 year ago

Problem/Motivation

See also https://drupal.slack.com/archives/C1VLTJJD8/p1682328304392049

The MediaLibraryState value object docblock states:

This object can also carry an optional opener-specific array of arbitrary values, under the media_library_opener_parameters

. However, for a third-party to add such parameters it seems not to be a straight task. For instance, on media fields a 3rd-party should alter the widget:

function mymodule_field_widget_media_library_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  if (isset($element['open_button'], $element['open_button']['#media_library_state'])) {
    /** @var \Drupal\media_library\MediaLibraryState $state */
    $state = $element['open_button']['#media_library_state'];
    $parameters = $state->getOpenerParameters();
    $parameters['foo'] = 'bar';
    $state->set('media_library_opener_parameters', $parameters);
    // Update the validation hash.
    $state->set('hash', $state->getHash());
  }
}

But if we open the library from CKEditor, we, additionally, need also to swap the plugin class:

/**
 * Overrides the "drupalmedialibrary" plugin.
 */
class DrupalMediaLibrary extends OriginalDrupalMediaLibrary {

  /**
   * {@inheritdoc}
   */
  public function getConfig(Editor $editor) {
    $config = parent::getConfig($editor);
    if (!empty($config['DrupalMediaLibrary_url'])) {
      $request = Request::create($config['DrupalMediaLibrary_url']);
      $state = MediaLibraryState::fromRequest($request);
      // Media library view and display not configurable, set display_id.
      // @see https://www.drupal.org/project/drupal/issues/2971209
      $state->set('views_display_id', 'widget_table');
      // Add custom parameter.
      $parameters = $state->getOpenerParameters();
      $parameters['foo'] = 'bar';
      $state->set('media_library_opener_parameters', $parameters);
      // Update the validation hash.
      $state->set('hash', $state->getHash());
      $config['DrupalMediaLibrary_url'] = Url::fromRoute('media_library.ui')
        ->setOption('query', $state->all())
        ->toString(TRUE)
        ->getGeneratedUrl();
    }
    return $config;
  }
}

This doesn't seem very DX friendly because if an arbitrary way to open the media dialog is added, then needs a new hack.

But we can do better...

Proposed resolution

Dispatch an event or invoke a hook from MediaLibraryState::__construct() just before calling the parent's constructor to allow 3rd-party to alter the media_library_opener_parameters parameters.

Remaining tasks

None.

User interface changes

None.

API changes

New event to subscribe to OR new hook.

Data model changes

None.

Release notes snippet

Third-party are able to add new parameters to the media library dialog opener by... (to be decided)

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
MediaΒ  β†’

Last updated 4 days ago

Created by

πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024