You are here

public function BlockContentPermissionsAddPageController::add in Block Content Permissions 8

Override.

Add create permission control over block content types.

Overrides BlockContentController::add

File

src/Controller/BlockContentPermissionsAddPageController.php, line 59

Class

BlockContentPermissionsAddPageController
Controller for the block content add page.

Namespace

Drupal\block_content_permissions\Controller

Code

public function add(Request $request) {
  $types = $this->blockContentTypeStorage
    ->loadMultiple();

  // Remove block content types based on create permissions.
  $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,
  ];
}