Elgg  Version master
FormsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
8 
15 class FormsService {
16 
17  use Loggable;
18 
19  protected bool $rendering = false;
20 
21  protected string $footer = '';
22 
30  public function __construct(
31  protected ViewsService $views,
32  protected EventsService $events,
33  protected ESMService $esm
34  ) {
35  }
36 
72  public function render(string $action, array $form_vars = [], array $body_vars = []): string {
73 
74  $defaults = [
75  'action' => elgg_generate_action_url($action, [], false),
76  'method' => 'post',
77  'ajax' => false,
78  'sticky_enabled' => false,
79  'sticky_form_name' => $action,
80  'sticky_ignored_fields' => [],
81  ];
82 
83  // append elgg-form class to any class options set
85  'elgg-form-' . preg_replace('/[^a-z0-9]/i', '-', $action)],
86  );
87 
88  $form_vars = array_merge($defaults, $form_vars);
89 
90  if (!isset($form_vars['enctype']) && strtolower($form_vars['method']) == 'post') {
91  $form_vars['enctype'] = 'multipart/form-data';
92  }
93 
94  if (elgg_extract('ajax', $form_vars)) {
95  $this->esm->import('input/form-ajax');
96  $form_vars['class'][] = 'elgg-js-ajax-form';
97  unset($form_vars['ajax']);
98  }
99 
100  $form_vars['action_name'] = $action;
101 
102  $form_vars['ignore_empty_body'] = (bool) elgg_extract('ignore_empty_body', $form_vars, false);
103 
104  $form_vars['prevent_double_submit'] = (bool) elgg_extract('prevent_double_submit', $form_vars, true);
105 
106  if (!isset($form_vars['body'])) {
107  // prepare body vars
108  $body_vars = (array) $this->events->triggerResults('form:prepare:fields', $action, $form_vars, $body_vars);
109 
110  $this->rendering = true;
111  $this->footer = '';
112 
113  // Render form body
114  $body = $this->views->renderView("forms/{$action}", $body_vars);
115 
116  if (!empty($body)) {
117  // wrap form body
118  $body = $this->views->renderView('elements/forms/body', [
119  'body' => $body,
120  'action_name' => $action,
121  'body_vars' => $body_vars,
122  'form_vars' => $form_vars,
123  ]);
124 
125  // Grab the footer if one was set during form rendering
126  $body .= $this->views->renderView('elements/forms/footer', [
127  'footer' => $this->getFooter(),
128  'action_name' => $action,
129  'body_vars' => $body_vars,
130  'form_vars' => $form_vars,
131  ]);
132  }
133 
134  $this->rendering = false;
135 
136  $form_vars['body'] = $body;
137  }
138 
139  return elgg_view('input/form', $form_vars);
140  }
141 
152  public function setFooter(string $footer = ''): void {
153  if (!$this->rendering) {
154  throw new LogicException('Form footer can only be set and retrieved during form rendering, anywhere in elgg_view_form() call stack (e.g. form view, extending views, or view events)');
155  }
156 
157  $this->footer = $footer;
158  }
159 
166  public function getFooter(): string {
167  if (!$this->rendering) {
168  throw new LogicException('Form footer can only be set and retrieved during form rendering, anywhere in elgg_view_form() call stack (e.g. form view, extending views, or view events)');
169  }
170 
171  return $this->footer;
172  }
173 }
if(!elgg_view_exists("plugins/{$plugin_id}/settings")) $form_vars
elgg_generate_action_url(string $action, array $query=[], bool $add_csrf_tokens=true)
Generate an action URL.
__construct(protected ViewsService $views, protected EventsService $events, protected ESMService $esm)
Constructor.
$footer
Wrap form footer.
Definition: footer.php:11
$defaults
Generic entity header upload helper.
Definition: header.php:6
Keeps track of ES modules.
Definition: ESMService.php:14
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
Events service.
Forms service.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:156
setFooter(string $footer= '')
Sets form footer and defers its rendering until the form view and extensions have been rendered...
render(string $action, array $form_vars=[], array $body_vars=[])
$body_vars
Views service.
Exception that represents error in the program logic.
$body
Definition: useradd.php:55
$action
Definition: subscribe.php:11
getFooter()
Returns currently set footer.
elgg_extract_class(array $array, array|string $existing=[], string $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:277
$views
Definition: item.php:17