Skip to content
Snippets Groups Projects
Commit d7a93f82 authored by Justin Toupin's avatar Justin Toupin
Browse files

fixed disabled items, toggle buttons, form errors

parent 7944efc5
No related branches found
No related tags found
No related merge requests found
...@@ -101,9 +101,9 @@ ...@@ -101,9 +101,9 @@
*/ */
function updateFields($container) { function updateFields($container) {
// Set deltas: // Set deltas:
let delta = -1; let delta = Number($container.find(".layout-paragraphs-weight option:first-child").val());
$container $container
.find(".layout-paragraphs-weight, .layout-paragraphs-new-item-delta") .find(".layout-paragraphs-weight")
.each((index, item) => { .each((index, item) => {
if ($(item).hasClass("layout-paragraphs-weight")) { if ($(item).hasClass("layout-paragraphs-weight")) {
delta += 1; delta += 1;
...@@ -378,6 +378,7 @@ ...@@ -378,6 +378,7 @@
const $parentUuidInput = $widget.find( const $parentUuidInput = $widget.find(
".layout-paragraphs-new-item-parent-uuid" ".layout-paragraphs-new-item-parent-uuid"
); );
const parentUuidSelector = '.' + $btn.attr('data-parent-uuid-class');
const $menu = $widget.find(".layout-paragraphs-add-more-menu"); const $menu = $widget.find(".layout-paragraphs-add-more-menu");
const region = getRegion( const region = getRegion(
$btn.closest(".layout-paragraphs-layout-region") $btn.closest(".layout-paragraphs-layout-region")
...@@ -385,23 +386,12 @@ ...@@ -385,23 +386,12 @@
const depth = region const depth = region
? $btn.parents(".layout-paragraphs-layout").length ? $btn.parents(".layout-paragraphs-layout").length
: 0; : 0;
let parentUuid = ""; const parentUuid = parentUuidSelector
if (region) { ? $btn
if ($btn.closest('.layout-paragraphs-item').is('.layout-paragraphs-layout')) { .closest('.layout-paragraphs-item')
parentUuid = $btn .find(parentUuidSelector)
.closest(".layout-paragraphs-item") .val()
.parent() : "";
.closest(".layout-paragraphs-layout")
.find(".layout-paragraphs-uuid")
.val();
}
else {
parentUuid = $btn
.closest(".layout-paragraphs-layout")
.find(".layout-paragraphs-uuid")
.val();
}
}
const parentWeight = const parentWeight =
0.5 + 0.5 +
Number( Number(
...@@ -468,9 +458,9 @@ ...@@ -468,9 +458,9 @@
$regionInput.val(region); $regionInput.val(region);
$parentWeightInput.val(parentWeight); $parentWeightInput.val(parentWeight);
$parentUuidInput.val(parentUuid); $parentUuidInput.val(parentUuid);
console.log(region); // console.log(region);
console.log(parentWeight); // console.log(parentWeight);
console.log(parentUuid); // console.log(parentUuid);
setTimeout(() => { setTimeout(() => {
positionMenu($menu); positionMenu($menu);
}, 100); }, 100);
...@@ -657,6 +647,19 @@ ...@@ -657,6 +647,19 @@
} }
}, 50); }, 50);
}); });
/**
* Only show disabled items if there are items in the field.
* Runs every time DOM is updated.
*/
$(".layout-paragraphs-field", context)
.each((index, field) => {
if ($('.layout-paragraphs-item', field).length == 0) {
$('.layout-paragraphs-disabled-items', field).hide();
}
else {
$('.layout-paragraphs-disabled-items', field).show();
}
});
/** /**
* Update weights, regions, and disabled area on load. * Update weights, regions, and disabled area on load.
* Runs every time DOM is updated. * Runs every time DOM is updated.
......
...@@ -424,25 +424,23 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -424,25 +424,23 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
} }
// Container for disabled / orphaned items. // Container for disabled / orphaned items.
if ($paragraph_items_count) { $elements['disabled'] = [
$elements['disabled'] = [ '#type' => 'fieldset',
'#type' => 'fieldset', '#attributes' => ['class' => ['layout-paragraphs-disabled-items']],
'#attributes' => ['class' => ['layout-paragraphs-disabled-items']], '#weight' => 999,
'#weight' => 999, '#title' => $this->t('Disabled Items'),
'#title' => $this->t('Disabled Items'), 'items' => [
'items' => [ '#type' => 'container',
'#type' => 'container', '#attributes' => [
'#attributes' => [ 'class' => [
'class' => [ 'layout-paragraphs-disabled-items__items',
'layout-paragraphs-disabled-items__items',
],
],
'description' => [
'#markup' => '<div class="layout-paragraphs-disabled-items__description">' . $this->t('Drop items here that you want to keep disabled / hidden, without removing them permanently.') . '</div>',
], ],
], ],
]; 'description' => [
} '#markup' => '<div class="layout-paragraphs-disabled-items__description">' . $this->t('Drop items here that you want to keep disabled / hidden, without removing them permanently.') . '</div>',
],
],
];
$elements['#attached']['drupalSettings']['paragraphsLayoutWidget']['maxDepth'] = $this->getSetting('nesting_depth'); $elements['#attached']['drupalSettings']['paragraphsLayoutWidget']['maxDepth'] = $this->getSetting('nesting_depth');
$elements['#attached']['drupalSettings']['paragraphsLayoutWidget']['requireLayouts'] = $this->getSetting('require_layouts'); $elements['#attached']['drupalSettings']['paragraphsLayoutWidget']['requireLayouts'] = $this->getSetting('require_layouts');
...@@ -541,7 +539,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -541,7 +539,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
'#type' => 'value', '#type' => 'value',
'#value' => $entity, '#value' => $entity,
], ],
'toggle_button' => $this->toggleButton(), 'toggle_button' => $this->toggleButton('layout-paragraphs-parent-uuid'),
// Edit and remove button. // Edit and remove button.
'actions' => [ 'actions' => [
'#type' => 'container', '#type' => 'container',
...@@ -596,7 +594,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -596,7 +594,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
'layout-paragraphs-layout-region--' . $region_name, 'layout-paragraphs-layout-region--' . $region_name,
], ],
], ],
'toggle_button' => $this->toggleButton(), 'toggle_button' => $this->toggleButton('layout-paragraphs-uuid'),
]; ];
} }
} }
...@@ -740,9 +738,9 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -740,9 +738,9 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
* @return array * @return array
* Returns the render array for an "add more" toggle button. * Returns the render array for an "add more" toggle button.
*/ */
protected function toggleButton() { protected function toggleButton($uuid_class = '') {
return [ return [
'#markup' => '<button class="layout-paragraphs-add-content__toggle">+<span class="visually-hidden">Add Item</span></button>', '#markup' => '<button class="layout-paragraphs-add-content__toggle" data-parent-uuid-class="' . $uuid_class . '">+<span class="visually-hidden">Add Item</span></button>',
'#allowed_tags' => ['button', 'span'], '#allowed_tags' => ['button', 'span'],
'#weight' => 10000, '#weight' => 10000,
]; ];
...@@ -908,10 +906,11 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -908,10 +906,11 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
$updated_layout_definition = $updated_layout_instance->getPluginDefinition(); $updated_layout_definition = $updated_layout_instance->getPluginDefinition();
$updated_regions = $updated_layout_definition->getRegions(); $updated_regions = $updated_layout_definition->getRegions();
$updated_regions_options = ['_disabled' => $this->t('Disabled')]; $updated_regions_options = [];
foreach ($updated_regions as $region_name => $region) { foreach ($updated_regions as $region_name => $region) {
$updated_regions_options[$region_name] = $region['label']; $updated_regions_options[$region_name] = $region['label'];
} }
$updated_regions_options['_disabled'] = $this->t('Disabled');
foreach ($original_regions as $region_name => $region) { foreach ($original_regions as $region_name => $region) {
if (!isset($updated_regions[$region_name]) && $this->hasChildren($entity, $widget_state['items'], $region_name)) { if (!isset($updated_regions[$region_name]) && $this->hasChildren($entity, $widget_state['items'], $region_name)) {
$move_items[$region_name] = [ $move_items[$region_name] = [
...@@ -1309,7 +1308,6 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -1309,7 +1308,6 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
} }
unset($widget_state['items'][$delta]['entity']); unset($widget_state['items'][$delta]['entity']);
$widget_state['remove_item'] = FALSE; $widget_state['remove_item'] = FALSE;
$widget_state['items_count'] = count($widget_state['items']);
static::setWidgetState($parents, $this->fieldName, $form_state, $widget_state); static::setWidgetState($parents, $this->fieldName, $form_state, $widget_state);
$form_state->setRebuild(); $form_state->setRebuild();
} }
...@@ -1429,8 +1427,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -1429,8 +1427,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
// If canceling an item that hasn't been created yet, remove it. // If canceling an item that hasn't been created yet, remove it.
if (!empty($widget_state['items'][$delta]['is_new'])) { if (!empty($widget_state['items'][$delta]['is_new'])) {
array_splice($widget_state['items'], $delta, 1); unset($widget_state['items'][$delta]['entity']);
$widget_state['items_count'] = count($widget_state['items']);
} }
$widget_state['open_form'] = FALSE; $widget_state['open_form'] = FALSE;
static::setWidgetState($parents, $this->fieldName, $form_state, $widget_state); static::setWidgetState($parents, $this->fieldName, $form_state, $widget_state);
...@@ -1725,7 +1722,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -1725,7 +1722,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
$defaults = [ $defaults = [
'parent_uuid' => '', 'parent_uuid' => '',
'layout' => '', 'layout' => '',
'region' => '_disabled', 'region' => '',
'config' => [], 'config' => [],
]; ];
$behavior_settings = $paragraph->getAllBehaviorSettings(); $behavior_settings = $paragraph->getAllBehaviorSettings();
...@@ -1843,9 +1840,9 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi ...@@ -1843,9 +1840,9 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
$entity_type = $this->getFieldSetting('target_type'); $entity_type = $this->getFieldSetting('target_type');
$handler_settings = $this->getFieldSetting('handler_settings'); $handler_settings = $this->getFieldSetting('handler_settings');
$bundles = isset($handler_settings["target_bundles_drag_drop"]) ? array_keys($handler_settings["target_bundles_drag_drop"]) : []; $bundles = array_keys($handler_settings["target_bundles_drag_drop"]);
$selected_bundles = !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : []; $selected_bundles = !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : [];
if (empty($handler_settings["negate"])) { if (!$handler_settings["negate"]) {
$selected_bundles = empty($selected_bundles) ? [] : $selected_bundles; $selected_bundles = empty($selected_bundles) ? [] : $selected_bundles;
$target_bundles = array_intersect($bundles, $selected_bundles); $target_bundles = array_intersect($bundles, $selected_bundles);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment