- Issue created by @tondeuse
I was very glad to find the edit media modal module, as in the course of a D7 to D10 migration, we realized that it was no longer possible to edit media data once it was inserted to a long text wysywig text field. The only flaw that keeps me from using this module is that it does not consider the language context when loading the media edit form in the modal.
Use the language manager to generate a language context before generating the URl of the media-edit-form.
Here is my code locally, inside file EditMediaModalController.php, it looks almost too simple.
namespace Drupal\edit_media_modal\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Redirect to the media edit route by UUID.
*/
class EditMediaModalController extends ControllerBase {
[ ... ... ... ]
/**
* Get the edit media url by the UUID.
*
* At the time where the CKEditor5 plugin wants to open the edit modal it
* only has access to the UUID of the media entity, not the ID.
*
* @param string $uuid
* The media uuid.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The ID of the entity if found.
*/
public function getEditUrl(string $uuid) {
$entity = $this->entityRepository->loadEntityByUuid('media', $uuid);
$language = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
$options = [
'language' => $language,
];
$url = $entity->toUrl('edit-form', $options);
$url->setOption('query', ['edit_media_in_modal' => TRUE]);
return new JsonResponse(['url' => $url->toString()]);
}
[ ... ... ... ]
}
Test this further. Is this the best way to generate and provide the language context? Does this cover all use cases?
Active
2.0
Code