Dropbutton.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. /**
  4. * Provides a render element for a set of links rendered as a drop-down button.
  5. *
  6. * By default, this element sets #theme so that the 'links' theme hook is used
  7. * for rendering, with suffixes so that themes can override this specifically
  8. * without overriding all links theming. If the #subtype property is provided in
  9. * your render array with value 'foo', #theme is set to links__dropbutton__foo;
  10. * if not, it's links__dropbutton; both of these can be overridden by setting
  11. * the #theme property in your render array. See template_preprocess_links()
  12. * for documentation on the other properties used in theming; for instance, use
  13. * element property #links to provide $variables['links'] for theming.
  14. *
  15. * Properties:
  16. * - #links: An array of links to actions. See template_preprocess_links() for
  17. * documentation the properties of links in this array.
  18. * - #dropbutton_type: A string defining a type of dropbutton variant for
  19. * styling proposes. Renders as class `dropbutton--#dropbutton_type`.
  20. *
  21. * Usage Example:
  22. * @code
  23. * $form['actions']['extra_actions'] = array(
  24. * '#type' => 'dropbutton',
  25. * '#dropbutton_type' => 'small',
  26. * '#links' => array(
  27. * 'simple_form' => array(
  28. * 'title' => $this->t('Simple Form'),
  29. * 'url' => Url::fromRoute('fapi_example.simple_form'),
  30. * ),
  31. * 'demo' => array(
  32. * 'title' => $this->t('Build Demo'),
  33. * 'url' => Url::fromRoute('fapi_example.build_demo'),
  34. * ),
  35. * ),
  36. * );
  37. * @endcode
  38. *
  39. * @see \Drupal\Core\Render\Element\Operations
  40. *
  41. * @RenderElement("dropbutton")
  42. */
  43. class Dropbutton extends RenderElement {
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getInfo() {
  48. $class = get_class($this);
  49. return [
  50. '#pre_render' => [
  51. [$class, 'preRenderDropbutton'],
  52. ],
  53. '#theme' => 'links__dropbutton',
  54. ];
  55. }
  56. /**
  57. * Pre-render callback: Attaches the dropbutton library and required markup.
  58. */
  59. public static function preRenderDropbutton($element) {
  60. $element['#attached']['library'][] = 'core/drupal.dropbutton';
  61. $element['#attributes']['class'][] = 'dropbutton';
  62. if (!empty($element['#dropbutton_type'])) {
  63. $element['#attributes']['class'][] = 'dropbutton--' . $element['#dropbutton_type'];
  64. }
  65. if (!isset($element['#theme_wrappers'])) {
  66. $element['#theme_wrappers'] = [];
  67. }
  68. array_unshift($element['#theme_wrappers'], 'dropbutton_wrapper');
  69. // Enable targeted theming of specific dropbuttons (e.g., 'operations' or
  70. // 'operations__node').
  71. if (isset($element['#subtype'])) {
  72. $element['#theme'] .= '__' . $element['#subtype'];
  73. }
  74. return $element;
  75. }
  76. }