diff --git a/core/includes/common.inc b/core/includes/common.inc index 1eb2e769f3..8083eedb73 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -723,11 +723,11 @@ function drupal_process_states(&$elements) { * @deprecated in Drupal 8.7.0 and will be removed in Drupal 9.0.0. Use * \Drupal\Core\Render\Element\Table::attachTabledrag() instead. * - * @see https://www.drupal.org/project/drupal/issues/3035343 + * @see https://www.drupal.org/node/3035565 * @see MenuForm::BuildOverviewForm() */ function drupal_attach_tabledrag(&$element, array $options) { - @trigger_error('drupal_attach_tabledrag() is deprecated in Drupal 8.7.0 and will be removed in Drupal 9.0.0. Use \Drupal\Core\Render\Element\Table::attachTabledrag() instead. See TBD', E_USER_DEPRECATED); + @trigger_error('drupal_attach_tabledrag() is deprecated in Drupal 8.7.0 and will be removed in Drupal 9.0.0. Use \Drupal\Core\Render\Element\Table::attachTabledrag() instead. See https://www.drupal.org/node/3035565', E_USER_DEPRECATED); Table::attachTabledrag($element, $options); } diff --git a/core/lib/Drupal/Core/Render/Element/Table.php b/core/lib/Drupal/Core/Render/Element/Table.php index aa52b060b4..e33cb84623 100644 --- a/core/lib/Drupal/Core/Render/Element/Table.php +++ b/core/lib/Drupal/Core/Render/Element/Table.php @@ -60,6 +60,13 @@ */ class Table extends FormElement { + /** + * Counter for tables having draggable rows. + * + * @var int + */ + protected static $tableDragId = 0; + /** * {@inheritdoc} */ @@ -85,9 +92,9 @@ public function getInfo() { ], // Properties for tabledrag support. // The value is a list of arrays that are passed to - // drupal_attach_tabledrag(). Table::preRenderTable() prepends the HTML ID + // Table::attachTabledrag(). Table::preRenderTable() prepends the HTML ID // of the table to each set of options. - // @see drupal_attach_tabledrag() + // @see Table::attachTabledrag() '#tabledrag' => [], // Render properties. '#pre_render' => [ @@ -328,14 +335,14 @@ public static function validateTable(&$element, FormStateInterface $form_state, * @param array $element * A structured array containing two sub-levels of elements. Properties used: * - #tabledrag: The value is a list of $options arrays that are passed to - * drupal_attach_tabledrag(). The HTML ID of the table is added to each + * Table::attachTabledrag(). The HTML ID of the table is added to each * $options array. * * @return array * * @see template_preprocess_table() * @see \Drupal\Core\Render\AttachmentsResponseProcessorInterface::processAttachments() - * @see drupal_attach_tabledrag() + * @see attachTabledrag() */ public static function preRenderTable($element) { foreach (Element::children($element) as $first) { @@ -388,7 +395,7 @@ public static function preRenderTable($element) { if (!empty($element['#tabledrag']) && isset($element['#attributes']['id'])) { foreach ($element['#tabledrag'] as $options) { $options['table_id'] = $element['#attributes']['id']; - drupal_attach_tabledrag($element, $options); + self::attachTabledrag($element, $options); } } @@ -443,13 +450,13 @@ public static function preRenderTable($element) { * - Rows with the 'tabledrag-leaf' class cannot have child rows. * - Rows with the 'tabledrag-root' class cannot be nested under a parent row. * - * Calling drupal_attach_tabledrag() would then be written as such: + * Calling Table::attachTabledrag() would then be written as such: * @code - * drupal_attach_tabledrag('my-module-table', array( + * Table::attachTabledrag('my-module-table', [ * 'action' => 'order', * 'relationship' => 'sibling', * 'group' => 'my-elements-weight', - * ); + * ]; * @endcode * * In a more complex case where there are several groups in one column (such @@ -461,12 +468,12 @@ public static function preRenderTable($element) { * * The 'group' option is still 'my-element-weight', and the additional * 'subgroup' option will be passed in as 'my-elements-weight-' . $region. - * This also means that you'll need to call drupal_attach_tabledrag() once for + * This also means that you'll need to call Table::attachTabledrag() once for * every region added. * * @code * foreach ($regions as $region) { - * drupal_attach_tabledrag('my-module-table', [ + * Table::attachTabledrag('my-module-table', [ * 'action' => 'order', * 'relationship' => 'sibling', * 'group' => 'my-elements-weight', @@ -481,7 +488,7 @@ public static function preRenderTable($element) { * See MenuForm::BuildOverviewForm for an example creating a table * containing parent relationships. * - * @param $element + * @param array $element * A form element to attach the tableDrag behavior to. * @param array $options * These options are used to generate JavaScript settings necessary to @@ -518,7 +525,7 @@ public static function preRenderTable($element) { * - 'limit': (optional) Limit the maximum amount of parenting in this * table. * - * @see MenuForm::BuildOverviewForm() + * @see \Drupal\menu_ui\MenuForm::buildOverviewForm() */ public static function attachTabledrag(&$element, array $options) { // Add default values to elements. @@ -531,8 +538,7 @@ public static function attachTabledrag(&$element, array $options) { $group = $options['group']; - $tabledrag_id = &drupal_static(__FUNCTION__); - $tabledrag_id = (!isset($tabledrag_id)) ? 0 : $tabledrag_id + 1; + $tabledrag_id = self::$tableDragId++; // If a subgroup or source isn't set, assume it is the same as the group. $target = isset($options['subgroup']) ? $options['subgroup'] : $group; diff --git a/core/misc/tabledrag.es6.js b/core/misc/tabledrag.es6.js index 8045d128ad..ef9e50d872 100644 --- a/core/misc/tabledrag.es6.js +++ b/core/misc/tabledrag.es6.js @@ -22,7 +22,7 @@ /** * Drag and drop table rows with field manipulation. * - * Using the drupal_attach_tabledrag() function, any table with weights or + * Using the Table::attachTabledrag() function, any table with weights or * parent relationships may be made into draggable tables. Columns containing * a field may optionally be hidden, providing a better user experience. * diff --git a/core/modules/field_ui/src/Element/FieldUiTable.php b/core/modules/field_ui/src/Element/FieldUiTable.php index c038aedb39..ccf5b4d5f8 100644 --- a/core/modules/field_ui/src/Element/FieldUiTable.php +++ b/core/modules/field_ui/src/Element/FieldUiTable.php @@ -32,7 +32,7 @@ public function getInfo() { * A structured array containing two sub-levels of elements. Properties * used: * - #tabledrag: The value is a list of $options arrays that are passed to - * drupal_attach_tabledrag(). The HTML ID of the table is added to each + * Table::attachTabledrag(). The HTML ID of the table is added to each * $options array. * * @return array @@ -40,6 +40,7 @@ public function getInfo() { * * @see \Drupal\Core\Render\RendererInterface::render() * @see \Drupal\Core\Render\Element\Table::preRenderTable() + * @see \Drupal\Core\Render\Element\Table::attachTabledrag() */ public static function tablePreRender($elements) { $js_settings = []; @@ -130,7 +131,7 @@ public static function tablePreRender($elements) { * A structured array containing two sub-levels of elements. Properties * used: * - #tabledrag: The value is a list of $options arrays that are passed to - * drupal_attach_tabledrag(). The HTML ID of the table is added to each + * Table::attachTabledrag(). The HTML ID of the table is added to each * $options array. * * @return array diff --git a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php index ebdbaed2c7..e5f260ecce 100644 --- a/core/modules/field_ui/src/Form/EntityDisplayFormBase.php +++ b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php @@ -698,13 +698,14 @@ public function multistepAjax($form, FormStateInterface $form_state) { * A structured array containing two sub-levels of elements. Properties * used: * - #tabledrag: The value is a list of $options arrays that are passed to - * drupal_attach_tabledrag(). The HTML ID of the table is added to each + * Table::attachTabledrag(). The HTML ID of the table is added to each * $options array. * * @return array * * @see \Drupal\Core\Render\RendererInterface::render() * @see \Drupal\Core\Render\Element\Table::preRenderTable() + * @see \Drupal\Core\Render\Element\Table::attachTabledrag() * * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. */ diff --git a/core/tests/Drupal/KernelTests/Core/Render/RendererLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Render/RendererLegacyTest.php index 8cea7a1b90..9402f42b11 100644 --- a/core/tests/Drupal/KernelTests/Core/Render/RendererLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Render/RendererLegacyTest.php @@ -2,6 +2,7 @@ namespace Drupal\KernelTests\Core\Render; +use Drupal\Core\Render\Element\Table; use Drupal\Core\Render\HtmlResponseAttachmentsProcessor; use Drupal\KernelTests\KernelTestBase; @@ -37,4 +38,23 @@ public function providerAttributes() { ]; } + /** + * Tests deprecation of the drupal_attach_tabledrag() function. + * + * @expectedDeprecation drupal_attach_tabledrag() is deprecated in Drupal 8.7.0 and will be removed in Drupal 9.0.0. Use \Drupal\Core\Render\Element\Table::attachTabledrag() instead. See https://www.drupal.org/node/3035565 + */ + public function testTableDrag() { + $elements = []; + $options = [ + 'table_id' => 'test-table', + 'action' => 'match', + 'relationship' => 'sibling', + 'group' => 'test', + ]; + drupal_attach_tabledrag($elements, $options); + $expected = []; + Table::attachTabledrag($expected, $options); + $this->assertSame($expected['#attached']['drupalSettings']['tableDrag']['test-table']['test'][1], $elements['#attached']['drupalSettings']['tableDrag']['test-table']['test'][0]); + } + }