You are here

function editablefields_form in Editable Fields 7

Form builder callback.

1 call to editablefields_form()
editablefields_views_form in ./editablefields.module
Form builder callback for Views-embedded forms.
1 string reference to 'editablefields_form'
editablefields_forms in ./editablefields.module
Implements hook_forms().

File

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

Code

function editablefields_form($form, &$form_state, $entity_type, $entity, $field, $instance, $langcode, $display) {

  // TODO: set a #action to a "safe" URL so that forms embedded in Views do not
  // break when the result set displayed in the Views changes.
  // Set #parents to 'top-level' by default.
  $form += array(
    '#parents' => array(),
  );
  $form['#attached']['library'][] = array(
    'editablefields',
    'editablefields',
  );
  $form['#attributes'] = array(
    'class' => array(
      'editable-field',
    ),
  );

  // Set the base properties of the form.
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  $form['#entity_type'] = $entity_type;
  $form['#bundle'] = $bundle;
  $form['#langcode'] = $langcode;
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  $form['#entity_id'] = $id;
  if (!isset($form_state['editablefields_entities'][$entity_type][$id])) {
    $form_state['editablefields_entities'][$entity_type][$id] = $entity;
  }
  else {

    // Use updated entity if already updated once.
    $entity = $form_state['editablefields_entities'][$entity_type][$id];
  }

  // Set the IDs of the entity.
  $info = entity_get_info($entity_type);
  foreach (array(
    'id',
    'revision',
    'bundle',
  ) as $key) {

    // Objects being created might not have id/vid yet.
    $id = isset($entity->{$info['entity keys']['id']}) ? $entity->{$info['entity keys']['id']} : NULL;
    if (isset($info['entity keys'][$key]) && isset($entity->{$info['entity keys'][$key]})) {
      $form[$info['entity keys'][$key]] = array(
        '#type' => 'value',
        '#value' => $entity->{$info['entity keys'][$key]},
      );
    }
  }

  // Wrap the whole form into a wrapper, and prepare AJAX settings.
  $wrapper_id = drupal_html_id('editablefields-' . $field['field_name']);
  $form['#prefix'] = '<div id="' . $wrapper_id . '" class="editablefield-item">';
  $form['#suffix'] = '</div>';
  $ajax = array(
    'callback' => 'editablefields_form_update',
    'wrapper' => $wrapper_id,
    'effect' => 'fade',
    'event' => 'click',
    'progress' => array(
      'type' => 'throbber',
      'message' => t('Please wait'),
    ),
  );

  // Remove the title of the field.
  $instance['label'] = '';
  $edit_mode_state = _editablefields_get_edit_mode($form_state, $form['#parents']);
  $edit_mode = empty($display['settings']['click_to_edit']) || $edit_mode_state;
  if ($edit_mode) {

    // Insert the field form.
    $items = $entity->{$field['field_name']};
    $items = isset($items[$langcode]) ? $items[$langcode] : array();
    $form += field_default_form($entity_type, $entity, $field, $instance, $langcode, $items, $form, $form_state);
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#name' => 'submit-' . implode('-', $form['#parents']),
      '#type' => 'submit',
      '#value' => t('Save'),
      '#ajax' => $ajax,
      '#submit' => array(
        'editablefields_form_submit',
      ),
      '#limit_validation_errors' => array(
        $form['#parents'],
      ),
    );
  }
  else {
    $edit_style = isset($display['settings']['click_to_edit_style']) ? $display['settings']['click_to_edit_style'] : 'button';
    $items = $entity->{$field['field_name']};
    $items = isset($items[$langcode]) ? $items[$langcode] : array();
    $shield = new EditableFieldsShield();
    $shield->arguments = array(
      $entity_type,
      $entity,
      $field,
      $instance,
      $langcode,
      $items,
      $display,
    );
    $form['field'] = array(
      '#pre_render' => array(
        'editablefields_lazy_render_fields',
      ),
      '#arguments' => $shield,
    );

    // Click to edit mode: generate a AJAX-bound submit handler.
    $form['actions']['edit'] = array(
      '#name' => 'edit-' . implode('-', $form['#parents']),
      '#type' => 'submit',
      '#value' => t('Edit this field'),
      '#submit' => array(
        'editablefields_form_submit_edit_mode',
      ),
      '#ajax' => $ajax,
      '#limit_validation_errors' => array(
        $form['#parents'],
      ),
      '#attributes' => array(
        'class' => array(
          'editablefield-edit',
          'editablefield-edit-' . $edit_style,
        ),
      ),
    );
  }

  // Specify the form build id to allow drupal to find the form cache.
  $form['#build_id'] = 'editablefields_form__' . $entity_type . '__' . $id . '__' . $vid . '__' . $field['field_name'];
  return $form;
}