diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 5c902da..ba367fd 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1612,7 +1612,6 @@ function template_preprocess_field_multiple_value_form(&$variables) { 'class' => ['field-label'], ], t('Order', [], ['context' => 'Sort order']), - t('Operations'), ]; $rows = []; @@ -1630,6 +1629,7 @@ function template_preprocess_field_multiple_value_form(&$variables) { } usort($items, '_field_multiple_value_form_sort_helper'); + $removable = FALSE; // Add the items as table rows. foreach ($items as $item) { $item['_weight']['#attributes']['class'] = [$order_class]; @@ -1637,24 +1637,35 @@ function template_preprocess_field_multiple_value_form(&$variables) { // Remove weight and remove form element from item render array so they // can be rendered in a separate table columns. $delta_element = $item['_weight']; - $remove_element = $item['_remove']; - unset($item['_weight'], $item['_remove']); + unset($item['_weight']); + if (!empty($item['_remove'])) { + $removable = TRUE; + $remove_element = $item['_remove']; + unset($item['_remove']); + } else { + $remove_element = NULL; + } $cells = [ ['data' => '', 'class' => ['field-multiple-drag']], ['data' => $item], ['data' => $delta_element, 'class' => ['delta-order']], - ['data' => $remove_element, 'class' => ['delta-remove']], ]; $class = ['draggable']; - if (!empty($remove_element['check']['#checked'])) { - $class[] = 'removed'; + if (!empty($remove_element)) { + $cells[] = ['data' => $remove_element, 'class' => ['delta-remove']]; + if (!empty($remove_element['check']['#checked'])) { + $class[] = 'removed'; + } } $rows[] = [ 'data' => $cells, 'class' => $class, ]; } + if ($removable) { + $header[] = t('Operations'); + } $variables['table'] = [ '#type' => 'table', diff --git a/core/misc/form.js b/core/misc/form.js index e55d574..902b1cb 100644 --- a/core/misc/form.js +++ b/core/misc/form.js @@ -124,4 +124,29 @@ }); } }; + + Drupal.behaviors.multipleRemove = { + attach: function attach(context, settings) { + $(context).find('table.field-multiple-table').once('multiple-remove').each(Drupal.multipleRemove); + } + }; + + Drupal.multipleRemove = function () { + if ($(this).find('td.delta-remove input[type="checkbox"]').length === 0) { + return; + } + + var table = this; + var $table = $(table); + + $table.find('td.delta-remove input[type="submit"]').removeClass('hidden').on('click', function (e) { + var $checkbox = $(this).closest('td').find('input[type="checkbox"]'); + var state = $checkbox.prop('checked'); + + $checkbox.prop('checked', !state); + $checkbox.closest('tr').toggleClass('removed', !state); + + return false; + }); + }; })(jQuery, Drupal, Drupal.debounce); \ No newline at end of file