Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block_content / src / Controller / BlockContentController.php
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
102    *   \Drupal\Core\Render\RendererInterface::render().
103    */
104   public function addForm(BlockContentTypeInterface $block_content_type, Request $request) {
105     $block = $this->blockContentStorage->create([
106       'type' => $block_content_type->id(),
107     ]);
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);
113     }
114     return $this->entityFormBuilder()->getForm($block);
115   }
116
117   /**
118    * Provides the page title for this controller.
119    *
120    * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type
121    *   The custom block type being added.
122    *
123    * @return string
124    *   The page title.
125    */
126   public function getAddFormTitle(BlockContentTypeInterface $block_content_type) {
127     return $this->t('Add %type custom block', ['%type' => $block_content_type->label()]);
128   }
129
130 }