You are here

function _jeditable_save_date_field in jEditable inline content editing 6

Same name and namespace in other branches
  1. 6.2 jeditable.module \_jeditable_save_date_field()

Helper function to save date field

1 call to _jeditable_save_date_field()
_jeditable_ajax_save in ./jeditable.module
Helper function to save a value using the jeditable callback

File

./jeditable.module, line 332
jeditable.module

Code

function _jeditable_save_date_field($node, $field, $value) {
  module_load_include('inc', 'node', 'node.pages');
  $field_name = $field['field_name'];
  $form_state = array();
  $form_state['values']['op'] = t('Save');
  switch ($field['widget']['type']) {
    case 'date_select':
      $date = date_parse($value);
      $form_state['values'][$field_name][0]['value']['year'] = $date['year'];
      $form_state['values'][$field_name][0]['value']['month'] = $date['month'];
      $form_state['values'][$field_name][0]['value']['day'] = $date['day'];
      $form_state['values'][$field_name][0]['value']['hour'] = $date['hour'];
      $form_state['values'][$field_name][0]['value']['minute'] = $date['minute'];
      break;
    case 'date_popup':
    case 'date_text':
      $form_state['values'][$field_name][0]['value']['date'] = $value;
      break;
  }
  $form_id = $node->type . '_node_form';
  drupal_execute($form_id, &$form_state, (object) $node);
  drupal_get_messages('status');

  // Discard status messages
  // @TODO: Do some Ajax with the warning and error messages.

  //$messages = drupal_get_messages();

  // Reload the node to get the value that was saved, if any. Avoid the cache.
  $node = node_load(array(
    "nid" => $node->nid,
  ));
  if (isset($node->{$field_name})) {
    $value = $node->{$field_name}[0]['value'];
    $value = content_format($field_name, array(
      'value' => $value,
    ));
  }
  else {
    $value = NULL;
  }

  // Any HTML tags will already be in place outside the editor
  $value = strip_tags($value);
  return $value;
}