Elgg  Version 5.1
FormsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
7 
14 class FormsService {
15 
16  use Loggable;
17 
21  protected $events;
22 
26  protected $views;
27 
31  protected $rendering;
32 
36  protected $footer = '';
37 
44  public function __construct(ViewsService $views, EventsService $events) {
45  $this->views = $views;
46  $this->events = $events;
47  }
48 
84  public function render(string $action, array $form_vars = [], array $body_vars = []): string {
85 
86  $defaults = [
87  'action' => elgg_generate_action_url($action, [], false),
88  'method' => 'post',
89  'ajax' => false,
90  'sticky_enabled' => false,
91  'sticky_form_name' => $action,
92  'sticky_ignored_fields' => [],
93  ];
94 
95  // append elgg-form class to any class options set
97  'elgg-form-' . preg_replace('/[^a-z0-9]/i', '-', $action)],
98  );
99 
100  $form_vars = array_merge($defaults, $form_vars);
101 
102  if (!isset($form_vars['enctype']) && strtolower($form_vars['method']) == 'post') {
103  $form_vars['enctype'] = 'multipart/form-data';
104  }
105 
106  if (elgg_extract('ajax', $form_vars)) {
107  $form_vars['class'][] = 'elgg-js-ajax-form';
108  unset($form_vars['ajax']);
109  }
110 
111  $form_vars['action_name'] = $action;
112 
113  $form_vars['ignore_empty_body'] = (bool) elgg_extract('ignore_empty_body', $form_vars, false);
114 
115  $form_vars['prevent_double_submit'] = (bool) elgg_extract('prevent_double_submit', $form_vars, true);
116 
117  if (!isset($form_vars['body'])) {
118  // prepare body vars
119  $body_vars = (array) $this->events->triggerResults('form:prepare:fields', $action, $form_vars, $body_vars);
120 
121  $this->rendering = true;
122  $this->footer = '';
123 
124  // Render form body
125  $body = $this->views->renderView("forms/{$action}", $body_vars);
126 
127  if (!empty($body)) {
128  // Grab the footer if one was set during form rendering
129  $body .= $this->views->renderView('elements/forms/footer', [
130  'footer' => $this->getFooter(),
131  'action_name' => $action,
132  'body_vars' => $body_vars,
133  'form_vars' => $form_vars,
134  ]);
135  }
136 
137  $this->rendering = false;
138 
139  $form_vars['body'] = $body;
140  }
141 
142  return elgg_view('input/form', $form_vars);
143  }
144 
155  public function setFooter(string $footer = ''): void {
156  if (!$this->rendering) {
157  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)');
158  }
159 
160  $this->footer = $footer;
161  }
162 
169  public function getFooter(): string {
170  if (!$this->rendering) {
171  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)');
172  }
173 
174  return $this->footer;
175  }
176 }
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.
$footer
Wrap form footer.
Definition: footer.php:11
$defaults
Generic entity header upload helper.
Definition: header.php:6
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_class(array $array, $existing=[], $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:276
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:177
setFooter(string $footer= '')
Sets form footer and defers its rendering until the form view and extensions have been rendered...
__construct(ViewsService $views, EventsService $events)
Constructor.
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.
$views
Definition: item.php:17