diff --git a/layout_paragraphs.module b/layout_paragraphs.module index 7f7392f6a8b8f66c256aa8e00646ec3717c9c79c..75c8f9335e902e84d5fca648fc7b342c5476c4a0 100644 --- a/layout_paragraphs.module +++ b/layout_paragraphs.module @@ -73,6 +73,14 @@ function layout_paragraphs_theme() { 'delete_access' => FALSE, ], ], + 'layout_paragraphs_insert_component_btn' => [ + 'variables' => [ + 'title' => NULL, + 'weight' => NULL, + 'attributes' => [], + 'url' => NULL, + ], + ], 'layout_paragraphs_builder_component_menu' => [ 'variables' => [ 'attributes' => [], diff --git a/src/Controller/ChooseComponentController.php b/src/Controller/ChooseComponentController.php index 5151345b397693fc9e3b14a4e07590f10b8ac569..e6e38797a47be539b089caa2f20f37278074252e 100644 --- a/src/Controller/ChooseComponentController.php +++ b/src/Controller/ChooseComponentController.php @@ -77,17 +77,10 @@ class ChooseComponentController extends ControllerBase { * The build array. */ public function list(Request $request, LayoutParagraphsLayout $layout_paragraphs_layout) { - - $route_name = 'layout_paragraphs.builder.insert'; $route_params = [ 'layout_paragraphs_layout' => $layout_paragraphs_layout->id(), ]; - $query_params = [ - 'parent_uuid' => $request->query->get('parent_uuid', NULL), - 'region' => $request->query->get('region', NULL), - 'sibling_uuid' => $request->query->get('sibling_uuid', NULL), - 'placement' => $request->query->get('placement', NULL), - ]; + $query_params = $this->getQueryParams($request); // If inserting a new item adjecent to a sibling component, the region // passed in the URL will be incorrect if the existing sibling component // was dragged into another region. In that case, always use the existing @@ -100,26 +93,61 @@ class ChooseComponentController extends ControllerBase { // If there is only one type to render, // return the component form instead of a list of links. if (count($types) === 1) { - $type_name = key($types); - $type = $this->entityTypeManager()->getStorage('paragraphs_type')->load($type_name); - $form = $this->formBuilder()->getForm( - '\Drupal\layout_paragraphs\Form\InsertComponentForm', - $layout_paragraphs_layout, - $type, - $query_params['parent_uuid'], - $query_params['region'], - $query_params['sibling_uuid'], - $query_params['placement'] - ); - if ($this->isAjax()) { - $response = new AjaxResponse(); - $selector = Dialog::dialogSelector($layout_paragraphs_layout); - $response->addCommand(new OpenDialogCommand($selector, $form['#title'], $form, Dialog::dialogSettings())); - return $response; - } - return $form; + return $this->componentForm(key($types), $layout_paragraphs_layout, $query_params); + } + else { + return $this->componentMenu($types, $route_params, $query_params); + } + } + + /** + * Returns a layout paragraphs component form using Ajax if appropriate. + * + * @param string $type_name + * The component (paragraph) type. + * @param \Drupal\layout_paragraphs\LayoutParagraphsLayout $layout_paragraphs_layout + * The layout paragraphs layout object. + * @param array $query_params + * An array of query parameters. + * + * @return \Drupal\Core\Ajax\AjaxResponse|array + * An ajax response or form render array. + */ + protected function componentForm(string $type_name, LayoutParagraphsLayout $layout_paragraphs_layout, array $query_params) { + $type = $this->entityTypeManager()->getStorage('paragraphs_type')->load($type_name); + $form = $this->formBuilder()->getForm( + '\Drupal\layout_paragraphs\Form\InsertComponentForm', + $layout_paragraphs_layout, + $type, + $query_params['parent_uuid'], + $query_params['region'], + $query_params['sibling_uuid'], + $query_params['placement'] + ); + if ($this->isAjax()) { + $response = new AjaxResponse(); + $selector = Dialog::dialogSelector($layout_paragraphs_layout); + $response->addCommand(new OpenDialogCommand($selector, $form['#title'], $form, Dialog::dialogSettings())); + return $response; } + return $form; + } + /** + * Returns a rendered menu of component types. + * + * @param array $types + * The component types. + * @param array $route_params + * The route parameters. + * @param array $query_params + * The query parameters. + * + * @return array + * The component menu render array. + */ + protected function componentMenu(array $types, array $route_params, array $query_params) { + $route_name = 'layout_paragraphs.builder.insert'; foreach ($types as &$type) { $url_route_params = $route_params + ['paragraph_type' => $type['id']]; $url_options = ['query' => $query_params]; @@ -128,7 +156,6 @@ class ChooseComponentController extends ControllerBase { 'class' => ['use-ajax'], ]); } - $section_components = array_filter($types, function ($type) { return $type['is_section'] === TRUE; }); @@ -156,7 +183,24 @@ class ChooseComponentController extends ControllerBase { ], ]; return $component_menu; + } + /** + * Returns an array of the query parameters to be paseed to a component form. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * The request object. + * + * @return array + * An array of query paramters. + */ + protected function getQueryParams(Request $request) { + return [ + 'parent_uuid' => $request->query->get('parent_uuid', NULL), + 'region' => $request->query->get('region', NULL), + 'sibling_uuid' => $request->query->get('sibling_uuid', NULL), + 'placement' => $request->query->get('placement', NULL), + ]; } /** @@ -259,7 +303,7 @@ class ChooseComponentController extends ControllerBase { foreach ($bundles as $machine_name => $bundle) { $return_bundles[$machine_name] = [ 'label' => $bundle['label'], - 'weight' => isset($drag_drop_settings[$machine_name]['weight']) ? $drag_drop_settings[$machine_name]['weight'] : $weight, + 'weight' => $drag_drop_settings[$machine_name]['weight'] ?? $weight, ]; $weight++; } diff --git a/src/Element/LayoutParagraphsBuilder.php b/src/Element/LayoutParagraphsBuilder.php index 649b6da9b23d0c4244800c869c9724ae77942bf6..3251f44e3ac0ccfdcfd2c2224e0a2ea835e3f168 100644 --- a/src/Element/LayoutParagraphsBuilder.php +++ b/src/Element/LayoutParagraphsBuilder.php @@ -408,7 +408,7 @@ class LayoutParagraphsBuilder extends RenderElement implements ContainerFactoryP */ protected function insertComponentButton(array $route_params = [], array $query_params = [], int $weight = 0, array $classes = []) { return [ - '#type' => 'link', + '#theme' => 'layout_paragraphs_insert_component_btn', '#title' => Markup::create('<span class="visually-hidden">' . $this->t('Choose component') . '</span>'), '#weight' => $weight, '#attributes' => [ @@ -442,7 +442,7 @@ class LayoutParagraphsBuilder extends RenderElement implements ContainerFactoryP */ protected function insertSectionButton(array $route_params = [], array $query_params = [], int $weight = 0, array $classes = []) { return [ - '#type' => 'link', + '#theme' => 'layout_paragraphs_insert_component_btn', '#title' => Markup::create($this->t('Add section')), '#attributes' => [ 'class' => array_merge(['lpb-btn', 'use-ajax'], $classes), diff --git a/src/LayoutParagraphsComponent.php b/src/LayoutParagraphsComponent.php index b62b257b3fabca589af1b9304a8d4388f7c56447..773ddbce1bf884f0a47c0c8c2ced3cc6ee495caa 100644 --- a/src/LayoutParagraphsComponent.php +++ b/src/LayoutParagraphsComponent.php @@ -26,7 +26,7 @@ class LayoutParagraphsComponent { /** * Class constructor. * - * @param \Drupal\paragraphs\ParagraphInterface$paragraph + * @param \Drupal\paragraphs\ParagraphInterface $paragraph * The paragraph entity. */ public function __construct(ParagraphInterface $paragraph) { diff --git a/src/LayoutParagraphsLayout.php b/src/LayoutParagraphsLayout.php index a7de35af410b94a9aa0a0a2d4e97af7a6ae2b6ea..0229aace420defed113bcfb9ceb01c708ac8e5ae 100644 --- a/src/LayoutParagraphsLayout.php +++ b/src/LayoutParagraphsLayout.php @@ -539,14 +539,14 @@ class LayoutParagraphsLayout implements ThirdPartySettingsInterface { * {@inheritdoc} */ public function getThirdPartySetting($provider, $key, $default = NULL) { - return isset($this->thirdPartySettings[$provider][$key]) ? $this->thirdPartySettings[$provider][$key] : $default; + return $this->thirdPartySettings[$provider][$key] ?? $default; } /** * {@inheritdoc} */ public function getThirdPartySettings($provider) { - return isset($this->thirdPartySettings[$provider]) ? $this->thirdPartySettings[$provider] : []; + return $this->thirdPartySettings[$provider] ?? []; } /** diff --git a/src/LayoutParagraphsRendererService.php b/src/LayoutParagraphsRendererService.php index ed3e8f5bb80d290bb7c306445092d4b8369ba285..57f177fe1e7770ebc738e9b28eedc1a365b74637 100644 --- a/src/LayoutParagraphsRendererService.php +++ b/src/LayoutParagraphsRendererService.php @@ -50,7 +50,7 @@ class LayoutParagraphsRendererService { * * @param array $build * The build array. - * @param \Drupal\paragraphs\Entity\Paragraph $paragraph + * @param \Drupal\paragraphs\ParagraphInterface $paragraph * The paragraph entity. * @param string $view_mode * The view mode. @@ -112,7 +112,7 @@ class LayoutParagraphsRendererService { /** * Returns the parent entity for a given paragraph. * - * @param \Drupal\paragraphs\Entity\Paragraph $paragraph + * @param \Drupal\paragraphs\ParagraphInterface $paragraph * The paragraph. * * @return \Drupal\Core\Entity\EntityInterface diff --git a/src/Plugin/Field/FieldFormatter/LayoutParagraphsBuilderFormatter.php b/src/Plugin/Field/FieldFormatter/LayoutParagraphsBuilderFormatter.php index 28439475d6013c33130a1f083ed511b7dafa63cd..3094bfb37d6afe7249b3858771d6ad662a1b387a 100644 --- a/src/Plugin/Field/FieldFormatter/LayoutParagraphsBuilderFormatter.php +++ b/src/Plugin/Field/FieldFormatter/LayoutParagraphsBuilderFormatter.php @@ -13,7 +13,6 @@ use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Entity\EntityDisplayRepositoryInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\layout_paragraphs\Access\LayoutParagraphsBuilderAccess; use Drupal\layout_paragraphs\LayoutParagraphsLayoutTempstoreRepository; use Drupal\layout_paragraphs\Plugin\Field\FieldWidget\LayoutParagraphsWidget; diff --git a/templates/layout-paragraphs-insert-component-btn.html.twig b/templates/layout-paragraphs-insert-component-btn.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..834490b32729436e78aebf31aa271575101dbb57 --- /dev/null +++ b/templates/layout-paragraphs-insert-component-btn.html.twig @@ -0,0 +1 @@ +<a{{attributes}} href="{{url}}">{{ title }}</a> diff --git a/tests/modules/layout_paragraphs_custom_host_entity_test/layout_paragraphs_custom_host_entity_test.module b/tests/modules/layout_paragraphs_custom_host_entity_test/layout_paragraphs_custom_host_entity_test.module index b246056cb4d421eb8f3f6111df5065f656eda145..676affdd8f5fe5f537296b111b6929b03e796fe8 100644 --- a/tests/modules/layout_paragraphs_custom_host_entity_test/layout_paragraphs_custom_host_entity_test.module +++ b/tests/modules/layout_paragraphs_custom_host_entity_test/layout_paragraphs_custom_host_entity_test.module @@ -25,8 +25,8 @@ function layout_paragraphs_custom_host_entity_test_theme() { * * @param array $variables * An associative array containing: - * - elements: An associative array containing the lp host entity information and any - * fields attached to the entity. + * - elements: An associative array containing the lp host entity information + * and any fields attached to the entity. * - attributes: HTML attributes for the containing element. */ function template_preprocess_lp_host_entity(array &$variables) { diff --git a/tests/modules/layout_paragraphs_custom_host_entity_test/src/LpHostEntityListBuilder.php b/tests/modules/layout_paragraphs_custom_host_entity_test/src/LpHostEntityListBuilder.php index 23ec26fe6f4b3180e2743a224091a3f5a7f68b73..daed0acdb172ee4c07038f02a80c75cb8c15782d 100644 --- a/tests/modules/layout_paragraphs_custom_host_entity_test/src/LpHostEntityListBuilder.php +++ b/tests/modules/layout_paragraphs_custom_host_entity_test/src/LpHostEntityListBuilder.php @@ -91,7 +91,7 @@ class LpHostEntityListBuilder extends EntityListBuilder { * {@inheritdoc} */ public function buildRow(EntityInterface $entity) { - /* @var $entity \Drupal\layout_paragraphs_custom_host_entity_test\LpHostEntityInterface */ + /** @var \Drupal\layout_paragraphs_custom_host_entity_test\LpHostEntityInterface $entity */ $row['id'] = $entity->id(); $row['title'] = $entity->toLink(); $row['status'] = $entity->isEnabled() ? $this->t('Enabled') : $this->t('Disabled'); diff --git a/tests/modules/layout_paragraphs_preprocess_layout_test/layout_paragraphs_preprocess_layout_test.module b/tests/modules/layout_paragraphs_preprocess_layout_test/layout_paragraphs_preprocess_layout_test.module index 47d9abb264efd3e14b9f867bbcbc6294e4fe2916..7790d55f6a51b914eb1b42c384922a149cb8ceb1 100644 --- a/tests/modules/layout_paragraphs_preprocess_layout_test/layout_paragraphs_preprocess_layout_test.module +++ b/tests/modules/layout_paragraphs_preprocess_layout_test/layout_paragraphs_preprocess_layout_test.module @@ -1,7 +1,13 @@ <?php -use Drupal\layout_paragraphs\LayoutParagraphsSection; +/** + * @file + * Tests that layout paragraphs variables are available to layout preprocess. + */ +/** + * Implements hook_preprocess_layout(). + */ function layout_paragraphs_preprocess_layout_test_preprocess_layout(&$variables) { /** @var \Drupal\layout_paragraphs\LayoutParagraphsSection $section */ $section = $variables['settings']['layout_paragraphs_section']; diff --git a/tests/modules/layout_paragraphs_translations_test/layout_paragraphs_translations_test.module b/tests/modules/layout_paragraphs_translations_test/layout_paragraphs_translations_test.module index c48423620f212838da238318c62c4a36169573f9..01790cd50333d1a19a814e02692516f320dd16a2 100644 --- a/tests/modules/layout_paragraphs_translations_test/layout_paragraphs_translations_test.module +++ b/tests/modules/layout_paragraphs_translations_test/layout_paragraphs_translations_test.module @@ -1,5 +1,10 @@ <?php +/** + * @file + * Assists in testing translation functionality for Layout Paragraphs. + */ + /** * Implements hook_theme(). */