]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Fixes #2616 Adds Evan's view form convenience function
authorcash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sun, 5 Dec 2010 19:41:42 +0000 (19:41 +0000)
committercash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544>
Sun, 5 Dec 2010 19:41:42 +0000 (19:41 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@7539 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/views.php

index 70b4b833d894ac722fac63fadb3e788777d7fa1b..5d7c210024d229157a72165059c6650c847db44f 100644 (file)
@@ -1005,6 +1005,47 @@ function elgg_view_listing($icon, $info) {
        return elgg_view('entities/entity_listing', array('icon' => $icon, 'info' => $info));
 }
 
+/**
+ * Convenience function for generating a form from a view in a standard location.
+ *
+ * This function assumes that the body of the form is located at "forms/$action" and
+ * sets the action by default to "action/$action".  Automatically wraps the forms/$action
+ * view with a <form> tag and inserts the anti-csrf security tokens.
+ *
+ * @example
+ * <code>echo elgg_view_form('login');</code>
+ *
+ * This would assume a "login" form body to be at "forms/login" and would set the action
+ * of the form to "http://yoursite.com/action/login".
+ *
+ * If elgg_view('forms/login') is:
+ * <input type="text" name="username" />
+ * <input type="password" name="password" />
+ *
+ * Then elgg_view_form('login') generates:
+ * <form action="http://yoursite.com/action/login" method="POST">
+ *     ...security tokens...
+ *     <input type="text" name="username" />
+ *     <input type="password" name="password" />
+ * </form>
+ *
+ * @param string $action    The name of the action (without the leading "action/") -- e.g. "login"
+ * @param array  $form_vars $vars environment passed to the "input/form" view
+ * @param array  $body_vars $vars environment passed to the "forms/$action" view
+ *
+ * @return string The complete form
+ */
+function elgg_view_form($action, $form_vars = array(), $body_vars = array()) {
+       global $CONFIG;
+
+       $defaults = array(
+               'action' => $CONFIG->wwwroot . "action/$action",
+               'body' => elgg_view("forms/$action", $body_vars),
+       );
+
+       return elgg_view('input/form', array_merge($defaults, $form_vars));
+}
+
 /**
  * Registers a function to handle templates.
  *