TwigExtension.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php declare(strict_types=1);
  2. namespace Grav\Plugin\Form;
  3. use Grav\Framework\Form\Interfaces\FormInterface;
  4. use Twig\Extension\AbstractExtension;
  5. use Twig\TwigFilter;
  6. use Twig\TwigFunction;
  7. use function is_string;
  8. /**
  9. * Class GravExtension
  10. * @package Grav\Common\Twig\Extension
  11. */
  12. class TwigExtension extends AbstractExtension
  13. {
  14. public function getFilters()
  15. {
  16. return [
  17. new TwigFilter('value_and_label', [$this, 'valueAndLabel'])
  18. ];
  19. }
  20. /**
  21. * Return a list of all functions.
  22. *
  23. * @return array
  24. */
  25. public function getFunctions(): array
  26. {
  27. return [
  28. new TwigFunction('prepare_form_fields', [$this, 'prepareFormFields'], ['needs_context' => true]),
  29. new TwigFunction('prepare_form_field', [$this, 'prepareFormField'], ['needs_context' => true]),
  30. new TwigFunction('include_form_field', [$this, 'includeFormField']),
  31. ];
  32. }
  33. public function valueAndLabel($value): array
  34. {
  35. if (!is_array($value)) {
  36. return [];
  37. }
  38. $list = [];
  39. foreach ($value as $key => $label) {
  40. $list[] = ['value' => $key, 'label' => $label];
  41. }
  42. return $list;
  43. }
  44. /**
  45. * Filters form fields for the current parent.
  46. *
  47. * @param array $context
  48. * @param array $fields Form fields
  49. * @param string|null $parent Parent field name if available
  50. * @return array
  51. */
  52. public function prepareFormFields(array $context, $fields, $parent = null): array
  53. {
  54. $list = [];
  55. if (is_iterable($fields)) {
  56. foreach ($fields as $name => $field) {
  57. $field = $this->prepareFormField($context, $field, $name, $parent);
  58. if ($field) {
  59. $list[$field['name']] = $field;
  60. }
  61. }
  62. }
  63. return $list;
  64. }
  65. /**
  66. * Filters field name by changing dot notation into array notation.
  67. *
  68. * @param array $context
  69. * @param array $field Form field
  70. * @param string|int|null $name Field name (defaults to field.name)
  71. * @param string|null $parent Parent field name if available
  72. * @param array|null $options List of options to override
  73. * @return array|null
  74. */
  75. public function prepareFormField(array $context, $field, $name = null, $parent = null, array $options = []): ?array
  76. {
  77. // Make sure that the field is a valid form field type and is not being ignored.
  78. if (empty($field['type']) || ($field['validate']['ignore'] ?? false)) {
  79. return null;
  80. }
  81. // Check if we have just a list of fields (no name given).
  82. if (is_int($name)) {
  83. // Look at the field.name and if not set, fall back to the key.
  84. $name = (string)($field['name'] ?? $name);
  85. }
  86. // Make sure that the field has a name.
  87. $name = $name ?? $field['name'] ?? null;
  88. if (!is_string($name) || $name === '') {
  89. return null;
  90. }
  91. // Prefix name with the parent name if needed.
  92. if (str_starts_with($name, '.')) {
  93. $name = $parent ? $parent . $name : (string)substr($name, 1);
  94. } elseif (isset($options['key'])) {
  95. $name = str_replace('*', $options['key'], $name);
  96. }
  97. unset($options['key']);
  98. // Set fields as readonly if form is in readonly mode.
  99. /** @var FormInterface $form */
  100. $form = $context['form'] ?? null;
  101. if ($form && method_exists($form, 'isEnabled') && !$form->isEnabled()) {
  102. $options['disabled'] = true;
  103. }
  104. // Loop through options
  105. foreach ($options as $key => $option) {
  106. $field[$key] = $option;
  107. }
  108. // Always set field name.
  109. $field['name'] = $name;
  110. return $field;
  111. }
  112. /**
  113. * @param string $type
  114. * @param string|string[]|null $layouts
  115. * @param string|null $default
  116. * @return string[]
  117. */
  118. public function includeFormField(string $type, $layouts = null, string $default = null): array
  119. {
  120. $list = [];
  121. foreach ((array)$layouts as $layout) {
  122. $list[] = "forms/fields/{$type}/{$layout}-{$type}.html.twig";
  123. }
  124. $list[] = "forms/fields/{$type}/{$type}.html.twig";
  125. if ($default) {
  126. foreach ((array)$layouts as $layout) {
  127. $list[] = "forms/fields/{$default}/{$layout}-{$default}.html.twig";
  128. }
  129. $list[] = "forms/fields/{$default}/{$default}.html.twig";
  130. }
  131. return $list;
  132. }
  133. }