function content_edit_content_form

6.x content.module content_edit_content_form($type = "", $cid = "")
4.x content.module content_edit_content_form($type = "", $cid = "")
5.x content.module content_edit_content_form($type = "", $cid = "")

This form lets the user edit some piece of content

4 string references to 'content_edit_content_form'
alerts_form_alter in modules/alerts/alerts.module
calendar_form_alter in modules/calendar/calendar.module
Implements hook_form_alter
content_form_alter in modules/content/content.module
Hook form alter, to set some defaults.
engagements_form_alter in modules/engagements/engagements.module
Implements hook_form_alter

File

modules/content/content.module, line 986

Code

function content_edit_content_form($type = "", $cid = "") {
  global $user;

  $form = array();
  $m = 0;

  if (@$_GET ['content_tabs'] === 'false') { // the string "false", not FALSE
    fp_add_body_class('content-tabs-false');
  }

  fp_add_css(fp_get_module_path("content") . "/css/content.css");
  fp_add_js(fp_get_module_path("content") . "/js/content.js");


  if ($type == "") {
    @$type = strip_tags($_REQUEST ["type"]); // strip_tags to make it safer.    
  }

  if ($cid == "") {
    @$cid = strip_tags($_REQUEST ["cid"]);
  }

  $types = content_get_types();

  if ($cid != "new") {
    $content = content_load($cid);
    if (!$content) {
      display_not_found();
      return;
    }
    fp_set_title(t("Editing") . " " . $content->title . "");
    // Re-set $type, just in case the user put the wrong type in the
    // URL
    $type = $content->type;


  }
  else {
    // New piece of content
    $content = new stdClass();
    $content->published = 1;
    $content->title = "";
    $content->delete_flag = 0;
    fp_set_title(t("Add new") . " " . $types [$type]["title"]);

  }


  fp_add_body_class('content-edit-type--' . $type);


  // For simplicity, and because it causes no harm except a small amount of extra bandwidth, we will
  // state that all "content" forms are multipart (so they can handle file uploads) even if there
  // are no files being uploaded.
  $form ["#attributes"] = array("enctype" => 'multipart/form-data');


  $form ["type"] = array(
    "type" => "hidden",
    "value" => $type,
  );

  $form ["cid"] = array(
    "type" => "hidden",
    "value" => $cid,
  );

  $form ["title"] = array(
    "type" => "textfield",
    "label" => t("Title:"),
    "value" => $content->title,
    "required" => TRUE,
  );

  // Are there any special settings for the title?
  if (isset($types [$type]['settings']) && isset($types [$type]['settings']['title'])) {
    if (is_array($types [$type]['settings']['title'])) {
      // Use array_merge to just override with whatever we have in our settings for the title.
      $form ['title'] = array_merge($form ['title'], $types [$type]['settings']['title']);

      if (isset($form ['title']['auto']) && $form ['title']['auto'] == TRUE) {
        // We will set the form to an automatic title, and hide.
        $form ['title']['value'] = $type . ' content saved on ' . format_date(convert_time(time()));
        $form ['title']['type'] = 'hidden';

      }

    }

  }


  if (isset($types [$type]['settings']['#redirect'])) {
    $form ['#redirect'] = $types [$type]['settings']['#redirect'];
  }


  if (isset($types [$type]['settings']['#validate_handlers'])) {
    $form ['#validate_handlers'] = $types [$type]['settings']['#validate_handlers'];
  }


  if (isset($types [$type]['settings']['#submit_handlers'])) {
    $form ['#submit_handlers'] = $types [$type]['settings']['#submit_handlers'];
  }


  // If a #redirect has not been set, then we'll go back to this content's "view"...
  if (!isset($form ['#redirect'])) {
    $form ['#redirect'] = array('path' => 'content/' . $cid);
  }


  // Any js or css to add for this form, defined in the settings?
  if (isset($types [$type]['settings']['js'])) {
    fp_add_js($types [$type]['settings']['js']);
  }
  if (isset($types [$type]['settings']['css'])) {
    fp_add_css($types [$type]['settings']['css']);
  }



  if ($content->delete_flag == 1) {
    $form ['mark_content_deleted'] = array(
      'type' => 'markup',
      'value' => "<div class='content-deleted'>" . t("NOTE: This content has been marked as deleted, and is scheduled for permanent removal.") . "</div>",
      'weight' => -999,
    );

  }


  // Are there extra fields?  If so, add their values in as well.
  if (is_array($types [$type]['fields'])) {
    foreach ($types [$type]["fields"] as $field_name => $field_details) {

      $form [$field_name] = $field_details;
      if ($field_details ['type'] != 'cfieldset') {

        $value = NULL;
        if (isset($content->{"field__$field_name"}['value'])) {
          $value = $content->{"field__$field_name"}['value'];
        }
        if ($cid == 'new') {
          // value won't be anything.  Use value attr from the form as the initial value
          $value = @$form [$field_name]['value'];
        }

        $form [$field_name]['value'] = $value;

        // If this is a datetime-local field, then the value needs to be adjusted for it to work correctly. (it was stored as UTC in database)             
        if ($form [$field_name]['type'] == 'datetime-local') {
          if (trim($value) != '') {
            $form [$field_name]["value"] = date('Y-m-d\TH:i', convert_time(strtotime($value)));
          }
        }

        // Similar to datetime-local, if this is a "time" field, then it has been stored as UTC in the database, and now
        // needs to be converted to local timezone.
        if ($form [$field_name]['type'] == 'time') {
          if (trim($value) != '') {
            $form [$field_name]["value"] = date('H:i', convert_time(strtotime($value)));
          }
        }


        // If this is a file, we want to have a way to REMOVE the file, so we can upload a different one, or simply to
        // have no file there at all.
        if ($form [$field_name]['type'] == 'file') {
          $limit = intval(@$form [$field_name]['limit']);

          $allowed = trim(@$form [$field_name]['allowed']);
          if (!$allowed) {
            $allowed = "txt, pdf, doc, docx, xls, xlsx, ppt, pptx, rtf, odt, jpg, jpeg, png, gif, zip, 7z"; // default safe files
          }

          $form ['file_allowed_ext_' . $field_name] = array(
            'type' => 'hidden',
            'value' => $allowed,
          );

          $file_count = 0;
          if ($limit == 0) {
            $limit = 1;
          }
          $form ['file_limit_' . $field_name] = array(
            'type' => 'hidden',
            'value' => $limit - 1,
          );
          // Do we already have a list of files we've attached?
          $html = "";
          $ofids = $value;
          $fids = explode(",", $value);
          $bool_is_empty = TRUE;
          foreach ($fids as $fid) {
            if ($fid == '' || intval($fid) == 0) {
              continue;
            }
            $fid = intval($fid);
            $file = content_get_uploaded_file($fid);
            if ($file) {
              $bool_is_empty = FALSE;
              $file_count++;
              // create a markup field, including a delete option, with the filename listed.
              $url = $file ['url'];
              $delete_link = "<a href='javascript:contentDeleteFile(\"$field_name\",\"$fid\");'>Delete</a>";
              $html .= "<div class='file-field-line-$fid'><a href='$url' target='_blank'>" . $file ['original_filename'] . "</a>  - $delete_link</div>";
            } // we already had a file.
          } // foreach          
          if (!$bool_is_empty) {
            $html = "<label>" . t("(Existing)") . " " . $form [$field_name]['label'] . "</label>" . $html;
          }

          $form ["markup_$field_name"] = array(
            'type' => 'markup',
            'value' => $html,
            'weight' => @intval($form [$field_name]['weight']) - 1,
          );

          // Add a hidden field to note that we have already uploaded this file.
          $form ["previous_upload_$field_name"] = array(
            'type' => 'hidden',
            'value' => $ofids,
            'weight' => @$form [$field_name]['weight'],
          );


          $form ["file_count_" . $field_name] = array(
            'type' => 'hidden',
            'value' => $file_count,
          );

          if ($file_count >= $limit) {
            // hide the original field using CSS.
            $form [$field_name]['attributes'] = array("class" => 'hidden');
          }

          if ($allowed) {
            if (!isset($form [$field_name]['description'])) {
              $form [$field_name]['description'] = '';
            }
            $form [$field_name]['description'] .= "<div class='content-allowed-ext'><strong>" . t("Allowed file extensions:") . "</strong> " . $allowed . "</div>";
          }

          if ($limit > 1 && $file_count <= $limit) {
            $add_link = "<a href='javascript:contentAddMoreFile(\"$field_name\");' class='add-more-link add-more-link-$field_name'><i class='fa fa-plus'></i> " . t("Add More") . "</a>";
            if (!isset($form [$field_name]['suffix'])) {
              $form [$field_name]['suffix'] = '';
            }
            $form [$field_name]['suffix'] .= $add_link;
          }


        } // type == file


      } // is not cfieldset
      else {
        // If this is a fieldset, then we need to assign values to its ELEMENTS.  If not empty, then OPEN the cfieldset!
        foreach ($form [$field_name]['elements'] as $c => $efields) {
          foreach ($efields as $efield_name => $efield_details) {
            $value = $content->{"field__$efield_name"}['value'];

            if ($cid == 'new') {
              // value won't be anything.  Use value attr from the form as the initial value
              $value = @$efield_details ['value'];
            }

            $efield_details ['value'] = $value;

            // If this is a datetime-local field, then the value needs to be adjusted for it to work correctly.
            if ($efield_details ['type'] == 'datetime-local') {
              if (trim($value) != '') {
                $value = date('Y-m-d\TH:i', convert_time(strtotime($value)));
              }
            }

            // Similar to datetime-local, if this is a "time" field, then it has been stored as UTC in the database, and now
            // needs to be converted to local timezone.
            if ($efield_details ['type'] == 'time') {
              if (trim($value) != '') {
                $value = date('H:i', convert_time(strtotime($value)));
              }
            }


            $form [$field_name]['elements'][$c][$efield_name]["value"] = $value;



            if (trim($value)) {
              $form [$field_name]['start_closed'] = FALSE;
            }
          }
        }

      } // else is fieldset
    }
  }



  // We never show the published option, since we will always give the user a way to "remove" and "restore" content.
  $form ['published'] = array(
    'type' => 'hidden',
    'value' => intval($content->published),
  );


  // Draw the controls (buttons)  
  $form ["submit_submit"] = array(
    "type" => "submit",
    "value" => t("Submit"),
    "spinner" => TRUE,
    'weight' => 9920,
    'attributes' => array('class' => 'content-submit-btn'),
  );

  if ($cid != 'new') {

    // Only display the delete button if we have permission
    if (content_user_access('delete', $content->cid)) {
      $form ["submit_delete"] = array(
        "type" => "submit",
        "value" => t("Delete"),
        "confirm" => t('Are you sure you wish to delete this?\nThis action cannot be undone.'),
        'weight' => 9930,
        //'spinner' => TRUE,
        'attributes' => array('class' => 'content-delete-btn'),
      );
    }
  }


  watchdog('content', "edit_content_form cid:$cid, type:$type", array(), WATCHDOG_DEBUG);


  return $form;
}