You are here

function editablefields_field_formatter_view in Editable Fields 7

Implements hook_field_formatter_view().

File

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

Code

function editablefields_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, &$items, $display) {
  if (!$display['type'] == 'editable') {
    return;
  }
  $field_name = $field['field_name'];
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

  // See if access to this form element is restricted,
  // if so, skip widget processing and just set the value.
  if (!entity_access('update', $entity_type, $entity) || !field_access('edit', $field, $entity_type, $entity) || !user_access('use editablefields')) {

    // Can't edit.
    return editablefields_fallback_formatter($entity_type, $entity, $field, $instance, $langcode, $items, $display);
  }
  if (isset($display['views_view'])) {

    // Views integration mode: we need to pass the unrendered form to the handler.
    // Remove the View data from the display, we don't need to save it.
    $view = $display['views_view'];
    $views_field = $display['views_field'];
    $row_id = $display['views_row_id'];
    unset($display['views_view']);
    unset($display['views_field']);
    unset($display['views_row_id']);

    // Set our form callback.
    $views_field->views_form_callback = 'editablefields_views_form';

    // Load the original (unrendered) entity from the cache of the Field API views handler.
    $entity = $view->result[$row_id]->_field_data[$views_field->field_alias]['entity'];

    // Save our data.
    $views_field->editablefield_data[$row_id] = compact('entity_type', 'entity', 'langcode', 'display');
    $views_field->editablefield_field = $field;
    $views_field->editablefield_instance = $instance;

    // Add a class to the field wrapper (a cell for example).
    if (empty($views_field->seen_by_editable_fields)) {
      $views_field->options['element_class'] .= ' views-field-editablefield';
      $views_field->seen_by_editable_fields = TRUE;
    }

    // Search for the field object in the $view->field array, so as to extract
    // its actual ID. Views doesn't provide any reliable way to do that otherwise.
    $field_id = array_search($views_field, $view->field, TRUE);
    $items = array(
      0 => array(),
    );
    return array(
      0 => array(
        '#markup' => '<!--form-item-' . $field_id . '--' . $row_id . '-->',
      ),
    );
  }
  else {

    // Standard Field API integration: generate the form.
    // Inject the HTML form.
    $display['label'] = 'hidden';
    $items = array(
      0 => array(),
    );
    return array(
      0 => drupal_get_form('editablefields_form__' . $entity_type . '__' . $id . '__' . $vid . '__' . $field['field_name'], $entity_type, $entity, $field, $instance, $langcode, $display),
    );
  }
}