Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\block_content\Controller;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Controller\ControllerBase;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
7 use Drupal\block_content\BlockContentTypeInterface;
|
Chris@0
|
8 use Drupal\Core\Extension\ThemeHandlerInterface;
|
Chris@0
|
9 use Drupal\Core\Url;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
11 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
12
|
Chris@0
|
13 class BlockContentController extends ControllerBase {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * The custom block storage.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $blockContentStorage;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The custom block type storage.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $blockContentTypeStorage;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The theme handler.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var \Drupal\Core\Extension\ThemeHandlerInterface
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $themeHandler;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * {@inheritdoc}
|
Chris@0
|
38 */
|
Chris@0
|
39 public static function create(ContainerInterface $container) {
|
Chris@0
|
40 $entity_manager = $container->get('entity.manager');
|
Chris@0
|
41 return new static(
|
Chris@0
|
42 $entity_manager->getStorage('block_content'),
|
Chris@0
|
43 $entity_manager->getStorage('block_content_type'),
|
Chris@0
|
44 $container->get('theme_handler')
|
Chris@0
|
45 );
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Constructs a BlockContent object.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_storage
|
Chris@0
|
52 * The custom block storage.
|
Chris@0
|
53 * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_type_storage
|
Chris@0
|
54 * The custom block type storage.
|
Chris@0
|
55 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
|
Chris@0
|
56 * The theme handler.
|
Chris@0
|
57 */
|
Chris@0
|
58 public function __construct(EntityStorageInterface $block_content_storage, EntityStorageInterface $block_content_type_storage, ThemeHandlerInterface $theme_handler) {
|
Chris@0
|
59 $this->blockContentStorage = $block_content_storage;
|
Chris@0
|
60 $this->blockContentTypeStorage = $block_content_type_storage;
|
Chris@0
|
61 $this->themeHandler = $theme_handler;
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Displays add custom block links for available types.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
68 * The current request object.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @return array
|
Chris@0
|
71 * A render array for a list of the custom block types that can be added or
|
Chris@0
|
72 * if there is only one custom block type defined for the site, the function
|
Chris@0
|
73 * returns the custom block add page for that custom block type.
|
Chris@0
|
74 */
|
Chris@0
|
75 public function add(Request $request) {
|
Chris@0
|
76 $types = $this->blockContentTypeStorage->loadMultiple();
|
Chris@0
|
77 if ($types && count($types) == 1) {
|
Chris@0
|
78 $type = reset($types);
|
Chris@0
|
79 return $this->addForm($type, $request);
|
Chris@0
|
80 }
|
Chris@0
|
81 if (count($types) === 0) {
|
Chris@0
|
82 return [
|
Chris@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.', [
|
Chris@0
|
84 ':url' => Url::fromRoute('block_content.type_add')->toString(),
|
Chris@0
|
85 ]),
|
Chris@0
|
86 ];
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 return ['#theme' => 'block_content_add_list', '#content' => $types];
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Presents the custom block creation form.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type
|
Chris@0
|
96 * The custom block type to add.
|
Chris@0
|
97 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
98 * The current request object.
|
Chris@0
|
99 *
|
Chris@0
|
100 * @return array
|
Chris@16
|
101 * A form array as expected by
|
Chris@16
|
102 * \Drupal\Core\Render\RendererInterface::render().
|
Chris@0
|
103 */
|
Chris@0
|
104 public function addForm(BlockContentTypeInterface $block_content_type, Request $request) {
|
Chris@0
|
105 $block = $this->blockContentStorage->create([
|
Chris@17
|
106 'type' => $block_content_type->id(),
|
Chris@0
|
107 ]);
|
Chris@0
|
108 if (($theme = $request->query->get('theme')) && in_array($theme, array_keys($this->themeHandler->listInfo()))) {
|
Chris@0
|
109 // We have navigated to this page from the block library and will keep track
|
Chris@0
|
110 // of the theme for redirecting the user to the configuration page for the
|
Chris@0
|
111 // newly created block in the given theme.
|
Chris@0
|
112 $block->setTheme($theme);
|
Chris@0
|
113 }
|
Chris@0
|
114 return $this->entityFormBuilder()->getForm($block);
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * Provides the page title for this controller.
|
Chris@0
|
119 *
|
Chris@0
|
120 * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type
|
Chris@0
|
121 * The custom block type being added.
|
Chris@0
|
122 *
|
Chris@0
|
123 * @return string
|
Chris@0
|
124 * The page title.
|
Chris@0
|
125 */
|
Chris@0
|
126 public function getAddFormTitle(BlockContentTypeInterface $block_content_type) {
|
Chris@0
|
127 return $this->t('Add %type custom block', ['%type' => $block_content_type->label()]);
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 }
|