// Create a new media library URL with the correct state parameters.
$selected_type_id =
reset($allowed_media_type_ids);
$remaining =
$cardinality_unlimited ? FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED :
$remaining;
// This particular media library opener needs some extra metadata for its
// \Drupal\media_library\MediaLibraryOpenerInterface::getSelectionResponse()
// to be able to target the element whose 'data-media-library-widget-value'
// attribute is the same as $field_widget_id. The entity ID, entity type ID,
// bundle, field name are used for access checking.
$entity =
$items->
getEntity();
$opener_parameters =
[ 'field_widget_id' =>
$field_widget_id,
'entity_type_id' =>
$entity->
getEntityTypeId(),
'bundle' =>
$entity->
bundle(),
'field_name' =>
$field_name,
];
// Only add the entity ID when we actually have one. The entity ID needs to
// be a string to ensure that the media library state generates its
// tamper-proof hash in a consistent way.
if (!
$entity->
isNew()) { $opener_parameters['entity_id'
] =
(string) $entity->
id();
if ($entity->
getEntityType()->
isRevisionable()) { $opener_parameters['revision_id'
] =
(string) $entity->
getRevisionId();
}