You are here

function slickgrid_editor_form in Slickgrid 7

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

Default form used for editors

_state

Parameters

array $form:

2 string references to 'slickgrid_editor_form'
slickgrid_plugin_inline_cell_process in plugins/editors/InlineCell.inc
Process the inline cell update callback
slickgrid_plugin_modal_form_process in plugins/editors/ModalForm.inc
Retrieve form / process a modal form This uses ctools ajax & the whole form so it works with all fields

File

./slickgrid.module, line 514

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);

    // Create an instance of the field
    if (!($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'),
  );
  return $form;
}