function _form_builder_add_element in Form Builder 7
Same name and namespace in other branches
- 6 includes/form_builder.admin.inc \_form_builder_add_element()
- 7.2 includes/form_builder.admin.inc \_form_builder_add_element()
Menu callback helper for adding or cloning a field.
If the $element_id parameter is provided, the new field will be cloned from the corresponding field (and the $element_type parameter will be ignored). Otherwise, a new field of type $element_type will be added from scratch.
5 calls to _form_builder_add_element()
- FormBuilderExamplesTest::testOptionsConfiguration in examples/
tests/ FormBuilderExamplesTest.php - FormBuilderFormBaseTest::test_form_builder_add_element in tests/
FormBuilderFormBaseTest.php - Integration test _form_builder_add_element().
- FormBuilderFormBaseTest::test_render_fieldset in tests/
FormBuilderFormBaseTest.php - Integration test: Render textfield inside fieldset.
- form_builder_add_page in includes/
form_builder.admin.inc - Menu callback for adding a field.
- form_builder_clone_page in includes/
form_builder.admin.inc - Menu callback for cloning a field.
File
- includes/
form_builder.admin.inc, line 56 - form_builder.admin.inc Administrative interface for editing forms.
Code
function _form_builder_add_element($form_type, $form_id, $element_type, $element_id = NULL, $sid = NULL, $return = FALSE) {
module_load_include('inc', 'form_builder', 'includes/form_builder.api');
module_load_include('inc', 'form_builder', 'includes/form_builder.cache');
$loader = FormBuilderLoader::instance();
$fields = $loader
->getElementTypeInfo($form_type, $form_id);
$form_obj = $loader
->fromCache($form_type, $form_id, $sid);
$element = NULL;
// Keep track of the number of elements added this requests.
$i =& drupal_static(__FUNCTION__, 1);
// If the field is being cloned, copy the original from the current form
// structure.
if (isset($element_id)) {
$element = $form_obj
->getElementArray($element_id);
if ($element) {
// @todo implement cloning as an element method.
// Remove the key so that a new one will be generated.
unset($element['#key']);
// Change the title to avoid confusion, and to avoid duplicate
// auto-generated machine names.
if (isset($element['#title'])) {
$element['#title'] = t('Copy of !title', array(
'!title' => $element['#title'],
));
}
// Set the element type to the correct value so it will be used
// consistently throughout this function.
$element_type = $element['#form_builder']['element_type'];
}
}
elseif (isset($fields[$element_type]['default'])) {
// @todo make this an element static method.
$element = $fields[$element_type]['default'];
}
if ($element) {
// @todo make this part of the element object's initialization.
$element_id = isset($_REQUEST['element_id']) ? $_REQUEST['element_id'] : 'new_' . REQUEST_TIME . $i++;
// Set the element ID to a hard-coded value if a unique field type.
if (isset($fields[$element_type]['unique']) && $fields[$element_type]['unique']) {
$element_id = $element_type;
}
$element['#key'] = $element_id;
$element['#form_builder']['element_id'] = $element_id;
$element['#form_builder']['is_new'] = TRUE;
$f = $form_obj
->getFormArray();
$element['#weight'] = count(element_children($f));
$element_id = $form_obj
->setElementArray($element, FORM_BUILDER_ROOT, TRUE);
$form_obj
->save();
if (isset($_REQUEST['js']) || $return) {
$form = drupal_get_form('form_builder_positions', $form_obj, $form_type, $form_id);
$data = array(
'formType' => $form_type,
'formId' => $form_id,
'elementId' => $element_id,
'html' => form_builder_field_render($form_type, $form_id, $element_id),
'positionForm' => drupal_render($form),
);
if ($return) {
return $data;
}
form_builder_json_output($data);
exit;
}
}
// Otherwise return to the previous page.
drupal_goto();
}