You are here

function arrange_fields_form_alter in Arrange Fields 6

Same name and namespace in other branches
  1. 7 arrange_fields.module \arrange_fields_form_alter()

Implementation of hook_form_alter().

File

./arrange_fields.module, line 651

Code

function arrange_fields_form_alter(&$form, &$form_state, $form_id) {
  $form_type = $GLOBALS["arrange_fields_editing_type"];
  if ($GLOBALS["arrange_fields_editing"] == $form_id) {

    // meaning, we are arranging the fields on the current form_id...
    // Let's unset the various elements which will just get in the
    // way of the fields.
    unset($form["#submit"]);
    $form["#attributes"]["class"] = "arrange-fields-container";
    if (!$GLOBALS["arrange_fields_added_wrappers_" . $form["form_id"]["#value"]]) {
      arrange_fields_add_draggable_wrappers($form, $form_type, TRUE);
    }
  }

  // Do we have position data for the content type (form) currently being displayed?
  // If we do, then let's add styles to the page which contain the position data.
  if ($position_data = variable_get("arrange_fields_position_data_{$form_id}", FALSE)) {
    if (strpos($form_id, "webform_client_form") === 0) {
      $form_type = "webform";
    }

    // If this is a webform, and we are looking at submission data, then we do not want to try to
    // arrange any fields.  Doing so (at least at the moment) does not look right.
    if ($form_type == "webform" && is_array($form["submission_info"]) && $form["submission_info"]["#type"] == "fieldset") {
      return;
    }
    drupal_add_css(drupal_get_path("module", "arrange_fields") . "/css/arrange_fields.css");
    drupal_add_js(drupal_get_path("module", "arrange_fields") . "/js/arrange_fields_node_edit.js");
    $fid = $form["#id"];

    // The form's CSS id.  We will use it later to target just this one form.
    $form["#attributes"]["class"] .= " arrange-fields-container ";
    $form["#attributes"]["class"] .= " arrange-fields-container-{$fid} ";
    if (!$GLOBALS["arrange_fields_added_wrappers_" . $form["form_id"]["#value"]]) {
      arrange_fields_add_draggable_wrappers($form, $form_type);
    }

    // Let's go through and assign the positions.
    $jsConfigArray = array();
    $jsMarkupArray = array();
    $css_markup = $markup_elements = "";
    $lines = explode(";", $position_data);
    foreach ($lines as $line) {
      if (trim($line) == "") {
        continue;
      }

      // skip blanks
      $temp = explode(",", $line);
      $wrapper_id = trim($temp[0]);
      $pos_top = trim($temp[1]);
      $pos_left = trim($temp[2]);
      $element_type = trim($temp[3]);
      $width = trim($temp[4]);
      $height = trim($temp[5]);
      $wrapper_width = trim($temp[6]);
      $wrapper_height = trim($temp[7]);
      $label_display = trim($temp[8]);
      $label_vertical_align = trim($temp[9]);
      $text_label_display = $label_display;
      if ($label_display == "inline-block") {
        $text_label_display = "inline";
      }
      if ($wrapper_id == "~~maxBottom~~") {

        // This is actually the height of the container.  Let's set that,
        // then continue.
        $css_markup .= " .arrange-fields-container-{$fid} {\n                      height: {$pos_top};\n                        }";
        continue;
      }

      // Was this actually a markup element which the user added?
      // If so, add
      if ($temp[6] == "~~markup_element~~") {
        $markup_width = $temp[7];
        $markup_height = $temp[8];
        $safe_markup_body = trim($temp[9]);
        $markup_body = arrange_fields_unconvert_unsafe_chars(trim($temp[9]));
        $safe_wrapper_style = trim($temp[10]);
        $wrapper_style = arrange_fields_unconvert_unsafe_chars(trim($temp[10]));
        $z_index = trim($temp[11]);
        $markup_elements .= "\n        <div class='draggable-form-item arrange-fields-draggable-markup' \n              id='{$wrapper_id}'>\n          <div class='arrange-fields-control-handle arrange-fields-control-handle-markup'><span class='arrange-fields-handle-region'> &nbsp; &nbsp; </span>\n            <a href='javascript: arrangeFieldsDialogEditMarkup(\"{$wrapper_id}\");' class='arrange-fields-config-markup-link' title='Configure this markup'>&nbsp;</a>\n          </div>\n          <div class='arrange-fields-markup-body form-item' \n              id='{$wrapper_id}_body'>{$markup_body}</div>\n        </div>";
        $css_markup .= "\n          #{$fid} #{$wrapper_id} {\n            top: {$pos_top};\n            left: {$pos_left};\n            width: {$markup_width};\n            height: {$markup_height};\n            z-index: {$z_index};\n            {$wrapper_style}\n          }\n        ";

        // Add this bit of markup to our jsMarkupArray, so it can be
        // added as a setting later.
        $jsMarkupArray[$wrapper_id]["markupBody"] = $safe_markup_body;
        $jsMarkupArray[$wrapper_id]["wrapperStyle"] = $safe_wrapper_style;
        $jsMarkupArray[$wrapper_id]["zIndex"] = $z_index;
        continue;
      }
      $jsConfigArray[$wrapper_id]["wrapperWidth"] = $wrapper_width;
      $jsConfigArray[$wrapper_id]["wrapperHeight"] = $wrapper_height;
      $jsConfigArray[$wrapper_id]["labelDisplay"] = $label_display;
      $jsConfigArray[$wrapper_id]["labelVerticalAlign"] = $label_vertical_align;

      // Add to our CSS markup value...
      $css_markup .= "\n            \n        #{$fid} #{$wrapper_id}{        \n          top: {$pos_top};\n          left: {$pos_left}; \n        }\n      ";

      // We do not want to try to resize any input fields if this
      // is the buttons wrapper.  Otherwise it will mess up our buttons.
      // This usually happens when you have CAPTCHA installed.
      if ($width != "0px" && $element_type != "") {
        $css_markup .= "        \n          #{$fid} #{$wrapper_id} {$element_type}.form-text, \n          #{$fid} #{$wrapper_id} {$element_type}.form-textarea {\n            width: {$width};\n            ";

        // If this is an input field (not a textarea) we do not need to specify the height.
        // This is hopefully a fix to a bug where, for some users, their textfields get shrunk down
        // to only 1px.
        if ($element_type != "input") {
          $css_markup .= "\n              height: {$height};        \n            ";
        }
        $css_markup .= "    }        ";
      }

      // Handle any configurations which were set in the configure dialog.
      if ($wrapper_width != "") {
        $css_markup .= " #{$fid} #{$wrapper_id} { width: {$wrapper_width}; } ";
      }
      if ($wrapper_height != "") {
        $css_markup .= "\n          #{$fid} #{$wrapper_id} { height: {$wrapper_height}; } \n          #{$fid} #{$wrapper_id} fieldset { height: 100%; } \n        ";
      }
      if ($label_display != "") {
        $css_markup .= "\n          #{$fid} #{$wrapper_id} .form-item label { \n            vertical-align: {$label_vertical_align}; \n          }\n          #{$fid} #{$wrapper_id} .form-item > input, \n          #{$fid} #{$wrapper_id} .form-item > label,\n          #{$fid} #{$wrapper_id} .form-item > div,\n          #{$fid} #{$wrapper_id} .form-item > div.form-radios > div,\n          #{$fid} #{$wrapper_id} .form-item > div.form-checkboxes > div\n          {\n            display: {$label_display};\n          }";
        if ($element_type == "input") {

          // Because of IE, we must do something special if the input
          // is a textfield, which is what this is testing for.
          // Basically, the label cannot have a display of "inline-block",
          // it must simply be "inline."  However, the textfield itself still
          // needs to be "inline-block."  Thanks IE!
          $css_markup .= "\n            #{$fid} #{$wrapper_id} .form-item > label\n            {\n              display: {$text_label_display};\n            }\n          ";
        }
        $css_markup .= "  \n          #{$fid} #{$wrapper_id} .form-item div.ui-resizable-handle,\n          #{$fid} #{$wrapper_id} .form-item div.description\n          {\n            display: block;\n          }                          \n                ";
      }
    }
    drupal_add_js(array(
      "arrangeFieldsDialogConfigObj" => $jsConfigArray,
    ), "setting");
    drupal_add_js(array(
      "arrangeFieldsDialogMarkupObj" => $jsMarkupArray,
    ), "setting");

    // Add in the user-specified grid width.
    drupal_add_js(array(
      "arrangeFieldsGridWidth" => variable_get("arrange_fields_grid_width", 10),
    ), "setting");
    if ($GLOBALS["arrange_fields_editing"] != $form_id) {

      // Meaning, we are not currently arranging this form.  The user
      // must actually be putting data into it on the node/edit page.
      // Let's remove the extra
      // styles around the various divs so that it looks more natural.
      $css_markup .= "\n      \n      #{$fid} {\n        border: 0;\n        background: none;\n      }\n      \n      #{$fid} .draggable-form-item {\n        border: 0;\n        background-color: transparent;\n      }\n      \n      ";
    }

    // Now, add in our css markup and markup elements...
    $form["arrange_fields_css_markup_and_elements"] = array(
      "#value" => "<style type='text/css'>{$css_markup}</style>{$markup_elements}",
      "#after_build" => array(
        "arrange_fields_add_form_css_js",
      ),
    );

    // Let's add our modified form back to the cache (needed to get the preview
    // button to work correctly).
    form_set_cache($form["#build_id"], $form, $form_state);
  }

  // The user is trying to configure a field, and we want to show
  // it to them in the custom popup.  When they submit that form, we don't
  // want it to go to CCK's (or Webform's) normal destination,
  // so we set it to our other popup function.
  if (($form_id == "content_field_edit_form" || $form_id == "webform_component_edit_form") && $GLOBALS["arrange_fields_editing_field"] == TRUE) {
    $form["#redirect"] = "arrange-fields/popup-close-window";
  }
}