You are here

function arrange_fields_form_alter in Arrange Fields 7

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

Implementation of hook_form_alter().

File

./arrange_fields.module, line 1263

Code

function arrange_fields_form_alter(&$form, &$form_state, $form_id) {
  if (variable_get("arrange_fields_enable_form_id_discovery", 0) && user_access("administer arrange fields")) {
    drupal_set_message(t("Arrange Fields module: Form ID <strong>") . $form_id . t("</strong> detected.\n                        <br>To turn off this message, visit ") . l("admin/config/arrange-fields/settings", "admin/config/arrange-fields/settings") . t(" and uncheck\n                        the Enable form_id discovery mode checkbox."));
    $tmf = arrange_fields_get_included_module_files();
    if ($tmf) {
      drupal_set_message(t("These are possible include files which <b>may or may not</b> need\n                          to be specified along with the form_id:") . "<br>{$tmf}" . t("\n                          If you are unsure what these are used for, please ignore them, and just\n                          use the form_id stated above."));
    }
  }
  $form_type = af_get($GLOBALS["arrange_fields_editing_type"]);
  if (af_get($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 (!af_get($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" && isset($form["submission_info"]) && 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.
    if (!isset($form["#attributes"])) {
      $form["#attributes"] = array();
      $form["#attributes"]["class"] = "";
    }
    if (is_array($form["#attributes"]["class"])) {
      $form["#attributes"]["class"][0] .= " arrange-fields-container arrange-fields-container-{$fid} ";
    }
    else {
      $form["#attributes"]["class"] .= " arrange-fields-container arrange-fields-container-{$fid} ";
    }
    if (!af_get($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(af_get($temp[0]));
      $pos_top = trim(af_get($temp[1]));
      $pos_left = trim(af_get($temp[2]));
      $element_type = trim(af_get($temp[3]));
      $element_id = trim(af_get($temp[4]));
      $width = trim(af_get($temp[5]));
      $height = trim(af_get($temp[6]));
      $wrapper_width = trim(af_get($temp[7]));
      $wrapper_height = trim(af_get($temp[8]));
      $label_display = trim(af_get($temp[9]));
      $label_vertical_align = trim(af_get($temp[10]));
      $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[7] == "~~markup_element~~") {
        $markup_width = af_get($temp[8]);
        $markup_height = af_get($temp[9]);
        $safe_markup_body = trim(af_get($temp[10]));
        $markup_body = arrange_fields_unconvert_unsafe_chars(trim(af_get($temp[10])));
        $safe_wrapper_style = trim(af_get($temp[11]));
        $wrapper_style = arrange_fields_unconvert_unsafe_chars(trim(af_get($temp[11])));
        $z_index = trim(af_get($temp[12]));
        $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        #{$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 != "" && $element_id == "" && !strstr($wrapper_id, "-vertical-tabs-")) {

        // We do not know the element_id in this situation
        $css_markup .= "        \n          #{$fid} #{$wrapper_id} {$element_type}.form-text, \n          #{$fid} #{$wrapper_id} {$element_type}.form-textarea {\n            width: {$width};\n            height: {$height};\n          }\n          ";
      }
      else {
        if ($width != "0px" && $element_type != "" && $element_id != "" && !strstr($wrapper_id, "-vertical-tabs-")) {

          // We know the element id, which makes this simpler.
          $css_markup .= "\n          #{$fid} #{$wrapper_id} {$element_type}#{$element_id} {\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");
    drupal_add_js(array(
      "arrangeFieldsSnapResize" => variable_get("arrange_fields_snap_resize", 0),
    ), "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...

    /*$head_array = array(
        "#tag" => "style",
        "#attributes" => array(
          "type" => "text/css",
        ),
        "#value" => $css_markup,
      );*/

    //drupal_add_html_head($head_array, "arrange_fields_position_data");
    $form["arrange_fields_css_markup_and_elements"] = array(
      "#markup" => "<style>{$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 Field's (or Webform's) normal destination,
  // so we set it to our other popup function.
  if (($form_id == "field_ui_field_edit_form" || $form_id == "webform_component_edit_form") && $GLOBALS["arrange_fields_editing_field"] == TRUE) {

    // Set up our special submit handler to fire off after whatever
    // submit handler is already there.
    if (is_array($form["#submit"])) {
      $form["#submit"][] = "arrange_fields_editing_field_submit";
    }
    else {
      $form["#submit"] = array(
        "arrange_fields_editing_field_submit",
      );
    }
  }
}