You are here

public function MediaBulkUploadController::addLinksList in Media Bulk Upload 8

Displays add links for the available bundles.

Redirects to the add form if there's only one bundle available.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse|array If there's only one available bundle, a redirect response. Otherwise, a render array with the add links for each bundle.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

1 string reference to 'MediaBulkUploadController::addLinksList'
media_bulk_upload.routing.yml in ./media_bulk_upload.routing.yml
media_bulk_upload.routing.yml

File

src/Controller/MediaBulkUploadController.php, line 70

Class

MediaBulkUploadController
Class MediaUploadController.

Namespace

Drupal\media_bulk_upload\Controller

Code

public function addLinksList() {
  $build = [
    '#theme' => 'entity_add_list',
    '#bundles' => [],
  ];
  $entity_type = $this->entityTypeManager
    ->getDefinition('media_bulk_config');
  $entity_type_label = $entity_type
    ->getSingularLabel();
  $build['#cache']['tags'] = $entity_type
    ->getListCacheTags();
  $link_text = $this
    ->t('Add a new @entity_type.', [
    '@entity_type' => $entity_type_label,
  ]);
  $link_route_name = 'entity.' . $entity_type
    ->id() . '.add_form';
  $build['#add_bundle_message'] = $this
    ->t('There is no @entity_type yet. @add_link', [
    '@entity_type' => $entity_type_label,
    '@add_link' => Link::createFromRoute($link_text, $link_route_name)
      ->toString(),
  ]);
  $mediaBulkConfigStorage = $this->entityTypeManager
    ->getStorage('media_bulk_config');
  $mediaBulkConfigEntities = $mediaBulkConfigStorage
    ->loadMultiple();
  $form_route_name = 'media_bulk_upload.upload_form';
  if (count($mediaBulkConfigEntities) == 1) {
    $mediaBulkConfigEntity = reset($mediaBulkConfigEntities);
    return $this
      ->redirect($form_route_name, [
      'media_bulk_config' => $mediaBulkConfigEntity
        ->id(),
    ]);
  }
  foreach ($mediaBulkConfigEntities as $mediaBulkConfigEntity) {
    $link = Link::createFromRoute($mediaBulkConfigEntity
      ->label(), $form_route_name, [
      'media_bulk_config' => $mediaBulkConfigEntity
        ->id(),
    ]);
    if (!$link
      ->getUrl()
      ->access()) {
      continue;
    }
    $build['#bundles'][$mediaBulkConfigEntity
      ->id()] = [
      'label' => $mediaBulkConfigEntity
        ->label(),
      'description' => '',
      'add_link' => Link::createFromRoute($mediaBulkConfigEntity
        ->label(), $form_route_name, [
        'media_bulk_config' => $mediaBulkConfigEntity
          ->id(),
      ]),
    ];
  }
  return $build;
}