Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. BlockContentController.php

function BlockContentController::add

Displays add content block links for available types.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

Return value

array A render array for a list of the block types that can be added or if there is only one block type defined for the site, the function returns the content block add page for that block type.

1 string reference to 'BlockContentController::add'
block_content.routing.yml in core/modules/block_content/block_content.routing.yml
core/modules/block_content/block_content.routing.yml

File

core/modules/block_content/src/Controller/BlockContentController.php, line 75

Class

BlockContentController

Namespace

Drupal\block_content\Controller

Code

public function add(Request $request) {
    // @todo deprecate see https://www.drupal.org/project/drupal/issues/3346394.
    $types = [];
    // Only use block types the user has access to.
    foreach ($this->blockContentTypeStorage
        ->loadMultiple() as $type) {
        $access = $this->entityTypeManager()
            ->getAccessControlHandler('block_content')
            ->createAccess($type->id(), NULL, [], TRUE);
        if ($access->isAllowed()) {
            $types[$type->id()] = $type;
        }
    }
    uasort($types, [
        $this->blockContentTypeStorage
            ->getEntityType()
            ->getClass(),
        'sort',
    ]);
    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,
    ];
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal