node_save custom Date field, file field, taxonomy field

WE HAVE MOVED!

Please visit our new Drupal Website at https://www.xenyo.com我們的新網頁設計公司網頁.

/* date field */
  $form['dod']['dateitem'] = array(
    '#type' => 'date_select',
    '#title' => t('Date of death'),
    '#date_format'   => $dateformat,
    '#date_label_position' => 'within',
    '#default_value' => '',
    '#date_timezone' => date_default_timezone(),
    '#date_increment' => 1,
    '#date_year_range' => '-3:+3',
    '#value_callback' => 'date_select_element_value_callback',
    '#tree' => FALSE,
    '#required' => TRUE,
  );
/* managed_file field */
  $form['marker_fid'] = array(
    '#title' => "Upload main image to put on marker",
    '#type' => 'managed_file',
    '#upload_location' => 'public://',
  );
 
/* taxonomy autocomplete field */
  $form['taxonomy_field'] = array(
    '#type' => 'textfield',
    '#title' => t('Select Options'),
    '#required' => FALSE,
    '#autocomplete_path' => 'taxonomy/autocomplete/field_select_option',
  );
 
/* form_submit handler */
 
/* date field format */
 $value =  $form_state['value'];
    $dateitem = array();
    $dateitem['value'] = $value->dateitem . ' 00:00:00';
    $dateitem['timezone'] = date_default_timezone();
    $dateitem['timezone_db'] = date_default_timezone();
    $dateitem['date_type'] = 'datetime';
  }
 
/* taxonomy field format */
 $term = taxonomy_get_term_by_name($value->taxonomy_field);
 
/* node_save (new node) */
  global $user;
  $node = new stdClass();
  $node->type = "xxx";
  $node->status = 1;
  $node->language = LANGUAGE_NONE;
  $node->uid = $user->uid;
  $node->title = $obj->name;
  $node->field_date_of_death[LANGUAGE_NONE][] = $dateitem;
  $node->field_select_option[LANGUAGE_NONE][] = array("tid" => current($term)->tid);
  $image = (array)file_load($value->marker_fid);
  $node->field_main_image[LANGUAGE_NONE][] = $image;
  node_save($node);
Help Share this Article