You are here

function editableviews_entity_form in Editable Views 7

Form builder for an editable view.

Parameters

$entities: An array of entities to get the form for, keyed first by entity type and then by entity id. The ids may be fake in the case of new, unsaved entities. Furthermore, new entities should have a property 'editableviews_exposed_fields'. This should be an array of field names corresponding to editable field handlers on the view. A value in at least one of these fields on the entity causes it to be saved by editableviews_entity_form_submit_save(). This allows the user to leave a blank part of the result empty. However, it does not account for the fact that there may be default values in the field widgets! TODO: consider this problem!

$results_coordinates: An array containing coordinates lookups for getting entity type and entity from row id and relationship, and the same the other way round. Contains:

  • 'entities_to_results': A nested array keyed by entity type then entity id. The final values are flat arrays of the relationship id and result index.
  • 'results_to_entities': A nested array keyed by relationship id then result index. The final values are flat arrays of the entity type and entity id.

Conceptually, the pair (relationship id, result index) can be visualized as the coordinates that specify which cells in the table the entity occupies: the index gives the row and the relationship id one or more columns.

$field_handlers: An array of the editable Views field handlers to show the form elements for. This should be grouped by relationship ID, with the base table for the view's base.

$view: The view object.

See also

editableviews_forms()

2 string references to 'editableviews_entity_form'
editableviews_forms in ./editableviews.module
Implements hook_forms().
editableviews_plugin_style_row_edit_table::get_form in ./editableviews_plugin_style_row_edit_table.inc
Helper method. Retrieves the form render array.

File

./editableviews.module, line 74
editableviews.module Contain module code. @todo:

Code

function editableviews_entity_form($form, &$form_state, $entities, $results_coordinates, $field_handlers, $view) {

  // Include this here as it's fairly likely we have handlers that need it;
  // rather than let the handler class include it over and over again.
  ctools_include('fields');

  //dsm(func_get_args(), 'form builder params');

  //dsm($entities);

  //dsm($field_handlers);
  $form['#tree'] = TRUE;
  $form['#entity_ids'] = array();

  //$form['#field_names'] = $fields;
  $form['#field_handlers'] = $field_handlers;

  // Put these in the form in case custom form handlers need to look at them.
  $form['#results_coordinates'] = $results_coordinates;

  // Put the view name and display name on the form, for hook_form_alter().
  $form['#view_name'] = $view->name;
  $form['#view_display_name'] = $view->current_display;

  // Get the message option from the view style plugin.
  $form['#save_messages'] = $view->style_plugin->options['save_messages'];

  // Get the batch option from the view style plugin.
  $form['#batch'] = $view->style_plugin->options['batch'];

  // Get the batch_size option from the view style plugin.
  $form['#batch_size'] = $view->style_plugin->options['batch_size'];
  foreach (array_keys($entities) as $entity_type) {
    foreach ($entities[$entity_type] as $entity_id => $entity) {
      if (isset($entity->language)) {
        $langcode = field_valid_language($entity->language);
      }
      else {
        $langcode = field_valid_language(NULL);
      }

      // Note we have to explicitly use the array key for the entity id rather
      // than extract it here, because it might not actually be a real id for
      // the case of an entity being created on a relationship.
      list(, , $bundle) = entity_extract_ids($entity_type, $entity);

      // Stash the entity type and id.
      $form['#entity_ids'][$entity_type][] = $entity_id;

      // Get the relationship that this entity is on. We only want to get form
      // elements from the field handlers that are on this relationship.
      list($relationship, $index) = $results_coordinates['entities_to_results'][$entity_type][$entity_id];

      // Build up the per-entity subform.
      $form[$entity_type][$entity_id] = array(
        // This allows FieldAPI to have multiple field form elements attached.
        '#parents' => array(
          $entity_type,
          $entity_id,
        ),
        '#entity_type' => $entity_type,
        '#entity' => $entity,
        '#bundle' => $bundle,
        // Stash the relationship this entity is on, so this form's validate and
        // submit handlers can get the relevant field handler for it.
        '#views_relationship' => $relationship,
      );
      foreach ($field_handlers[$relationship] as $field_handler) {

        // Pass the form to each field handler for it to add its element.
        $field_handler
          ->edit_form($entity_type, $entity, $form[$entity_type][$entity_id], $form_state);
      }
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['#submit'] = array(
    // We split the submit process up into the building of form values and the
    // actual saving, to allow implementations of this to add extra processing
    // steps in between, using hook_form_alter().
    // For easier inserting of submit handlers, we key this array, which will
    // not bother FormAPI at all (see form_execute_handlers()).
    'build_values' => 'editableviews_entity_form_submit_build_values',
    'save' => 'editableviews_entity_form_submit_save',
  );

  //dsm($form, 'end of form builder');
  return $form;
}