diff --git a/core/lib/Drupal/Core/Render/Element/Actions.php b/core/lib/Drupal/Core/Render/Element/Actions.php index ad85401ed9260d3050ea8ee431a448c2d59830e8..2e7516548e1fe490e5c32e369f882527c374d568 100644 --- a/core/lib/Drupal/Core/Render/Element/Actions.php +++ b/core/lib/Drupal/Core/Render/Element/Actions.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Render\Element; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\Element; /** @@ -103,6 +104,10 @@ public static function preRenderActionsDropbutton(&$element, FormStateInterface $dropbuttons[$dropbutton]['#links'][$key] = [ 'title' => $button, ]; + // Merge metadata like drupalSettings. + BubbleableMetadata::createFromRenderArray($dropbuttons[$dropbutton]) + ->merge(BubbleableMetadata::createFromRenderArray($element[$key])) + ->applyTo($dropbuttons[$dropbutton]); } } // @todo For now, all dropbuttons appear first. Consider to invent a more diff --git a/core/tests/Drupal/KernelTests/Core/Render/Element/ActionsTest.php b/core/tests/Drupal/KernelTests/Core/Render/Element/ActionsTest.php new file mode 100644 index 0000000000000000000000000000000000000000..135c7aabb192ff5f3f3f5ffd55a9b56851426597 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Render/Element/ActionsTest.php @@ -0,0 +1,70 @@ +<?php + +namespace Drupal\KernelTests\Core\Render\Element; + +use Drupal\Core\Form\FormInterface; +use Drupal\Core\Form\FormStateInterface; +use Drupal\KernelTests\KernelTestBase; + +/** + * @coversDefaultClass \Drupal\Core\Render\Element\Actions + * @group Render + */ +class ActionsTest extends KernelTestBase implements FormInterface { + + /** + * {@inheritdoc} + */ + public static $modules = ['system']; + + /** + * {@inheritdoc} + */ + public function getFormId() { + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form['actions'] = ['#type' => 'actions']; + $form['actions']['key'] = [ + '#type' => 'submit', + '#value' => 'Key', + '#dropbutton' => 'submit', + '#cache' => [ + 'tags' => ['foo'], + ], + '#attached' => [ + 'library' => [ + 'system/base', + ], + ], + ]; + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => 'Save', + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + } + + public function testDropbuttonWithBubbleableMetadata() { + $result = \Drupal::formBuilder()->getForm($this); + \Drupal::service('renderer')->renderRoot($result); + $this->assertEquals(['system/base', 'core/drupal.dropbutton'], $result['#attached']['library']); + $this->assertEquals(['foo'], $result['#cache']['tags']); + } + +}