- Issue created by @claudiu.cristea
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...
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.
None.
None.
New event to subscribe to OR new hook.
None.
Third-party are able to add new parameters to the media library dialog opener by... (to be decided)
Active
11.0 π₯
Last updated
Enhances developer experience.