diff --git a/css/layout-select.css b/css/layout-select.css index 0db8be5abbf56b39f1e05d83667cd672943c670f..7d1b23636194d168e4f95a9823057ba4c3e8eb19 100644 --- a/css/layout-select.css +++ b/css/layout-select.css @@ -3,21 +3,23 @@ flex-wrap: wrap; justify-content: flex-start; } -.layout-select__item label { - display: flex; - flex: 1 0 21%; +.layout-select__item { + flex: 0 1 150px; + margin-right: 10px; } -.layout-select__item input[type=radio] { - position: absolute; - opacity: 0; +.layout-select__item label.option { + display: flex; + padding: 10px; + outline: 1px solid #ccc; } .layout-select__item input:checked + label { - outline: 1px solid blue; - padding: 10px; + outline: 2px solid blue; + transition: .1s all; + font-weight: bold; } .layout-select__item-icon { margin-right: 10px; } -.layout-select__item-icon svg { +.layout-select .layout-select__item-icon svg { display: block; } \ No newline at end of file diff --git a/src/LayoutParagraphsLayout.php b/src/LayoutParagraphsLayout.php index 4b3514ca7b7d606435cdcb203e17eb6805ffae8e..2c2384710e358006201bf0a4ddbbfcedb7bc3a42 100644 --- a/src/LayoutParagraphsLayout.php +++ b/src/LayoutParagraphsLayout.php @@ -333,6 +333,19 @@ class LayoutParagraphsLayout { return $this; } + /** + * Append a new component. + * + * @param \Drupal\paragraphs\Entity\Paragraph $new_paragraph + * The paragraph component to append. + * + * @return $this + */ + public function appendComponent(Paragraph $new_paragraph) { + $this->entity->{$this->fieldName}->appendItem(['entity' => $new_paragraph]); + return $this; + } + /** * Delete a component. * diff --git a/src/Plugin/paragraphs/Behavior/LayoutParagraphsBehavior.php b/src/Plugin/paragraphs/Behavior/LayoutParagraphsBehavior.php index 0e45c3c07903a919fe79a494a03c2de136f53a19..e613bcce890543c3c83b6ee9b151f68e0fcae426 100644 --- a/src/Plugin/paragraphs/Behavior/LayoutParagraphsBehavior.php +++ b/src/Plugin/paragraphs/Behavior/LayoutParagraphsBehavior.php @@ -13,6 +13,9 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\layout_paragraphs\LayoutParagraphsSection; use Drupal\layout_paragraphs\LayoutParagraphsService; +use Drupal\Core\Layout\LayoutInterface; +use Drupal\Core\Plugin\PluginWithFormsInterface; +use Drupal\Core\Plugin\PluginFormInterface; /** * Provides a way to define grid based layouts. @@ -112,14 +115,20 @@ class LayoutParagraphsBehavior extends ParagraphsBehaviorBase { ) { $layout_paragraphs_section = new LayoutParagraphsSection($paragraph); + $layout_settings = $layout_paragraphs_section->getSettings(); $available_layouts = $this->configuration['available_layouts']; + $layout_id = $layout_paragraphs_section->getLayoutId(); + $default_value = $layout_id ? $layout_id : key($available_layouts); + $plugin_instance = $this->layoutPluginManager->createInstance($default_value, $layout_settings); + $plugin_form = $this->getLayoutPluginForm($plugin_instance); $form['layout'] = [ '#title' => $this->t('Choose a layout:'), '#type' => 'layout_select', '#options' => $available_layouts, - '#default_value' => $layout_paragraphs_section->getLayoutId(), + '#default_value' => $default_value, ]; + return $form; } @@ -187,4 +196,30 @@ class LayoutParagraphsBehavior extends ParagraphsBehaviorBase { } } + /** + * Retrieves the plugin form for a given layout. + * + * @param \Drupal\Core\Layout\LayoutInterface $layout + * The layout plugin. + * + * @return \Drupal\Core\Plugin\PluginFormInterface|null + * The plugin form for the layout. + */ + protected function getLayoutPluginForm(LayoutInterface $layout) { + if ($layout instanceof PluginWithFormsInterface) { + try { + return $this->pluginFormFactory->createInstance($layout, 'configure'); + } + catch (\Exception $e) { + $this->loggerFactory->get('layout_paragraphs')->error('Erl, Layout Configuration', $e); + } + } + + if ($layout instanceof PluginFormInterface) { + return $layout; + } + + return NULL; + } + }