BlockContentPermissionsAddPageController.php in Block Content Permissions 8
File
src/Controller/BlockContentPermissionsAddPageController.php
View source
<?php
namespace Drupal\block_content_permissions\Controller;
use Drupal\block_content\Controller\BlockContentController;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class BlockContentPermissionsAddPageController extends BlockContentController {
protected $account;
public static function create(ContainerInterface $container) {
$entity_manager = $container
->get('entity_type.manager');
return new static($entity_manager
->getStorage('block_content'), $entity_manager
->getStorage('block_content_type'), $container
->get('theme_handler'), $container
->get('current_user'));
}
public function __construct(EntityStorageInterface $block_content_storage, EntityStorageInterface $block_content_type_storage, ThemeHandlerInterface $theme_handler, AccountInterface $account) {
$this->blockContentStorage = $block_content_storage;
$this->blockContentTypeStorage = $block_content_type_storage;
$this->themeHandler = $theme_handler;
$this->account = $account;
}
public function add(Request $request) {
$types = $this->blockContentTypeStorage
->loadMultiple();
$account = $this->account;
foreach ($types as $bundle_type => $bundle_obj) {
if (!$account
->hasPermission("create {$bundle_type} block content")) {
unset($types[$bundle_type]);
}
}
if ($types && count($types) == 1) {
$type = reset($types);
return $this
->addForm($type, $request);
}
if (count($types) === 0) {
return [
'#markup' => $this
->t('You have not created any block types yet. Go to the <a href=":url">block type creation page</a> to add a new block type.', [
':url' => Url::fromRoute('block_content.type_add')
->toString(),
]),
];
}
return [
'#theme' => 'block_content_add_list',
'#content' => $types,
];
}
}