3 namespace Drupal\Core\Render\Element;
6 * Provides a render element for a set of links rendered as a drop-down button.
8 * By default, this element sets #theme so that the 'links' theme hook is used
9 * for rendering, with suffixes so that themes can override this specifically
10 * without overriding all links theming. If the #subtype property is provided in
11 * your render array with value 'foo', #theme is set to links__dropbutton__foo;
12 * if not, it's links__dropbutton; both of these can be overridden by setting
13 * the #theme property in your render array. See template_preprocess_links()
14 * for documentation on the other properties used in theming; for instance, use
15 * element property #links to provide $variables['links'] for theming.
18 * - #links: An array of links to actions. See template_preprocess_links() for
19 * documentation the properties of links in this array.
23 * $form['actions']['extra_actions'] = array(
24 * '#type' => 'dropbutton',
26 * 'simple_form' => array(
27 * 'title' => $this->t('Simple Form'),
28 * 'url' => Url::fromRoute('fapi_example.simple_form'),
31 * 'title' => $this->t('Build Demo'),
32 * 'url' => Url::fromRoute('fapi_example.build_demo'),
38 * @see \Drupal\Core\Render\Element\Operations
40 * @RenderElement("dropbutton")
42 class Dropbutton extends RenderElement {
47 public function getInfo() {
48 $class = get_class($this);
51 [$class, 'preRenderDropbutton'],
53 '#theme' => 'links__dropbutton',
58 * Pre-render callback: Attaches the dropbutton library and required markup.
60 public static function preRenderDropbutton($element) {
61 $element['#attached']['library'][] = 'core/drupal.dropbutton';
62 $element['#attributes']['class'][] = 'dropbutton';
63 if (!isset($element['#theme_wrappers'])) {
64 $element['#theme_wrappers'] = [];
66 array_unshift($element['#theme_wrappers'], 'dropbutton_wrapper');
68 // Enable targeted theming of specific dropbuttons (e.g., 'operations' or
69 // 'operations__node').
70 if (isset($element['#subtype'])) {
71 $element['#theme'] .= '__' . $element['#subtype'];