Elgg  Version 2.3
FormsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
13 class FormsService {
14 
18  private $views;
19 
23  private $logger;
24 
28  private $rendering;
29 
33  private $footer = '';
34 
41  public function __construct(ViewsService $views, Logger $logger) {
42  $this->views = $views;
43  $this->logger = $logger;
44  }
45 
80  public function render($action, $form_vars = array(), $body_vars = array()) {
81 
82  $defaults = array(
83  'action' => elgg_normalize_url("action/$action"),
84  );
85 
86  // append elgg-form class to any class options set
87  $form_vars['class'] = (array) elgg_extract('class', $form_vars, []);
88  $form_vars['class'][] = 'elgg-form-' . preg_replace('/[^a-z0-9]/i', '-', $action);
89 
90  $form_vars = array_merge($defaults, $form_vars);
91 
92  $form_vars['action_name'] = $action;
93 
94  if (!isset($form_vars['body'])) {
95  $this->rendering = true;
96  $this->footer = '';
97 
98  // Render form body
99  $body = $this->views->renderView("forms/$action", $body_vars);
100 
101  if (!empty($body)) {
102  // Grab the footer if one was set during form rendering
103  $body .= $this->views->renderView('elements/forms/footer', [
104  'footer' => $this->getFooter(),
105  'action_name' => $action,
106  ]);
107  }
108 
109  $this->rendering = false;
110 
111  $form_vars['body'] = $body;
112  }
113 
114  return elgg_view('input/form', $form_vars);
115  }
116 
126  public function setFooter($footer = '') {
127 
128  if (!$this->rendering) {
129  $this->logger->error('Form footer can only be set and retrieved during form rendering, '
130  . 'anywhere in elgg_view_form() call stack (e.g. form view, extending views, or view hooks)');
131  return false;
132  }
133 
134  $this->footer = $footer;
135  return true;
136  }
137 
142  public function getFooter() {
143  if (!$this->rendering) {
144  $this->logger->error('Form footer can only be set and retrieved during form rendering, '
145  . 'anywhere in elgg_view_form() call stack (e.g. form view, extending views, or view hooks)');
146  return false;
147  }
148 
149  return $this->footer;
150  }
151 
152 }
$action
Definition: full.php:133
elgg_normalize_url($url)
Definition: output.php:280
$footer
Wrap form footer $vars[&#39;footer&#39;] Form footer $vars[&#39;action_name&#39;] Action name.
Definition: footer.php:8
$defaults
__construct(ViewsService $views, Logger $logger)
Constructor.
WARNING: API IN FLUX.
Save menu items.
$body_vars
WARNING: API IN FLUX.
setFooter($footer= '')
Sets form footer and defers its rendering until the form view and extensions have been rendered...
elgg_view($view, $vars=array(), $ignore1=false, $ignore2=false, $viewtype= '')
Return a parsed view.
Definition: views.php:336
if(!elgg_instanceof($comment, 'object', 'comment')||!$comment->canEdit()) $form_vars
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1375
render($action, $form_vars=array(), $body_vars=array())
getFooter()
Returns currently set footer, or false if not in the form rendering stack.
clearfix elgg elgg elgg elgg page elgg page footer
Definition: admin.css.php:127