See #2994699-71: Create a CKEditor plugin to select and embed a media item from the Media Library

When using the icon to open Media Library, there's a visual bug in that Image is selected by default (+1) but Audio is also highlighted, causing a visual regression:

focus on wrong element

@phenaproxima explained that this is because there's code somewhere for accessibility reasons to automatically select the first item in a list. That's good, except that because Image isn't the first thing in the list, it's selecting both.

A workaround was introduced for this in #2994699: Create a CKEditor plugin to select and embed a media item from the Media Library, see :
\Drupal\media_library\Plugin\CKEditorPlugin\DrupalMediaLibrary::getConfig

    if (in_array('image', $media_type_ids, TRUE)) {
      // Due to a bug where the active item styling and the focus styling
      // create the visual appearance of two active items, we'll move
      // the 'image' media type to first position, so that the focused item and
      // the active item are the same.
      // This workaround can be removed once this issue is fixed:
      // @see https://www.drupal.org/project/drupal/issues/3073799
      array_unshift($media_type_ids, 'image');
      $media_type_ids = array_unique($media_type_ids);
    }

So this behaviour can only be seen if the workaround is removed.

Comments

oknate created an issue. See original summary.

phenaproxima’s picture

Title: [PP-1] Fix visual bug in media library where focus applied to non active tab » Fix visual bug in media library where focus applied to non active tab

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

andrewmacpherson’s picture

Title: Fix visual bug in media library where focus applied to non active tab » Fix visual bug in media library where active-tab highlight applied to non active tab

Updating title. Clarifying that this bug is about the highlight used to indicate a selected tab. The term "focus" gets misused, or is kind-of overloaded.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

phenaproxima’s picture

bbu23’s picture

Hello, I am not able to reproduce the behavior from the screenshot. Could you please share more details of what you are doing to get this result? Thx

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lendude’s picture

Issue summary: View changes

Updated the IS with notes to the existing workaround that prevents you from reproducing this behaviour currently.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jaykumar95’s picture

This issue is not reproduced in Claro 9.4.1 theme.
landing from issue https://www.drupal.org/project/drupal/issues/3303889#comment-14655475 to here.

I have found that in patch #91
from https://www.drupal.org/project/drupal/issues/2994699#comment-13214946

array_unshift($media_type_ids, 'image');
$media_type_ids = array_unique($media_type_ids);

the above code prepends the image at first position but as it is an associative array the first pair becomes 0 => "image" instead of "image" => "image"
so to fix it we can use the below code

unset($media_type_ids['image']);
$media_type_ids = ['image' => 'image'] + $media_type_ids;

as I said earlier this issue is not coming in Claro theme we can remove this code block.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bbu23’s picture

Actually this bug is quite interesting. I have the following scenario on CLARO theme:

On two environments, let's call them:
- LOCAL
- REMOTE

The environments have the same codebase. One of them has the bug, the other one doesn't if we use the non-zero key:

e.g.

unset($media_type_ids['image']);
$media_type_ids = ['image' => 'image'] + $media_type_ids;

What I noticed in REMOTE environment is that the media_library_allowed_types array parameter that is sent to MediaLibraryState gets its keys reordered alphabetically. In MediaLibraryState in construct, even though the parameters are set in the correct order just before, they arrive sorted by keys alphabetically. In the LOCAL environment, the order is kept.

If array_unshift($media_type_ids, 'image'); is used in both environments, then the order is not altered anymore because the key 0 will be in the top of the array no matter what.

On the remote environment, I changed the image key to be

unset($media_type_ids['image']);
$media_type_ids = ['aaimage' => 'image'] + $media_type_ids;

and then

unset($media_type_ids['image']);
$media_type_ids = ['bbimage' => 'image'] + $media_type_ids;

which showed that the parameters are sorted by keys and placed the Image tab first for the first case, and second for the second case.

This concludes that it is not related to the Claro theme, but I don't know if it's a server issue, or anything else. But since the LOCAL parameters are not sorted at all when they arrive in the MediaLibraryState construct for the exact same code, it could be something else outside of Drupal.

If this is not controllable from Drupal, maybe it would be better to send the parameters to MediaLibraryState with numeric keys instead of alpha keys. This will definitely not require the "image" fix that we currently have. (unless they are required later in the code). E.g.

$state = MediaLibraryState::create(
  'media_library.opener.editor',
  array_values($media_type_ids), // Numeric keys here.
  reset($media_type_ids),
  1,
  ['filter_format_id' => $editor->getFilterFormat()->id()],
);

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.