diff --git a/css/layout-paragraphs-widget.css b/css/layout-paragraphs-widget.css index 4826d16d49efafcc1f0176738781680ba47d9724..db75ece9886fd525e20bbb3b5d864fe17afd0225 100644 --- a/css/layout-paragraphs-widget.css +++ b/css/layout-paragraphs-widget.css @@ -178,17 +178,11 @@ fieldset.layout-paragraphs-field > legend { .reversed .layout-up { background-image: url(../img/icon-up--reversed.png); } -.layout-paragraphs-field .layout-paragraphs-layout:first-child > .layout-controls .layout-up, -.layout-paragraphs-field .layout-paragraphs-layout:first-child .layout-paragraphs-item:first-child .layout-up { +.layout-paragraphs-item:first-child > .layout-controls .layout-up { pointer-events: none; opacity: .15; } -.layout-paragraphs-field .layout-paragraphs-layout:first-child .layout-paragraphs-item:first-child .layout-up { - pointer-events: none; - opacity: .15; -} -.layout-paragraphs-field .layout-paragraphs-layout:last-child > .layout-controls .layout-down, -.layout-paragraphs-field .layout-paragraphs-layout:last-child .layout-paragraphs-item:nth-last-child(2) .layout-down { +.layout-paragraphs-item:nth-last-child(2) > .layout-controls .layout-down { pointer-events: none; opacity: .15; } diff --git a/js/layout-paragraphs-widget.js b/js/layout-paragraphs-widget.js index 37e91c9e7eb355ed41184ed69cf8d76d2e3c6d22..f06d2b8fac623451f4a2c925dbc8f1b1ecb049cb 100644 --- a/js/layout-paragraphs-widget.js +++ b/js/layout-paragraphs-widget.js @@ -487,7 +487,6 @@ /** * Hide all "add paragraph item" buttons if we have reached cardinality. */ - console.log(widgetSettings.itemsCount); if ( widgetSettings.cardinality > -1 && widgetSettings.itemsCount >= widgetSettings.cardinality diff --git a/src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php b/src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php index 2f33f1991c27414650e72368ffbf6e1bd86725c7..239ac6ee121172834b6e66b73c9e00dbafca9a06 100644 --- a/src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php @@ -316,7 +316,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi '#title_display' => 'invisible', // Note: this 'delta' is the FAPI #type 'weight' element's property. '#delta' => $widget_state['items_count'], - '#default_value' => $items[$delta]->_weight ?: $delta, + '#default_value' => $weight, '#weight' => -1000, '#wrapper_attributes' => [ 'class' => ['hidden'], @@ -643,6 +643,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi '#host' => $items->getEntity(), '#value' => $this->t('Create New'), '#submit' => [[$this, 'newItemSubmit']], + '#element_validate' => [[$this, 'newItemValidate']], '#limit_validation_errors' => [array_merge($parents, [ $this->fieldName, 'add_more', @@ -1010,6 +1011,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi $available_layouts = $this->getAvailableLayouts($entity); $layout_settings = $this->getLayoutSettings($entity); $layout = $layout_settings['layout']; + $default_layout = !empty($layout) ? $layout : key($available_layouts); $layout_plugin_config = $layout_settings['config'] ?? []; $element['entity_form']['layout_selection'] = [ @@ -1019,7 +1021,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi '#type' => 'radios', '#title' => $this->t('Select a layout:'), '#options' => $available_layouts, - '#default_value' => $layout, + '#default_value' => $default_layout, '#attributes' => [ 'class' => ['layout-paragraphs-layout-select'], ], @@ -1446,6 +1448,29 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi } } + /** + * New item validator - checks cardinality. + * + * @param array $element + * The element to validate. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public function newItemValidate(array $element, FormStateInterface $form_state) { + $triggering_element = $form_state->getTriggeringElement(); + // Only check cardinality if the user clicked to add a new paragraph. + if ($triggering_element['#array_parents'] == $element['#array_parents']) { + $parents = $element['#element_parents']; + $widget_state = static::getWidgetState($parents, $this->fieldName, $form_state); + $count = $this->activeItemsCount($widget_state['items']); + $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); + + if ($cardinality != -1 && $count >= $cardinality) { + $form_state->setError($element, $this->t('You can only add @cardinality or fewer items.', ['@cardinality' => $cardinality])); + } + } + } + /** * Form submit handler - adds a new item and opens its edit form. * @@ -1846,9 +1871,21 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ]; $response = new AjaxResponse(); - $response->addCommand(new AppendCommand('#' . $this->wrapperId, '<div id="' . $html_id . '"></div>')); - $response->addCommand(new OpenDialogCommand('#' . $html_id, 'Edit Form', $entity_form, $dialog_options)); - $response->addCommand(new LayoutParagraphsStateResetCommand('#' . $this->wrapperId)); + if ($form_state->hasAnyErrors()) { + $content = [ + 'status' => [ + '#weight' => -100, + '#type' => 'status_messages', + ], + ]; + $response->addCommand(new OpenDialogCommand('#' . $html_id, $this->t('Unexpected Error'), $content, $dialog_options)); + $response->addCommand(new LayoutParagraphsStateResetCommand('#' . $this->wrapperId)); + } + else { + $response->addCommand(new AppendCommand('#' . $this->wrapperId, '<div id="' . $html_id . '"></div>')); + $response->addCommand(new OpenDialogCommand('#' . $html_id, 'Edit Form', $entity_form, $dialog_options)); + $response->addCommand(new LayoutParagraphsStateResetCommand('#' . $this->wrapperId)); + } return $response; }