function arrange_fields_add_draggable_wrappers in Arrange Fields 6
Same name and namespace in other branches
- 7 arrange_fields.module \arrange_fields_add_draggable_wrappers()
In this function, we are going to add div's around elements we wish to make draggable on the page. The jQuery can then grab onto those divs to make them draggable.
Parameters
array $form:
string $form_type:
bool $disable_buttons:
1 call to arrange_fields_add_draggable_wrappers()
- arrange_fields_form_alter in ./
arrange_fields.module - Implementation of hook_form_alter().
File
- ./
arrange_fields.module, line 407
Code
function arrange_fields_add_draggable_wrappers(&$form, $form_type, $disable_buttons = FALSE) {
// Figure out just what are the fields for this form.
if ($form_type == "webform") {
// If we are dealing with a webform, then we need to make sure
// we get every element in $form["submitted"], as well as the submit
// buttons. I am doing it this way because webform sometimes represents
// its fields in a way that the arrange_fields_get_form_fields cannot
// find.
$fields = array();
$fields[] = array(
"name" => "submit",
"element" => &$form["submit"],
);
$fields[] = array(
"name" => "next",
"element" => &$form["next"],
);
$fields[] = array(
"name" => "previous",
"element" => &$form["previous"],
);
foreach ($form["submitted"] as $name => &$element) {
if (is_array($element)) {
$fields[] = array(
"name" => $name,
"element" => &$element,
);
}
}
// If the user is using webform 3.x, there may also be an "actions"
// array element, which contains our buttons.
if (is_array($form["actions"])) {
foreach ($form["actions"] as $name => &$element) {
if (is_array($element)) {
$fields[] = array(
"name" => $name,
"element" => &$element,
);
}
}
}
// We may need the webform's node later, so let's load it now.
if (isset($form["details"]["nid"]["#value"])) {
$webform_nid = $form["details"]["nid"]["#value"];
$webform_node = node_load($webform_nid);
}
// If using the captcha module, add that element as well.
if (is_array($form["captcha"])) {
$fields[] = array(
"name" => "captcha",
"element" => &$form["captcha"],
);
}
// If using the mollom captcha module, add that element as well
if (isset($form["mollom"]) && is_array($form["mollom"])) {
$fields[] = array(
"name" => "mollom",
"element" => &$form["mollom"],
);
}
}
else {
// Not using webform, so we can use the function "get_form_fields()".
$fields = arrange_fields_get_form_fields($form);
}
// Make each field draggable by adding wrappers to it.
foreach ($fields as $field_arr) {
$field_name = $field_arr["name"];
$element =& $field_arr["element"];
// We need to make sure that field_name does not contain trouble characters, like spaces.
// The Profile module lets you create fieldsets with spaces in it, for example.
$field_name = preg_replace("/[^a-zA-Z0-9]/", "-", $field_name);
if ($element["#arrange_fields_added_wrappers"] == TRUE) {
// We have already added wrappers to this element. This can possibly
// happen when we are working on Webforms, based on the way the logic works,
// we can have the same elements in there twice.
continue;
}
// Make sure we are not still looking at a hidden or otherwise unwanted
// field which might be present because of the way I am doing Webform fields.
if ($element["#type"] == "hidden" || $element["#type"] == "token" || $element["#type"] == "value") {
// Just skip to the next one.
continue;
}
$field_type = $element["#type"];
$edit_link = "";
$edit_link = "<div class='arrange-fields-control-handle'>";
// If this is a CCK field, give it an option to edit in a popup.
if ($GLOBALS["arrange_fields_editing_type"] == $form_type && $form_type != "" && substr($field_name, 0, 6) == "field_") {
$edit_link .= "<a href='javascript: arrangeFieldsPopupEditField(\"{$form_type}\", \"{$field_name}\");'>cck</a>";
}
// If this is a webform field, give it an option to edit in a popup.
if ($form_type == "webform" && is_array($webform_node->webform["components"])) {
// We need to figure out the webform_component (cid) of this field.
// We will do this by scanning the webform["components"] array for
// this field_name
$cid = FALSE;
foreach ($webform_node->webform["components"] as $tcid => $c_arr) {
if ($c_arr["form_key"] == $field_name) {
$cid = $tcid;
break;
}
}
if ($cid) {
$webform_field_id = $webform_nid . "_" . $cid;
$edit_link .= "<a href='javascript: arrangeFieldsPopupEditField(\"{$form_type}\", \"{$webform_field_id}\");'>wf</a>";
}
}
$edit_link .= "<span class='arrange-fields-handle-region'> </span>\n <a href='javascript: arrangeFieldsDialogConfigureField(\"{$field_name}\",\"{$field_type}\");' class='arrange-fields-config-link' \n title='Configure this field'> </a>";
$edit_link .= "</div>";
$css_class = "draggable-form-item";
$css_id = "edit-{$field_name}-draggable-wrapper";
// Are we dealing with a fieldset?**
if ($element["#type"] == "fieldset" && $field_name != "captcha") {
// Fieldsets get an extra css_class and the ID changes.
$css_class .= " draggable-form-item-fieldset";
$css_id = "edit-{$field_name}-fieldset-draggable-wrapper";
}
// **The bit about captcha is a hack put in to ensure that Captcha is NOT considered a fieldset.
// It seems that at different times during form rendering, the #type property
// for it sometimes has "fieldset" and sometimes does not, possibly depending
// on other modules installed in the system. To make it consistent, we are not
// going to see it as a fieldset, as that is how it appears for most systems.
// Set the prefix and suffix for this field such that we
// wrap a div around it which the jquery can lock onto later to
// make it draggable.
$element["#prefix"] .= "<div id='{$css_id}' class='{$css_class}'>{$edit_link}";
$element["#suffix"] .= "</div>";
// If the field is a button, and we have chosen to disable buttons, then
// disable this one.
if ($element["#type"] == "submit" || $element["#type"] == "button") {
if ($disable_buttons) {
$element["#attributes"]["disabled"] = "disabled";
$element["#attributes"]["class"] .= " disabled-button ";
}
}
// This is essentially the same disable buttons code, but for CCK content types,
// since all those buttons are in a [buttons] element.
if ($field_name == "buttons" && !isset($element["#type"]) && $disable_buttons) {
foreach ($element as &$e) {
if (is_array($e)) {
$e["#attributes"]["disabled"] = "disabled";
$e["#attributes"]["class"] .= " disabled-button ";
}
}
}
// Indicate that we have successfully added all the necessary wrappers
// to this element. This is so we don't do it twice.
$element["#arrange_fields_added_wrappers"] = TRUE;
}
// We set this global so later on we can easily tell if we've already performed this opperation.
$GLOBALS["arrange_fields_added_wrappers_" . $form["form_id"]["#value"]] = TRUE;
}