You are here

function eck__entity__form_submit in Entity Construction Kit (ECK) 7.2

Same name and namespace in other branches
  1. 7.3 eck.entity.inc \eck__entity__form_submit()
  2. 7 eck.entity.inc \eck__entity__form_submit()

Submit function for entity form.

File

./eck.entity.inc, line 452
All the menus, pages, and functionality related to administering entities.

Code

function eck__entity__form_submit($form, &$state) {
  $entity = $state['values']['entity'];
  $entity_type = $entity
    ->entityType();
  $entity_type = EntityType::loadByName($entity_type);
  $properties = $entity_type->properties;

  // Collect all form values for properties to be passed to pre_set behavior.
  $values = array();
  foreach ($properties as $property => $info) {

    // Pass form values "as are" because for example NULL values are valid.
    // The isset() does not return TRUE for array keys that correspond to a NULL
    // value, while array_key_exists() does.
    if (array_key_exists($property, $state['values'])) {
      $values[$property] = array(
        'data' => $state['values'][$property],
      );
    }
  }

  // @TODO: maybe we should do this in the form validation step. That way we can
  // catch entity wrapper validation exceptions and set a form error message
  // appropriately.
  // Lets give the behavior a chance to manipulate the data before it is set.
  $data = eck_property_behavior_invoke_plugin($entity_type, 'pre_set', array(
    'entity' => $entity,
  ), $values);

  // Now that behaviors got a chance to manipulate the values given by the user,
  // lets set them in the entity.
  $wrapper = entity_metadata_wrapper($entity_type->name, $entity);
  foreach ($properties as $property => $info) {

    // Use the value added by "pre_set" behavior callback, if any.
    if (array_key_exists($property, $data)) {
      $value = $data[$property];
    }
    elseif (isset($values[$property])) {
      $value = $values[$property]['data'];
    }
    else {
      continue;
    }
    $wrapper->{$property}
      ->set($value);
  }
  field_attach_submit($entity
    ->entityType(), $entity, $form, $state);

  // Let the behaviors modify the entity.
  // @todo Why do we need to pass form information to the save behavior.
  // This is related to eck_revisions. Is there a danger that the current
  // eck_revision logic will not apply when entities are manipulated from
  // code and not the UI?
  eck_property_behavior_invoke_plugin($entity_type, 'entity_save', array(
    'entity' => $entity,
    'form' => $form,
    'form_state' => $state,
  ));
  $entity
    ->save();
  $msg = 'Entity @entity_id - @entity_label has been saved';
  $args = array(
    '@entity_id' => entity_id($form['#entity_type'], $entity),
    '@entity_label' => entity_label($form['#entity_type'], $entity),
  );
  $context = array(
    'entity' => $entity,
    'form' => $form,
    'form_state' => $state,
  );
  drupal_alter('eck_entity_save_message', $msg, $args, $context);
  if ($msg) {
    drupal_set_message(t($msg, $args));
  }
  $uri = entity_uri($entity
    ->entityType(), $entity);
  $state['redirect'] = $uri['path'];
}