function MediaLibraryUiBuilder::buildMediaTypeMenu

Same name in other branches
  1. 9 core/modules/media_library/src/MediaLibraryUiBuilder.php \Drupal\media_library\MediaLibraryUiBuilder::buildMediaTypeMenu()
  2. 10 core/modules/media_library/src/MediaLibraryUiBuilder.php \Drupal\media_library\MediaLibraryUiBuilder::buildMediaTypeMenu()
  3. 11.x core/modules/media_library/src/MediaLibraryUiBuilder.php \Drupal\media_library\MediaLibraryUiBuilder::buildMediaTypeMenu()

Get the media type menu for the media library.

Parameters

\Drupal\media_library\MediaLibraryState $state: The current state of the media library, derived from the current request.

Return value

array The render array for the media type menu.

1 call to MediaLibraryUiBuilder::buildMediaTypeMenu()
MediaLibraryUiBuilder::buildUi in core/modules/media_library/src/MediaLibraryUiBuilder.php
Build the media library UI.

File

core/modules/media_library/src/MediaLibraryUiBuilder.php, line 224

Class

MediaLibraryUiBuilder
Service which builds the media library.

Namespace

Drupal\media_library

Code

protected function buildMediaTypeMenu(MediaLibraryState $state) {
    // Add the menu for each type if we have more than 1 media type enabled for
    // the field.
    $allowed_type_ids = $state->getAllowedTypeIds();
    if (count($allowed_type_ids) <= 1) {
        return [];
    }
    // @todo: Add a class to the li element.
    //   https://www.drupal.org/project/drupal/issues/3029227
    $menu = [
        '#theme' => 'links__media_library_menu',
        '#links' => [],
        '#attributes' => [
            'class' => [
                'js-media-library-menu',
            ],
        ],
    ];
    $allowed_types = $this->entityTypeManager
        ->getStorage('media_type')
        ->loadMultiple($allowed_type_ids);
    $selected_type_id = $state->getSelectedTypeId();
    foreach ($allowed_types as $allowed_type_id => $allowed_type) {
        $link_state = MediaLibraryState::create($state->getOpenerId(), $state->getAllowedTypeIds(), $allowed_type_id, $state->getAvailableSlots(), $state->getOpenerParameters());
        // Add the 'media_library_content' parameter so the response will contain
        // only the updated content for the tab.
        // @see self::buildUi()
        $link_state->set('media_library_content', 1);
        $title = $allowed_type->label();
        $display_title = [
            '#markup' => $this->t('<span class="visually-hidden">Show </span>@title<span class="visually-hidden"> media</span>', [
                '@title' => $title,
            ]),
        ];
        if ($allowed_type_id === $selected_type_id) {
            $display_title = [
                '#markup' => $this->t('<span class="visually-hidden">Show </span>@title<span class="visually-hidden"> media</span><span class="active-tab visually-hidden"> (selected)</span>', [
                    '@title' => $title,
                ]),
            ];
        }
        $menu['#links']['media-library-menu-' . $allowed_type_id] = [
            'title' => $display_title,
            'url' => Url::fromRoute('media_library.ui', [], [
                'query' => $link_state->all(),
            ]),
            'attributes' => [
                'role' => 'button',
                'data-title' => $title,
            ],
        ];
    }
    // Set the active menu item.
    $menu['#links']['media-library-menu-' . $selected_type_id]['attributes']['class'][] = 'active';
    return $menu;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.