Elgg  Version master
FormsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
7 use Elgg\Traits\Loggable;
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 }
$body
Definition: useradd.php:55
if(!elgg_view_exists("plugins/{$plugin_id}/settings")) $form_vars
Events service.
Exception that represents error in the program logic.
Forms service.
getFooter()
Returns currently set footer.
setFooter(string $footer='')
Sets form footer and defers its rendering until the form view and extensions have been rendered.
__construct(protected ViewsService $views, protected EventsService $events, protected ESMService $esm)
Constructor.
render(string $action, array $form_vars=[], array $body_vars=[])
Keeps track of ES modules.
Definition: ESMService.php:14
Views service.
$views
Definition: item.php:17
$body_vars
$footer
Wrap form footer.
Definition: footer.php:11
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:279
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:256
$defaults
Generic entity header upload helper.
Definition: header.php:6
elgg_view(string $view, array $vars=[], string $viewtype='')
Return a parsed view.
Definition: views.php:156
elgg_generate_action_url(string $action, array $query=[], bool $add_csrf_tokens=true)
Generate an action URL.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$action
Definition: subscribe.php:11