You are here

function editor_note_add_note in Editor Notes 7

Ajax callback for 'Add note' button in 'editor_note_field_widget_form'.

1 string reference to 'editor_note_add_note'
editor_note_field_widget_form in ./editor_note.module
Implements hook_field_widget_form().

File

./editor_note.module, line 1430
Main functionality for Editor Notes module.

Code

function editor_note_add_note(&$form, &$form_state) {
  $ajax_commands = array();
  $entity_type = $form['#entity_type'];
  $entity = $form['#entity'];
  $field_name = str_replace('_add_note', '', $form_state['triggering_element']['#name']);
  $note = $form_state['values'][$field_name][LANGUAGE_NONE][0]['note'];
  $entity_id = $form_state['values'][$field_name][LANGUAGE_NONE][0]['note_entity_id'];
  $edit_path = implode('/', array(
    $entity_type,
    $entity_id,
    'edit',
  ));
  $errors = form_get_errors();
  if (!empty($errors) && isset($errors[$field_name])) {

    // Displays validation message.
    $message = theme('editor_note_message', array(
      'field_name' => $field_name,
      'message_text' => $errors[$field_name],
      'message_type' => 'error',
    ));
  }
  else {

    // Adds note entity to the database.
    editor_note_save($note, $entity_type, $entity, $field_name);

    // Displays status message.
    $message = theme('editor_note_message', array(
      'field_name' => $field_name,
      'message_text' => t('Note has been added and saved.'),
      'message_type' => 'status',
    ));

    // Updates notes table and pager.
    $field = field_info_field($field_name);
    $notes = editor_note_get_notes($entity_type, $entity, $field);
    $formatted_notes = editor_note_get_formatted_notes($field, $notes, TRUE, $edit_path);
    $ajax_commands[] = ajax_command_replace('#formatted_notes_' . $field_name, drupal_render($formatted_notes));

    // Returns blank textarea for adding notes.
    $form[$field_name][LANGUAGE_NONE][0]['note']['#default_value'] = '';
  }
  $ajax_commands[] = ajax_command_replace('#status_message_' . $field_name, $message);
  $ajax_commands[] = ajax_command_replace('#textarea_' . $field_name, drupal_render($form[$field_name][LANGUAGE_NONE][0]['note']));
  return array(
    '#type' => 'ajax',
    '#commands' => $ajax_commands,
  );
}