You are here

editablefields.module in Editable Fields 6

Editable fields module.

File

editablefields.module
View source
<?php

/**
 * @file
 * Editable fields module.
 */

/**
 * Implementation of hook_menu().
 */
function editablefields_menu() {
  global $user;
  $items = array();

  // Admin pages:
  $items['editablefields_html'] = array(
    'page callback' => 'editablefields_html',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'title' => 'ajax form',
  );
  $items['editablefields_submit'] = array(
    'page callback' => 'editablefields_submit',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
    'title' => 'ajax submit',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function editablefields_theme() {
  return array(
    'editablefields_formatter_editable' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_editablefields_formatter_editable',
    ),
    'editablefields_formatter_clicktoedit' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_editablefields_formatter_editable',
    ),
  );
}

/**
 * Implementation of hook_field_formatter_info().
 */
function editablefields_field_formatter_info() {
  return array(
    'editable' => array(
      'label' => t('Editable'),
      'field types' => array_keys(_content_field_types()),
    ),
    'clicktoedit' => array(
      'label' => t('Click to Edit'),
      'field types' => array_keys(_content_field_types()),
    ),
  );
}

/**
 * Theme the editable field.
 */
function theme_editablefields_formatter_editable($element) {
  static $js_ready;
  $field_name = $element['#field_name'];
  $field = content_fields($field_name);
  $node = node_load($element['#node']->nid);
  $delta = $element['#item']['#delta'];
  if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
  }

  // 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,
          )),
          'clicktoedit_message' => '<div class="editablefields_clicktoedit_message">' . t('[edit]') . '</div>',
        );
        drupal_add_js(array(
          'editablefields' => $settings,
        ), 'setting');
      }
      $theme = $formatter['module'] . '_formatter_' . $formatter_name;
      $class = "editablefields";
      if ($element['#formatter'] == 'clicktoedit') {
        $class .= " clicktoedit";
      }

      // CORE handling shoudl have a div on each,
      // MODULE handling should have a div surounding all elements (treat it as
      // one field) (So, we'll arrainge for the JS to remove the rest!)
      if (content_handle('widget', 'multiple values', $field) != CONTENT_HANDLE_CORE) {
        if ($delta != 0) {
          $class = "editablefields editablefields_REMOVE";
        }
      }
      $pre = '<div class="' . $class . '" nid="' . $node->nid . '" field="' . $field_name . '" delta="' . $delta . '">';
      $post = '</div>';
      return $pre . theme($theme, $element) . $post;
    }
  }
}

/**
 * Implementation of hook_forms().
 */
function editablefields_forms() {
  $forms = array();
  $forms['editablefields_form'] = array(
    'callback' => 'editablefields_form_builder',
  );
  return $forms;
}

/**
 * Form builder callback.
 */
function editablefields_form_builder(&$form_state, $node, $field_name, $delta) {
  $field = content_fields($field_name);
  $form = array(
    '#node' => $node,
  );

  //  $form_state = array('values' => array($field['field_name'] => $default_value));
  module_load_include('inc', 'content', 'includes/content.node_form');
  $form['#field_info'] = array(
    $field['field_name'] => $field,
  );
  $form = content_field_form($form, $form_state, $field, $delta);
  unset($form[$field_name]['#title']);
  if (is_array($form[$field_name][0]) && !is_array($form[$field_name][1])) {
    unset($form[$field_name][0]['#title']);
  }
  $form['#field_info'] = array(
    $field['field_name'] => $field,
  );
  return $form;
}

/**
 * Menu callback: ajax form.
 */
function editablefields_html() {
  $nid = arg(1);
  $field_name = arg(2);
  $delta = arg(3);
  $node = node_load($nid);
  if (node_access('update', $node)) {

    //  $html = _editablefields_create_form($node, $field_name);
    $html = drupal_get_form('editablefields_form', $node, $field_name, $delta);
    $object = new stdClass();
    $object->content = $html;

    // Register the JavaScript callback for this module.
    $object->__callbacks = array();

    // Allow other modules to extend the data returned.
    drupal_alter('ajax_data', $object, 'editablefields', $html);
    drupal_json($object);
  }
  else {
    drupal_not_found();
  }
  exit;
}

/**
 * Menu callback: ajax submit.
 */
function editablefields_submit() {
  $nid = $_POST['nid'];
  $field_name = $_POST['field'];
  $delta = $_POST['delta'];
  $node = node_load($nid);
  if (node_access('update', $node)) {
    if (!isset($_POST[$field_name])) {
      $_POST[$field_name] = array();
    }
    $form_state = array(
      'values' => $_POST,
    );
    if (is_array($node->{$field_name})) {
      drupal_execute('editablefields_form', $form_state, $node, $field_name, $delta);
      $err = drupal_get_messages();
      if (count($err) > 0) {
        drupal_set_header('HTTP/1.1 404 Not Found');

        // format the error message suitable for a popup window in simple text.
        foreach ($err as $type => $messages) {
          foreach ($messages as $message) {
            print $type . ' : ' . $message . "\n";
          }
        }
        exit;
      }
      $field = content_fields($field_name);
      if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
        if ($node->{$field_name}[$delta] != $form_state['values'][$field_name][0]) {
          $node->{$field_name}[$delta] = $form_state['values'][$field_name][0];
          node_save($node);
        }
      }
      else {
        if ($node->{$field_name} != $form_state['values'][$field_name]) {
          $node->{$field_name} = $form_state['values'][$field_name];
          node_save($node);
        }
      }

      // make sure sensible headers etc are sent...
      drupal_set_header('Content-Type: text; charset=utf-8');
    }
    else {
      drupal_set_header('HTTP/1.1 404 Not Found');
      print t('No field found, of name: %field', array(
        '%field' => $field_name,
      ));
    }
  }
  else {
    drupal_set_header('HTTP/1.1 404 Not Found');
    print t('No write permissions for %field', array(
      '%field' => $field_name,
    ));
  }
  exit;
}

Functions

Namesort descending Description
editablefields_field_formatter_info Implementation of hook_field_formatter_info().
editablefields_forms Implementation of hook_forms().
editablefields_form_builder Form builder callback.
editablefields_html Menu callback: ajax form.
editablefields_menu Implementation of hook_menu().
editablefields_submit Menu callback: ajax submit.
editablefields_theme Implementation of hook_theme().
theme_editablefields_formatter_editable Theme the editable field.