BlockContentController.php

Same filename and directory in other branches
  1. 11.x core/modules/block_content/src/Controller/BlockContentController.php
  2. 10 core/modules/block_content/src/Controller/BlockContentController.php
  3. 9 core/modules/block_content/src/Controller/BlockContentController.php
  4. 8.9.x core/modules/block_content/src/Controller/BlockContentController.php

Namespace

Drupal\block_content\Controller

File

core/modules/block_content/src/Controller/BlockContentController.php

View source
<?php

namespace Drupal\block_content\Controller;

use Drupal\block_content\BlockContentTypeInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Routing\Attribute\Route;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Controller routines for custom block routes.
 */
class BlockContentController extends ControllerBase {
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $entity_type_manager = $container->get('entity_type.manager');
    return new static($entity_type_manager->getStorage('block_content'), $entity_type_manager->getStorage('block_content_type'), $container->get('theme_handler'));
  }
  
  /**
   * Constructs a BlockContent object.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $blockContentStorage
   *   The content block storage.
   * @param \Drupal\Core\Entity\EntityStorageInterface $blockContentTypeStorage
   *   The block type storage.
   * @param \Drupal\Core\Extension\ThemeHandlerInterface $themeHandler
   *   The theme handler.
   */
  public function __construct(protected EntityStorageInterface $blockContentStorage, protected EntityStorageInterface $blockContentTypeStorage, protected ThemeHandlerInterface $themeHandler) {
  }
  
  /**
   * Presents the content block creation form.
   *
   * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type
   *   The block type to add.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object.
   *
   * @return array
   *   A form array as expected by
   *   \Drupal\Core\Render\RendererInterface::render().
   */
  public function addForm(BlockContentTypeInterface $block_content_type, Request $request) {
    $block = $this->blockContentStorage
      ->create([
      'type' => $block_content_type->id(),
    ]);
    if (($theme = $request->query
      ->get('theme')) && in_array($theme, array_keys($this->themeHandler
      ->listInfo()))) {
      // We have navigated to this page from the block library and will keep
      // track of the theme for redirecting the user to the configuration page
      // for the newly created block in the given theme.
      $block->setTheme($theme);
    }
    return $this->entityFormBuilder()
      ->getForm($block);
  }
  
  /**
   * Provides the page title for this controller.
   *
   * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type
   *   The block type being added.
   *
   * @return string
   *   The page title.
   *
   * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use the
   *   title closure on the route attribute instead.
   *
   * @see https://www.drupal.org/node/3614321
   */
  public function getAddFormTitle(BlockContentTypeInterface $block_content_type) {
    @trigger_error('The ' . __METHOD__ . ' method is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use the title closure on the route attribute instead. See https://www.drupal.org/node/3614321', E_USER_DEPRECATED);
    return $this->t('Add %type content block', [
      '%type' => $block_content_type->label(),
    ]);
  }

}

Classes

Title Deprecated Summary
BlockContentController Controller routines for custom block routes.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.