comparison core/modules/block_content/src/Controller/BlockContentController.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children c2387f117808
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\block_content\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\block_content\BlockContentTypeInterface;
8 use Drupal\Core\Extension\ThemeHandlerInterface;
9 use Drupal\Core\Url;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\Request;
12
13 class BlockContentController extends ControllerBase {
14
15 /**
16 * The custom block storage.
17 *
18 * @var \Drupal\Core\Entity\EntityStorageInterface
19 */
20 protected $blockContentStorage;
21
22 /**
23 * The custom block type storage.
24 *
25 * @var \Drupal\Core\Entity\EntityStorageInterface
26 */
27 protected $blockContentTypeStorage;
28
29 /**
30 * The theme handler.
31 *
32 * @var \Drupal\Core\Extension\ThemeHandlerInterface
33 */
34 protected $themeHandler;
35
36 /**
37 * {@inheritdoc}
38 */
39 public static function create(ContainerInterface $container) {
40 $entity_manager = $container->get('entity.manager');
41 return new static(
42 $entity_manager->getStorage('block_content'),
43 $entity_manager->getStorage('block_content_type'),
44 $container->get('theme_handler')
45 );
46 }
47
48 /**
49 * Constructs a BlockContent object.
50 *
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
56 * The theme handler.
57 */
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;
62 }
63
64 /**
65 * Displays add custom block links for available types.
66 *
67 * @param \Symfony\Component\HttpFoundation\Request $request
68 * The current request object.
69 *
70 * @return array
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.
74 */
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);
80 }
81 if (count($types) === 0) {
82 return [
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(),
85 ]),
86 ];
87 }
88
89 return ['#theme' => 'block_content_add_list', '#content' => $types];
90 }
91
92 /**
93 * Presents the custom block creation form.
94 *
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.
99 *
100 * @return array
101 * A form array as expected by drupal_render().
102 */
103 public function addForm(BlockContentTypeInterface $block_content_type, Request $request) {
104 $block = $this->blockContentStorage->create([
105 'type' => $block_content_type->id()
106 ]);
107 if (($theme = $request->query->get('theme')) && in_array($theme, array_keys($this->themeHandler->listInfo()))) {
108 // We have navigated to this page from the block library and will keep track
109 // of the theme for redirecting the user to the configuration page for the
110 // newly created block in the given theme.
111 $block->setTheme($theme);
112 }
113 return $this->entityFormBuilder()->getForm($block);
114 }
115
116 /**
117 * Provides the page title for this controller.
118 *
119 * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type
120 * The custom block type being added.
121 *
122 * @return string
123 * The page title.
124 */
125 public function getAddFormTitle(BlockContentTypeInterface $block_content_type) {
126 return $this->t('Add %type custom block', ['%type' => $block_content_type->label()]);
127 }
128
129 }