You are here

function editableviews_entity_form_submit_build_values in Editable Views 7

Form submit handler the first: build entities from field values.

Entities are flagged as needing to be saved by our other submit handler, editableviews_entity_form_submit_save().

Existing entities are flagged if they have editable fields on them.

New entities are checked to see whether they need to be saved or not. This allows the user to leave a new entity's form elements empty to indicate they do not wish to create an entity in that space. Note however that this means if FieldAPI fields have default values, and these are left in the form, the entity will be created! There is no real way that I can envisage to account for this, short of adding a form element with a checkbox for the user to explicitly say a new entity should be saved. TODO: consider implementing this.

1 string reference to 'editableviews_entity_form_submit_build_values'
editableviews_entity_form in ./editableviews.module
Form builder for an editable view.

File

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

Code

function editableviews_entity_form_submit_build_values($form, &$form_state) {
  ctools_include('fields');

  //dsm($form);

  //dsm($form_state, 'fs');
  $field_handlers = $form['#field_handlers'];

  // Act on each entity in turn.
  foreach ($form['#entity_ids'] as $entity_type => $entity_ids) {
    foreach ($entity_ids as $entity_id) {
      $bundle = $form[$entity_type][$entity_id]['#bundle'];
      $entity = $form[$entity_type][$entity_id]['#entity'];
      $relationship = $form[$entity_type][$entity_id]['#views_relationship'];

      // Invoke the field handlers on the relationship the entity is on, if any.
      foreach ($field_handlers[$relationship] as $field_handler) {
        $field_handler
          ->edit_form_submit($entity_type, $entity, $form[$entity_type][$entity_id], $form_state);
      }

      // Set our flag on the entity if it needs to be saved.
      if (is_numeric($entity_id)) {

        // For existing entities, we save if the entity had editable fields
        // in the view. This does mean that an entity could be saved needlessly,
        // as we don't compare old values with incoming form values.
        if (count($field_handlers[$relationship])) {
          $entity->editableviews_needs_save = TRUE;
        }
      }
      else {

        // For new entities, check they have had values set on them by the user
        // that justify saving them.
        $wrapper = entity_metadata_wrapper($entity_type, $entity);
        foreach ($entity->editableviews_exposed_fields as $expected_field_name) {

          // We have to use the wrapper, as FieldAPI fields will contain a
          // nested array with the langcode even if left empty.
          $value = $wrapper->{$expected_field_name}
            ->raw();
          if (!empty($value)) {

            // If there is a value, mark this entity as needing to be saved by
            // editableviews_entity_form_submit_save(). Obviously, custom
            // form submit handlers that come after us are free to remove or
            // force this.
            $entity->editableviews_needs_save = TRUE;

            // No point going any further.
            break;
          }
        }
      }

      // Put the entities in the form state for the subsequent submit handlers to
      // find. Key by entity type to make it easy to get hold of that.
      $form_state['entities'][$entity_type][$entity_id] = $entity;
    }
  }
}