You are here

function editor_note_field_widget_form in Editor Notes 7

Implements hook_field_widget_form().

Creates widget for the field, called for each field item.

File

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

Code

function editor_note_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($field['type'] == 'editor_note') {
    $text_processing = !empty($field['settings']['text_processing']) ? $field['settings']['text_processing'] : EDITOR_NOTE_DEFAULT_TEXT_FORMAT;

    // Rich text.
    if ($text_processing == 'filtered_text') {
      $element['#attached']['css'][] = drupal_get_path('module', 'editor_note') . '/css/editor-note-text-format.css';
      $type = 'text_format';
      $format = !empty($instance['default_value'][0]['note']['format']) ? $instance['default_value'][0]['note']['format'] : NULL;
      $default_value = !empty($instance['default_value'][0]['note']['value']) ? $instance['default_value'][0]['note']['value'] : '';
      $maxlength = '';
    }
    else {
      $type = 'textarea';
      $format = NULL;
      $default_value = !empty($instance['default_value'][0]['note']) ? $instance['default_value'][0]['note'] : '';
      $maxlength = $field['settings']['notes_maxlength'] > 0 ? $field['settings']['notes_maxlength'] : '';
    }
    $element['note'] = array(
      '#type' => $type,
      '#prefix' => '<div id="textarea_' . $field['field_name'] . '" class="editor_note_field_wrapper">',
      '#suffix' => '</div>',
      '#title' => check_plain($instance['label']) . ($instance['required'] ? ' ' . theme('form_required_marker') : ''),
      '#default_value' => $default_value,
      '#format' => $format,
      '#description' => $instance['description'],
      '#rows' => $field['settings']['notes_size'],
      '#resizable' => FALSE,
      '#weight' => $field['settings']['display'] ? 1 : 4,
      '#attributes' => array(
        'maxlength' => $maxlength,
        'placeholder' => $field['settings']['notes_placeholder'],
      ),
    );
    if (isset($instance['entity_type']) && isset($form['#entity'])) {
      list($entity_id) = entity_extract_ids($instance['entity_type'], $form['#entity']);
    }
    if (isset($entity_id)) {

      // Load CTools components for modal popups.
      editor_note_load_ctools_modal($field);
      $notes = editor_note_get_notes($instance['entity_type'], $form['#entity'], $field);
      $formatted_notes = editor_note_get_formatted_notes($field, $notes, TRUE);

      // Display 'Add Note' button only if entity has been already created
      // and stored in the database.
      $element['note_add'] = array(
        '#type' => 'submit',
        '#name' => $field['field_name'] . '_add_note',
        '#value' => t('Add note'),
        '#submit' => array(
          'editor_note_add_note_submit',
        ),
        '#validate' => array(
          'editor_note_add_note_validate',
        ),
        '#limit_validation_errors' => array(
          array(
            $field['field_name'],
          ),
        ),
        '#weight' => $field['settings']['display'] ? 2 : 5,
        '#ajax' => array(
          'callback' => 'editor_note_add_note',
          'event' => 'click',
        ),
      );
      $element['message'] = array(
        '#type' => 'container',
        '#attributes' => array(
          'id' => array(
            'status_message_' . $element['#field_name'],
          ),
        ),
        '#value' => '',
        '#weight' => 3,
      );
      $element['formatted_notes'] = array(
        '#markup' => drupal_render($formatted_notes),
        '#weight' => $field['settings']['display'] ? 4 : 1,
      );
      $element['note_entity_id'] = array(
        '#type' => 'value',
        '#value' => $entity_id,
      );
      if (isset($form_state['triggering_element']['#name']) && $form_state['triggering_element']['#name'] == $field['field_name'] . '_add_note') {

        // Fixing Ajax #default_value issue, https://drupal.org/node/1082818.
        $storage = 'input';
        unset($form_state[$storage][$field['field_name']][LANGUAGE_NONE][0]['note']);
      }
    }
  }
  return $element;
}