You are here

class GutenbergMediaLibraryUiBuilder in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 src/GutenbergMediaLibraryUiBuilder.php \Drupal\gutenberg\GutenbergMediaLibraryUiBuilder

Class GutenbergMediaLibraryUiBuilder.

This is temporary fix for the issue that causes problem with file upload on initial media library dialog render.

@package Drupal\gutenberg

Hierarchy

Expanded class hierarchy of GutenbergMediaLibraryUiBuilder

1 string reference to 'GutenbergMediaLibraryUiBuilder'
gutenberg.services.yml in ./gutenberg.services.yml
gutenberg.services.yml
1 service uses GutenbergMediaLibraryUiBuilder
gutenberg.media_library.ui_builder in ./gutenberg.services.yml
Drupal\gutenberg\GutenbergMediaLibraryUiBuilder

File

src/GutenbergMediaLibraryUiBuilder.php, line 17

Namespace

Drupal\gutenberg
View source
class GutenbergMediaLibraryUiBuilder extends MediaLibraryUiBuilder {

  /**
   * {@inheritDoc}
   */
  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();

    // @todo: this is temporary fix that allows for rendering at least one tab
    // to make it clickable on frontend side by Gutenberg.
    if (!$allowed_type_ids) {
      return [];
    }

    // @todo: Add a class to the li element.
    // https://www.drupal.org/project/drupal/issues/3029227
    $menu = [
      '#theme' => 'links',
      '#links' => [],
      '#attributes' => [
        'class' => [
          'media-library-menu',
          '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' => [
          'class' => [
            'media-library-menu__link',
          ],
          'role' => 'button',
          'data-title' => $title,
        ],
      ];
    }

    // Set the active menu item.
    $menu['#links']['media-library-menu-' . $selected_type_id]['attributes']['class'][] = 'active';
    return $menu;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GutenbergMediaLibraryUiBuilder::buildMediaTypeMenu protected function Get the media type menu for the media library. Overrides MediaLibraryUiBuilder::buildMediaTypeMenu
MediaLibraryUiBuilder::$entityTypeManager protected property The entity type manager.
MediaLibraryUiBuilder::$formBuilder protected property The form builder.
MediaLibraryUiBuilder::$openerResolver protected property The media library opener resolver.
MediaLibraryUiBuilder::$request protected property The currently active request object.
MediaLibraryUiBuilder::$viewsExecutableFactory protected property The views executable factory.
MediaLibraryUiBuilder::buildLibraryContent protected function Build the media library content area.
MediaLibraryUiBuilder::buildMediaLibraryView protected function Get the media library view.
MediaLibraryUiBuilder::buildMediaTypeAddForm protected function Get the add form for the selected media type.
MediaLibraryUiBuilder::buildUi public function Build the media library UI.
MediaLibraryUiBuilder::checkAccess public function Check access to the media library.
MediaLibraryUiBuilder::dialogOptions public static function Get media library dialog options.
MediaLibraryUiBuilder::__construct public function Constructs a MediaLibraryUiBuilder instance.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.