Mercurial > hg > isophonics-drupal-site
comparison core/modules/layout_builder/src/Controller/ChooseBlockController.php @ 18:af1871eacc83
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:33:08 +0100 |
parents | 129ea1e6d783 |
children |
comparison
equal
deleted
inserted
replaced
17:129ea1e6d783 | 18:af1871eacc83 |
---|---|
4 | 4 |
5 use Drupal\Core\Ajax\AjaxHelperTrait; | 5 use Drupal\Core\Ajax\AjaxHelperTrait; |
6 use Drupal\Core\Block\BlockManagerInterface; | 6 use Drupal\Core\Block\BlockManagerInterface; |
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | 7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
8 use Drupal\Core\Entity\EntityTypeManagerInterface; | 8 use Drupal\Core\Entity\EntityTypeManagerInterface; |
9 use Drupal\Core\Session\AccountInterface; | |
9 use Drupal\Core\StringTranslation\StringTranslationTrait; | 10 use Drupal\Core\StringTranslation\StringTranslationTrait; |
10 use Drupal\Core\Url; | 11 use Drupal\Core\Url; |
11 use Drupal\layout_builder\Context\LayoutBuilderContextTrait; | 12 use Drupal\layout_builder\Context\LayoutBuilderContextTrait; |
13 use Drupal\layout_builder\LayoutBuilderHighlightTrait; | |
12 use Drupal\layout_builder\SectionStorageInterface; | 14 use Drupal\layout_builder\SectionStorageInterface; |
13 use Symfony\Component\DependencyInjection\ContainerInterface; | 15 use Symfony\Component\DependencyInjection\ContainerInterface; |
14 | 16 |
15 /** | 17 /** |
16 * Defines a controller to choose a new block. | 18 * Defines a controller to choose a new block. |
17 * | 19 * |
18 * @internal | 20 * @internal |
21 * Controller classes are internal. | |
19 */ | 22 */ |
20 class ChooseBlockController implements ContainerInjectionInterface { | 23 class ChooseBlockController implements ContainerInjectionInterface { |
21 | 24 |
22 use AjaxHelperTrait; | 25 use AjaxHelperTrait; |
23 use LayoutBuilderContextTrait; | 26 use LayoutBuilderContextTrait; |
27 use LayoutBuilderHighlightTrait; | |
24 use StringTranslationTrait; | 28 use StringTranslationTrait; |
25 | 29 |
26 /** | 30 /** |
27 * The block manager. | 31 * The block manager. |
28 * | 32 * |
34 * The entity type manager. | 38 * The entity type manager. |
35 * | 39 * |
36 * @var \Drupal\Core\Entity\EntityTypeManagerInterface | 40 * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
37 */ | 41 */ |
38 protected $entityTypeManager; | 42 protected $entityTypeManager; |
43 | |
44 /** | |
45 * The current user. | |
46 * | |
47 * @var \Drupal\Core\Session\AccountInterface | |
48 */ | |
49 protected $currentUser; | |
39 | 50 |
40 /** | 51 /** |
41 * ChooseBlockController constructor. | 52 * ChooseBlockController constructor. |
42 * | 53 * |
43 * @param \Drupal\Core\Block\BlockManagerInterface $block_manager | 54 * @param \Drupal\Core\Block\BlockManagerInterface $block_manager |
44 * The block manager. | 55 * The block manager. |
45 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager | 56 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
46 * The entity type manager. | 57 * The entity type manager. |
47 */ | 58 * @param \Drupal\Core\Session\AccountInterface $current_user |
48 public function __construct(BlockManagerInterface $block_manager, EntityTypeManagerInterface $entity_type_manager) { | 59 * The current user. |
60 */ | |
61 public function __construct(BlockManagerInterface $block_manager, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user = NULL) { | |
49 $this->blockManager = $block_manager; | 62 $this->blockManager = $block_manager; |
50 $this->entityTypeManager = $entity_type_manager; | 63 $this->entityTypeManager = $entity_type_manager; |
64 if (!$current_user) { | |
65 @trigger_error('The current_user service must be passed to ChooseBlockController::__construct(), it is required before Drupal 9.0.0.', E_USER_DEPRECATED); | |
66 $current_user = \Drupal::currentUser(); | |
67 } | |
68 $this->currentUser = $current_user; | |
51 } | 69 } |
52 | 70 |
53 /** | 71 /** |
54 * {@inheritdoc} | 72 * {@inheritdoc} |
55 */ | 73 */ |
56 public static function create(ContainerInterface $container) { | 74 public static function create(ContainerInterface $container) { |
57 return new static( | 75 return new static( |
58 $container->get('plugin.manager.block'), | 76 $container->get('plugin.manager.block'), |
59 $container->get('entity_type.manager') | 77 $container->get('entity_type.manager'), |
78 $container->get('current_user') | |
60 ); | 79 ); |
61 } | 80 } |
62 | 81 |
63 /** | 82 /** |
64 * Provides the UI for choosing a new block. | 83 * Provides the UI for choosing a new block. |
72 * | 91 * |
73 * @return array | 92 * @return array |
74 * A render array. | 93 * A render array. |
75 */ | 94 */ |
76 public function build(SectionStorageInterface $section_storage, $delta, $region) { | 95 public function build(SectionStorageInterface $section_storage, $delta, $region) { |
77 $build['#title'] = $this->t('Choose a block'); | |
78 if ($this->entityTypeManager->hasDefinition('block_content_type') && $types = $this->entityTypeManager->getStorage('block_content_type')->loadMultiple()) { | 96 if ($this->entityTypeManager->hasDefinition('block_content_type') && $types = $this->entityTypeManager->getStorage('block_content_type')->loadMultiple()) { |
79 if (count($types) === 1) { | 97 if (count($types) === 1) { |
80 $type = reset($types); | 98 $type = reset($types); |
81 $plugin_id = 'inline_block:' . $type->id(); | 99 $plugin_id = 'inline_block:' . $type->id(); |
82 if ($this->blockManager->hasDefinition($plugin_id)) { | 100 if ($this->blockManager->hasDefinition($plugin_id)) { |
103 '#url' => $url, | 121 '#url' => $url, |
104 '#title' => $this->t('Create @entity_type', [ | 122 '#title' => $this->t('Create @entity_type', [ |
105 '@entity_type' => $this->entityTypeManager->getDefinition('block_content')->getSingularLabel(), | 123 '@entity_type' => $this->entityTypeManager->getDefinition('block_content')->getSingularLabel(), |
106 ]), | 124 ]), |
107 '#attributes' => $this->getAjaxAttributes(), | 125 '#attributes' => $this->getAjaxAttributes(), |
126 '#access' => $this->currentUser->hasPermission('create and edit custom blocks'), | |
108 ]; | 127 ]; |
109 $build['add_block']['#attributes']['class'][] = 'inline-block-create-button'; | 128 $build['add_block']['#attributes']['class'][] = 'inline-block-create-button'; |
110 } | 129 } |
111 } | 130 } |
112 | 131 |
132 $build['filter'] = [ | |
133 '#type' => 'search', | |
134 '#title' => $this->t('Filter by block name'), | |
135 '#title_display' => 'invisible', | |
136 '#size' => 30, | |
137 '#placeholder' => $this->t('Filter by block name'), | |
138 '#attributes' => [ | |
139 'class' => ['js-layout-builder-filter'], | |
140 'title' => $this->t('Enter a part of the block name to filter by.'), | |
141 ], | |
142 ]; | |
143 | |
113 $block_categories['#type'] = 'container'; | 144 $block_categories['#type'] = 'container'; |
114 $block_categories['#attributes']['class'][] = 'block-categories'; | 145 $block_categories['#attributes']['class'][] = 'block-categories'; |
146 $block_categories['#attributes']['class'][] = 'js-layout-builder-categories'; | |
147 $block_categories['#attributes']['data-layout-builder-target-highlight-id'] = $this->blockAddHighlightId($delta, $region); | |
115 | 148 |
116 // @todo Explicitly cast delta to an integer, remove this in | 149 // @todo Explicitly cast delta to an integer, remove this in |
117 // https://www.drupal.org/project/drupal/issues/2984509. | 150 // https://www.drupal.org/project/drupal/issues/2984509. |
118 $delta = (int) $delta; | 151 $delta = (int) $delta; |
119 | 152 |
123 'region' => $region, | 156 'region' => $region, |
124 ]); | 157 ]); |
125 $grouped_definitions = $this->blockManager->getGroupedDefinitions($definitions); | 158 $grouped_definitions = $this->blockManager->getGroupedDefinitions($definitions); |
126 foreach ($grouped_definitions as $category => $blocks) { | 159 foreach ($grouped_definitions as $category => $blocks) { |
127 $block_categories[$category]['#type'] = 'details'; | 160 $block_categories[$category]['#type'] = 'details'; |
161 $block_categories[$category]['#attributes']['class'][] = 'js-layout-builder-category'; | |
128 $block_categories[$category]['#open'] = TRUE; | 162 $block_categories[$category]['#open'] = TRUE; |
129 $block_categories[$category]['#title'] = $category; | 163 $block_categories[$category]['#title'] = $category; |
130 $block_categories[$category]['links'] = $this->getBlockLinks($section_storage, $delta, $region, $blocks); | 164 $block_categories[$category]['links'] = $this->getBlockLinks($section_storage, $delta, $region, $blocks); |
131 } | 165 } |
132 $build['block_categories'] = $block_categories; | 166 $build['block_categories'] = $block_categories; |
152 'region' => $region, | 186 'region' => $region, |
153 'list' => 'inline_blocks', | 187 'list' => 'inline_blocks', |
154 ]); | 188 ]); |
155 $blocks = $this->blockManager->getGroupedDefinitions($definitions); | 189 $blocks = $this->blockManager->getGroupedDefinitions($definitions); |
156 $build = []; | 190 $build = []; |
157 if (isset($blocks['Inline blocks'])) { | 191 $inline_blocks_category = (string) $this->t('Inline blocks'); |
158 $build['links'] = $this->getBlockLinks($section_storage, $delta, $region, $blocks['Inline blocks']); | 192 if (isset($blocks[$inline_blocks_category])) { |
193 $build['links'] = $this->getBlockLinks($section_storage, $delta, $region, $blocks[$inline_blocks_category]); | |
159 $build['links']['#attributes']['class'][] = 'inline-block-list'; | 194 $build['links']['#attributes']['class'][] = 'inline-block-list'; |
160 foreach ($build['links']['#links'] as &$link) { | 195 foreach ($build['links']['#links'] as &$link) { |
161 $link['attributes']['class'][] = 'inline-block-list__item'; | 196 $link['attributes']['class'][] = 'inline-block-list__item'; |
162 } | 197 } |
163 $build['back_button'] = [ | 198 $build['back_button'] = [ |
172 ), | 207 ), |
173 '#title' => $this->t('Back'), | 208 '#title' => $this->t('Back'), |
174 '#attributes' => $this->getAjaxAttributes(), | 209 '#attributes' => $this->getAjaxAttributes(), |
175 ]; | 210 ]; |
176 } | 211 } |
212 $build['links']['#attributes']['data-layout-builder-target-highlight-id'] = $this->blockAddHighlightId($delta, $region); | |
177 return $build; | 213 return $build; |
178 } | 214 } |
179 | 215 |
180 /** | 216 /** |
181 * Gets a render array of block links. | 217 * Gets a render array of block links. |
193 * The block links render array. | 229 * The block links render array. |
194 */ | 230 */ |
195 protected function getBlockLinks(SectionStorageInterface $section_storage, $delta, $region, array $blocks) { | 231 protected function getBlockLinks(SectionStorageInterface $section_storage, $delta, $region, array $blocks) { |
196 $links = []; | 232 $links = []; |
197 foreach ($blocks as $block_id => $block) { | 233 foreach ($blocks as $block_id => $block) { |
234 $attributes = $this->getAjaxAttributes(); | |
235 $attributes['class'][] = 'js-layout-builder-block-link'; | |
198 $link = [ | 236 $link = [ |
199 'title' => $block['admin_label'], | 237 'title' => $block['admin_label'], |
200 'url' => Url::fromRoute('layout_builder.add_block', | 238 'url' => Url::fromRoute('layout_builder.add_block', |
201 [ | 239 [ |
202 'section_storage_type' => $section_storage->getStorageType(), | 240 'section_storage_type' => $section_storage->getStorageType(), |
204 'delta' => $delta, | 242 'delta' => $delta, |
205 'region' => $region, | 243 'region' => $region, |
206 'plugin_id' => $block_id, | 244 'plugin_id' => $block_id, |
207 ] | 245 ] |
208 ), | 246 ), |
209 'attributes' => $this->getAjaxAttributes(), | 247 'attributes' => $attributes, |
210 ]; | 248 ]; |
211 | 249 |
212 $links[] = $link; | 250 $links[] = $link; |
213 } | 251 } |
214 return [ | 252 return [ |