Make the module language aware

Created on 27 September 2024, 3 months ago

Make the module language aware

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.

Steps to reproduce

  • Install a site with 2 languages
  • Enable translation for the image media type and a node type
  • Configure a CkEditor profile to enable wysiwyg image insertion
  • Create an image and translate it, making sure you have at least one translatable text field (title, description or the like)
  • Create a node in the default language, insert the image in the wysywig
  • Translate the node and edit the image in place, using the modal provided by this module, save the node
  • Go back to the default language node you created before
  • Pop the image in the modal, the data you have saved in the the translatable image text field from the other language context is now the data you are looking at in the default language, you overwrote default language data from the non-default langauge.

Proposed resolution

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()]);
  }

 [ ... ... ... ]

}

Remaining tasks

Test this further. Is this the best way to generate and provide the language context? Does this cover all use cases?

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇨🇦Canada tondeuse

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

Comments & Activities

Production build 0.71.5 2024