You are here

function slickgrid_editor_form in Slickgrid 7.2

Same name and namespace in other branches
  1. 7 slickgrid.module \slickgrid_editor_form()

Default form used for editors

_state

Parameters

array $form:

2 string references to 'slickgrid_editor_form'
InlineCell.inc in plugins/editors/InlineCell.inc
Provides an inline cell editor
ModalForm.inc in plugins/editors/ModalForm.inc
Provides an inline cell editor

File

./slickgrid.module, line 440

Code

function slickgrid_editor_form($form, &$form_state) {
  $editor = $form_state['editor'];

  // Use the first entity - this will be used as the default value for all selected entities
  foreach ($editor->entities as $entity) {
    list($id, $vid, $bundle_name) = entity_extract_ids($editor->entity_type, $entity);
    $info = entity_get_info($editor->entity_type);
    $label_key = $info['entity keys']['label'];

    // Is this the label we're editing?
    if ($editor->field_id == $label_key) {
      $form[$editor->field_id] = array(
        '#type' => 'textfield',
        '#title' => ucfirst($label_key),
        '#default_value' => $entity->{$label_key},
        '#size' => 60,
        '#maxlength' => 128,
        '#required' => TRUE,
      );
      $form['#entity_property'] = true;

      // Create an instance of the field
    }
    elseif (!($instance = field_info_instance($editor->entity_type, $editor->field_id, $bundle_name))) {
      $editor
        ->set_error($id, t('Field doesn\'t exist'), 'form');
      return;
    }
    elseif ($instance['required']) {

      // If any field instance is required, set required to true
      $required = true;
    }

    // have we retrieved the form field yet?
    if (!count($form)) {

      // file_field_widget_form() requires parents to be an array so ensure it is
      $form['#parents'] = array();

      // Invoke & return the field form
      $form = _field_invoke_default('form', $editor->entity_type, $entity, $form, $form_state, array(
        'field_id' => $editor->field_id,
        'field_name' => $editor->field_id,
      ));
    }
  }

  // If any of the bundles have the field as required, make the form field required
  if ($required) {
    $langcode = $form[$editor->field_id]['#language'];
    $form[$editor->field_id][$langcode][0]['#required'] = $required;
  }

  // Ensure values passed in from the slickgrid are persistent across the form rebuild
  foreach (array(
    'field_name',
    'field_id',
    'view',
    'display_id',
    'plugin',
    'revision',
    'entity_type',
    'entity_ids',
  ) as $element_name) {
    if (is_array($form_state['values'][$element_name])) {

      // entity ids will be passed as an array
      foreach ($form_state['values'][$element_name] as $element_value) {
        $form[$element_name][] = array(
          '#type' => 'hidden',
          '#value' => $element_value,
          '#parents' => array(
            $element_name,
            '',
          ),
        );
      }
    }
    else {
      $form[$element_name] = array(
        '#type' => 'hidden',
        '#value' => $form_state['values'][$element_name],
      );
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 100,
  );
  return $form;
}