MediaBulkUploadController.php in Media Bulk Upload 8
File
src/Controller/MediaBulkUploadController.php
View source
<?php
namespace Drupal\media_bulk_upload\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\media_bulk_upload\Entity\MediaBulkConfigInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaBulkUploadController extends ControllerBase {
protected $entityTypeManager;
protected $renderer;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) {
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('renderer'));
}
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;
}
public function accessList(AccountInterface $account) {
if ($account
->hasPermission('administer media_bulk_upload configuration')) {
return AccessResult::allowed();
}
$mediaBulkConfigStorage = $this->entityTypeManager
->getStorage('media_bulk_config');
$mediaBulkConfigEntities = $mediaBulkConfigStorage
->loadMultiple();
foreach ($mediaBulkConfigEntities as $mediaBulkConfig) {
$url = Url::fromRoute('media_bulk_upload.upload_form', [
'media_bulk_config' => $mediaBulkConfig
->id(),
]);
if ($url
->access()) {
return AccessResult::allowed();
}
}
return AccessResult::forbidden('No media bulk config entity accessible for the user.');
}
public function accessForm(AccountInterface $account, MediaBulkConfigInterface $media_bulk_config) {
$mediaBulkConfigId = $media_bulk_config
->id();
if (!$account
->hasPermission("use {$mediaBulkConfigId} bulk upload form")) {
return AccessResult::forbidden('Media Bulk Upload form is not accessible for the user.');
}
return AccessResult::allowed();
}
}