You are here

function theme_editablefields_formatter_editable in Editable Fields 6.3

Same name and namespace in other branches
  1. 6 editablefields.module \theme_editablefields_formatter_editable()
  2. 6.2 editablefields.module \theme_editablefields_formatter_editable()

Theme the editable field.

File

./editablefields.module, line 247
Editable fields module.

Code

function theme_editablefields_formatter_editable($element) {
  static $js_ready;
  $field_name = $element['#field_name'];
  $field = content_fields($field_name);
  $revision = !empty($element['#node']->vid) ? $element['#node']->vid : NULL;
  $node = node_load($element['#node']->nid, $revision);
  $delta = $element['#item']['#delta'];

  // to disable overriding textareas just set this variable to 0
  if (variable_get('editablefields_textareas_resizable_threshold', 25)) {
    static $textareas_count = 0;
    if ($field['type'] == 'text' && $field['widget']['type'] == 'text_textarea') {
      if ($textareas_count++ >= variable_get('editablefields_textareas_resizable_threshold', 25)) {
        drupal_add_js(drupal_get_path('module', 'editablefields') . '/editablefields_overrides.js');
      }
    }
  }

  // See if access to this form element is restricted,
  // if so, skip widget processing and just set the value.
  if (!node_access('update', $node) || !content_access('edit', $field)) {

    // can't edit
    $formatter_name = 'default';
    if ($formatter = _content_get_formatter($formatter_name, $field['type'])) {
      $theme = $formatter['module'] . '_formatter_' . $formatter_name;
      return theme($theme, $element);
    }
  }
  else {
    $formatter_name = 'default';
    if ($formatter = _content_get_formatter($formatter_name, $field['type'])) {
      if (!isset($js_ready)) {
        $js_ready = TRUE;
        drupal_add_js('misc/jquery.form.js');
        drupal_add_js(drupal_get_path('module', 'editablefields') . '/editablefields.js');
        drupal_add_css(drupal_get_path('module', 'editablefields') . '/editablefields.css');
        $settings = array(
          'url_html' => url('editablefields_html', array(
            'absolute' => TRUE,
          )),
          'url_submit' => url('editablefields_submit', array(
            'absolute' => TRUE,
          )),
          'url_view' => url('editablefields_view', array(
            'absolute' => TRUE,
          )),
          'clicktoedit_message' => theme('editablefields_clicktoedit_message'),
          'clicktoedit_message_empty' => theme('editablefields_clicktoedit_message_empty'),
        );
        drupal_add_js(array(
          'editablefields' => $settings,
        ), 'setting');
      }

      // [andreiashu] not sure how this works exactly but it does...
      if (content_handle('widget', 'multiple values', $field) != CONTENT_HANDLE_CORE) {
        if ($delta != 0) {
          return;
        }
      }
      $theme = $formatter['module'] . '_formatter_' . $formatter_name;
      $class = "editablefields";
      if ($element['#formatter'] == 'editable') {
        $class .= " ajax-editable";
      }
      elseif ($element['#formatter'] == 'clicktoedit') {
        $class .= " clicktoedit";
      }
      elseif ($element['#formatter'] == 'editable_html') {
        $class .= " editablefields-html-load";
      }
      $pre = '<div class="' . $class . '" nid="' . $node->nid . '" field="' . $field_name . '" delta="' . $delta . '">';
      $post = '</div>';
      if ($element['#formatter'] != 'editable_html') {
        $themed_element = theme($theme, $element);

        // add the edit link for clicktoedit formatters
        if ($element['#formatter'] == 'clicktoedit') {
          if (!empty($themed_element)) {
            $pre .= theme('editablefields_clicktoedit_message');
          }
          else {
            $pre .= theme('editablefields_clicktoedit_message_empty');
          }
        }
        return $pre . $themed_element . $post;
      }
      else {

        // $node seems to be incomplete, so we reload it
        $node = node_load($node->nid, $revision);
        return $pre . drupal_get_form('editablefields_form', $node, $field_name, $delta) . $post;
      }
    }
  }
}