When creating a new entity of a entity type that supports bundles and the entity creation form for that entity type lets the user select the bundle for that entity entity_form_submit_build_entity() fails.

This happens, because it invokes entity_extract_ids to retrieve the bundle property when it hasn't been set yet. This can easily be solved by populating the bundle property of that entity before continuing.

function entity_form_submit_build_entity($entity_type, $entity, $form, &$form_state) {
  $info = entity_get_info($entity_type);
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  // Copy top-level form values that are not for fields to entity properties,
  // without changing existing entity properties that are not being edited by
  // this form. Copying field values must be done using field_attach_submit().
  $values_excluding_fields = $info['fieldable'] ? array_diff_key($form_state['values'], field_info_instances($entity_type, $bundle)) : $form_state['values'];
  foreach ($values_excluding_fields as $key => $value) {
    $entity->$key = $value;
  }
  // Invoke all specified builders for copying form values to entity properties.
  if (isset($form['#entity_builders'])) {
    foreach ($form['#entity_builders'] as $function) {
      $function($entity_type, $entity, $form, $form_state);
    }
  }

  // Copy field values to the entity.
  if ($info['fieldable']) {
    field_attach_submit($entity_type, $entity, $form, $form_state);
  }
}

Comments

fubhy’s picture

Status: Active » Needs review
StatusFileSize
new18.66 KB

Status: Needs review » Needs work

The last submitted patch, entity-form-submit-build-entity-bundle-property-1.patch, failed testing.

berdir’s picture

Version: 8.x-dev » 7.x-dev

All different in 8.x, moving back to 7.x ;)

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.