Permalink
Browse files

feature(forms): adds elgg_view_input() to the views api

Form fields can now be generated using elgg_view_input()

Fixes #6356
  • Loading branch information...
hypeJunction committed Oct 30, 2015
1 parent c3be8f7 commit 70b35bd731810f15330ae51cff5bffb940ba5601
View
@@ -1268,6 +1268,58 @@ function elgg_view_form($action, $form_vars = array(), $body_vars = array()) {
return elgg_view('input/form', $form_vars);
}
/**
* Renders a form field
*
* @param string $input_type Input type, used to generate an input view ("input/$input_type")
* @param array $vars Fields and input vars.
* Field vars contain both field and input params. 'label', 'help',
* and 'field_class' params will not be passed on to the input view.
* Others, including 'required' and 'id', will be available to the
* input view. Both 'label' and 'help' params accept HTML, and
* will be printed unescaped within their wrapper element.
* @return string
*/
function elgg_view_input($input_type, array $vars = array()) {
static $id_num;
if (!elgg_view_exists("input/$input_type")) {
return '';
}
$id = elgg_extract('id', $vars);
if (!$id) {
$id_num++;
$id = "elgg-field-$id_num";
$vars['id'] = $id;
}
$vars['input_type'] = $input_type;
$label = elgg_view('elements/forms/label', $vars);
unset($vars['label']);
$help = elgg_view('elements/forms/help', $vars);
unset($vars['help']);
$required = elgg_extract('required', $vars);
$field_class = (array) elgg_extract('field_class', $vars, array());
unset($vars['field_class']);
$input = elgg_view("elements/forms/input", $vars);
return elgg_view('elements/forms/field', array(
'label' => $label,
'help' => $help,
'required' => $required,
'id' => $id,
'input' => $input,
'class' => $field_class,
));
}
/**
* Create a tagcloud for viewing
*
View
@@ -1475,4 +1475,7 @@
"za" => "Zuang",
"zh" => "Chinese",
"zu" => "Zulu",
"field:required" => 'Required',
);
@@ -6,7 +6,7 @@
/* ***************************************
Form Elements
*************************************** */
fieldset > div {
fieldset > div, .elgg-field {
margin-bottom: 15px;
}
fieldset > div:last-child {
@@ -16,14 +16,24 @@
border-top: 1px solid #DCDCDC;
padding: 10px 0;
}
label {
label, .elgg-field-label {
font-weight: bold;
color: #333;
font-size: 110%;
}
label.elgg-state-disabled {
.elgg-field-label {
display: block;
}
label.elgg-state-disabled, .elgg-field-label.elgg-state-disabled {
opacity: 0.6;
}
.elgg-required-indicator {
font-size: 110%;
font-weight: bold;
color: #C24000;
display: inline;
padding: 0 5px;
}
input, textarea {
border: 1px solid #DCDCDC;
color: #666;
@@ -463,11 +463,22 @@
font-size: 110%;
}
label.elgg-state-disabled,
input.elgg-state-disabled {
input.elgg-state-disabled,
.elgg-field-label.elgg-state-disabled {
opacity: 0.6;
}
.elgg-field-label {
display: block;
}
.elgg-required-indicator {
font-size: 110%;
font-weight: bold;
color: #C24000;
display: inline;
padding: 0 5px;
}
fieldset > div {
fieldset > div, .elgg-field {
margin-bottom: 15px;
}
fieldset > div:last-child {
@@ -0,0 +1,31 @@
<?php
/**
* Form field view
*
* @uses $vars['input'] Form input element
* @uses $vars['id'] ID attribute of the input element
* @uses $vars['required'] Required or optional input
* @uses $vars['label'] HTML content of the label element
* @uses $vars['help'] HTML content of the help element
*/
$input = elgg_extract('input', $vars);
if (!$input) {
return;
}
$label = elgg_extract('label', $vars, '');
$help = elgg_extract('help', $vars, '');
$field_class = (array) elgg_extract('class', $vars, array());
$field_class[] = 'elgg-field';
if (elgg_extract('required', $vars)) {
$field_class[] = "elgg-field-required";
}
$field = $label . $input . $help;
echo elgg_format_element('div', [
'class' => $field_class,
], $field);
@@ -0,0 +1,15 @@
<?php
/**
* Form input help text view
*
* @uses $vars['help'] HTML content of the help element
*/
$help = elgg_extract('help', $vars, '');
if (!$help) {
return;
}
echo elgg_format_element('div', [
'class' => 'elgg-field-help elgg-text-help',
], $help);
@@ -0,0 +1,9 @@
<?php
/**
* Helper view that can be used to filter vars for all input views
*/
$input_type = elgg_extract('input_type', $vars);
unset($vars['input_type']);
echo elgg_view("input/$input_type", $vars);
@@ -0,0 +1,27 @@
<?php
/**
* Form input label view
*
* @uses $vars['label'] HTML content of the label element
* @uses $vars['id'] ID attribute of the input element
*/
$label = elgg_extract('label', $vars, '');
$id = elgg_extract('id', $vars);
$required = elgg_extract('required', $vars);
if (!$label) {
return;
}
if ($required) {
$label .= elgg_format_element('span', [
'title' => elgg_echo('field:required'),
'class' => 'elgg-required-indicator',
], "&ast;");
}
echo elgg_format_element('label', [
'for' => $id,
'class' => 'elgg-field-label'
], $label);

0 comments on commit 70b35bd

Please sign in to comment.