diff --git a/core/lib/Drupal/Core/Render/Element/Splitbutton.php b/core/lib/Drupal/Core/Render/Element/Splitbutton.php index 7d927aa960..f3e350c1ec 100644 --- a/core/lib/Drupal/Core/Render/Element/Splitbutton.php +++ b/core/lib/Drupal/Core/Render/Element/Splitbutton.php @@ -40,6 +40,29 @@ * - #dropbutton_type: The value is copied or appended to #splitbutton_type. * This will be removed in Drupal 10. * + * Usage Example: + * @code + * $form['actions']['splitbutton_actions'] = [ + * '#type' => 'splitbutton', + * '#splitbutton_type' => 'small', + * '#splitbutton_items' => [ + * 'added_link' => [ + * '#type' => 'link', + * '#title' => $this->t('Simple Form'), + * '#url' => Url::fromRoute('route.for.thelink'), + * ], + * 'added_button' => [ + * '#type' => 'button', + * '#value' => $this->t('Added Button'), + * ], + * 'another_added_button' => [ + * '#type' => 'submit', + * '#value' => $this->t('Another Added Button'), + * ], + * ], + * ]; + * @endcode + * * @RenderElement("splitbutton") */ class Splitbutton extends FormElement { diff --git a/core/misc/splitbutton/splitbutton-init.es6.js b/core/misc/splitbutton/splitbutton-init.es6.js index 6304738f1a..bbf4a6756c 100644 --- a/core/misc/splitbutton/splitbutton-init.es6.js +++ b/core/misc/splitbutton/splitbutton-init.es6.js @@ -17,11 +17,9 @@ const $splitbuttons = $(context) .find('[data-drupal-splitbutton-multiple]') .once('splitbutton'); - if ($splitbuttons.length) { - for (let i = 0; i < $splitbuttons.length; i++) { - Drupal.splitbuttons.push(new Drupal.SplitButton($splitbuttons[i])); - } - } + $splitbuttons.map((index, splitbutton) => + Drupal.splitbuttons.push(new Drupal.SplitButton(splitbutton)), + ); }, }; diff --git a/core/misc/splitbutton/splitbutton-init.js b/core/misc/splitbutton/splitbutton-init.js index b09c1963f1..d7f6cfcd84 100644 --- a/core/misc/splitbutton/splitbutton-init.js +++ b/core/misc/splitbutton/splitbutton-init.js @@ -9,12 +9,9 @@ Drupal.behaviors.SplitButton = { attach: function attach(context) { var $splitbuttons = $(context).find('[data-drupal-splitbutton-multiple]').once('splitbutton'); - - if ($splitbuttons.length) { - for (var i = 0; i < $splitbuttons.length; i++) { - Drupal.splitbuttons.push(new Drupal.SplitButton($splitbuttons[i])); - } - } + $splitbuttons.map(function (index, splitbutton) { + return Drupal.splitbuttons.push(new Drupal.SplitButton(splitbutton)); + }); } }; Drupal.splitbuttons = []; diff --git a/core/tests/Drupal/KernelTests/Component/Render/SplitbuttonDeprecationTest.php b/core/tests/Drupal/KernelTests/Component/Render/SplitbuttonDeprecationTest.php new file mode 100644 index 0000000000..64de2aaf7f --- /dev/null +++ b/core/tests/Drupal/KernelTests/Component/Render/SplitbuttonDeprecationTest.php @@ -0,0 +1,69 @@ + 'splitbutton', + '#dropbutton_type' => 'small', + '#links' => [ + 'one' => [ + 'title' => 'one', + 'url' => Url::fromRoute('splitbutton.test_link_1'), + ], + 'two' => [ + 'title' => 'two', + 'url' => Url::fromRoute('splitbutton.test_link_2'), + ], + ], + ]; + + $plugin_definition = [ + 'id' => 'splitbutton', + 'class' => 'Drupal\Core\Render\Element\Splitbutton', + 'provider' => 'core', + ]; + + $splitbutton_element = new Splitbutton([], 'splitbutton', $plugin_definition); + $splitbutton = $splitbutton_element::preRenderSplitbutton($element_with_deprecated_properties); + + $expected_array_keys = [ + '#type', + '#dropbutton_type', + '#links', + '#variants', + '#trigger_id', + '#toggle_attributes', + '#main_element', + '#splitbutton_item_list', + '#splitbutton_multiple', + '#attributes', + ]; + + $this->assertEquals($expected_array_keys, array_keys($splitbutton)); + } + +}