3 namespace Drupal\block_content\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\block_content\BlockContentTypeInterface;
8 use Drupal\Core\Extension\ThemeHandlerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\Request;
13 class BlockContentController extends ControllerBase {
16 * The custom block storage.
18 * @var \Drupal\Core\Entity\EntityStorageInterface
20 protected $blockContentStorage;
23 * The custom block type storage.
25 * @var \Drupal\Core\Entity\EntityStorageInterface
27 protected $blockContentTypeStorage;
32 * @var \Drupal\Core\Extension\ThemeHandlerInterface
34 protected $themeHandler;
39 public static function create(ContainerInterface $container) {
40 $entity_manager = $container->get('entity.manager');
42 $entity_manager->getStorage('block_content'),
43 $entity_manager->getStorage('block_content_type'),
44 $container->get('theme_handler')
49 * Constructs a BlockContent object.
51 * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_storage
52 * The custom block storage.
53 * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_type_storage
54 * The custom block type storage.
55 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
58 public function __construct(EntityStorageInterface $block_content_storage, EntityStorageInterface $block_content_type_storage, ThemeHandlerInterface $theme_handler) {
59 $this->blockContentStorage = $block_content_storage;
60 $this->blockContentTypeStorage = $block_content_type_storage;
61 $this->themeHandler = $theme_handler;
65 * Displays add custom block links for available types.
67 * @param \Symfony\Component\HttpFoundation\Request $request
68 * The current request object.
71 * A render array for a list of the custom block types that can be added or
72 * if there is only one custom block type defined for the site, the function
73 * returns the custom block add page for that custom block type.
75 public function add(Request $request) {
76 $types = $this->blockContentTypeStorage->loadMultiple();
77 if ($types && count($types) == 1) {
78 $type = reset($types);
79 return $this->addForm($type, $request);
81 if (count($types) === 0) {
83 '#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.', [
84 ':url' => Url::fromRoute('block_content.type_add')->toString(),
89 return ['#theme' => 'block_content_add_list', '#content' => $types];
93 * Presents the custom block creation form.
95 * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type
96 * The custom block type to add.
97 * @param \Symfony\Component\HttpFoundation\Request $request
98 * The current request object.
101 * A form array as expected by
102 * \Drupal\Core\Render\RendererInterface::render().
104 public function addForm(BlockContentTypeInterface $block_content_type, Request $request) {
105 $block = $this->blockContentStorage->create([
106 'type' => $block_content_type->id(),
108 if (($theme = $request->query->get('theme')) && in_array($theme, array_keys($this->themeHandler->listInfo()))) {
109 // We have navigated to this page from the block library and will keep track
110 // of the theme for redirecting the user to the configuration page for the
111 // newly created block in the given theme.
112 $block->setTheme($theme);
114 return $this->entityFormBuilder()->getForm($block);
118 * Provides the page title for this controller.
120 * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type
121 * The custom block type being added.
126 public function getAddFormTitle(BlockContentTypeInterface $block_content_type) {
127 return $this->t('Add %type custom block', ['%type' => $block_content_type->label()]);