Index: core/modules/entity/entity.module
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
<+><?php\n\n/**\n * @file\n * Entity API for handling entities like nodes or users.\n */\n\n/**\n * Implements hook_help().\n */\nfunction entity_help($path, $arg) {\n  switch ($path) {\n    case 'admin/help#entity':\n      $output = '';\n      $output .= '<h3>' . t('About') . '</h3>';\n      $output .= '<p>' . t('The Entity module provides an API for managing entities like nodes and users, i.e. an API for loading and identifying entities. For more information, see the online handbook entry for <a href=\"!url\">Entity module</a>', array('!url' => 'http://drupal.org/handbook/modules/entity')) . '</p>';\n      return $output;\n  }\n}\n\n/**\n * Implements hook_modules_preenable().\n */\nfunction entity_modules_preenable() {\n  entity_info_cache_clear();\n}\n\n/**\n * Implements hook_modules_disabled().\n */\nfunction entity_modules_disabled() {\n  entity_info_cache_clear();\n}\n\n/**\n * Gets the entity info array of an entity type.\n *\n * @see hook_entity_info()\n * @see hook_entity_info_alter()\n *\n * @param $entity_type\n *   The entity type, e.g. node, for which the info shall be returned, or NULL\n *   to return an array with info about all types.\n */\nfunction entity_get_info($entity_type = NULL) {\n  global $language;\n\n  // Use the advanced drupal_static() pattern, since this is called very often.\n  static $drupal_static_fast;\n  if (!isset($drupal_static_fast)) {\n    $drupal_static_fast['entity_info'] = &drupal_static(__FUNCTION__);\n  }\n  $entity_info = &$drupal_static_fast['entity_info'];\n\n  // hook_entity_info() includes translated strings, so each language is cached\n  // separately.\n  $langcode = $language->langcode;\n\n  if (empty($entity_info)) {\n    if ($cache = cache()->get(\"entity_info:$langcode\")) {\n      $entity_info = $cache->data;\n    }\n    else {\n      $entity_info = module_invoke_all('entity_info');\n      // Merge in default values.\n      foreach ($entity_info as $name => $data) {\n        $entity_info[$name] += array(\n          'fieldable' => FALSE,\n          'controller class' => 'DrupalDefaultEntityController',\n          'static cache' => TRUE,\n          'field cache' => TRUE,\n          'load hook' => $name . '_load',\n          'bundles' => array(),\n          'view modes' => array(),\n          'entity keys' => array(),\n          'translation' => array(),\n        );\n        $entity_info[$name]['entity keys'] += array(\n          'revision' => '',\n          'bundle' => '',\n        );\n        foreach ($entity_info[$name]['view modes'] as $view_mode => $view_mode_info) {\n          $entity_info[$name]['view modes'][$view_mode] += array(\n            'custom settings' => FALSE,\n          );\n        }\n        // If no bundle key is provided, assume a single bundle, named after\n        // the entity type.\n        if (empty($entity_info[$name]['entity keys']['bundle']) && empty($entity_info[$name]['bundles'])) {\n          $entity_info[$name]['bundles'] = array($name => array('label' => $entity_info[$name]['label']));\n        }\n        // Prepare entity schema fields SQL info for\n        // DrupalEntityControllerInterface::buildQuery().\n        if (isset($entity_info[$name]['base table'])) {\n          $entity_info[$name]['schema_fields_sql']['base table'] = drupal_schema_fields_sql($entity_info[$name]['base table']);\n          if (isset($entity_info[$name]['revision table'])) {\n            $entity_info[$name]['schema_fields_sql']['revision table'] = drupal_schema_fields_sql($entity_info[$name]['revision table']);\n          }\n        }\n      }\n      // Let other modules alter the entity info.\n      drupal_alter('entity_info', $entity_info);\n      cache()->set(\"entity_info:$langcode\", $entity_info);\n    }\n  }\n\n  if (empty($entity_type)) {\n    return $entity_info;\n  }\n  elseif (isset($entity_info[$entity_type])) {\n    return $entity_info[$entity_type];\n  }\n}\n\n/**\n * Resets the cached information about entity types.\n */\nfunction entity_info_cache_clear() {\n  drupal_static_reset('entity_get_info');\n  // Clear all languages.\n  cache()->deletePrefix('entity_info:');\n}\n\n/**\n * Helper function to extract id, vid, and bundle name from an entity.\n *\n * @param $entity_type\n *   The entity type; e.g. 'node' or 'user'.\n * @param $entity\n *   The entity from which to extract values.\n * @return\n *   A numerically indexed array (not a hash table) containing these\n *   elements:\n *   0: primary id of the entity\n *   1: revision id of the entity, or NULL if $entity_type is not versioned\n *   2: bundle name of the entity\n */\nfunction entity_extract_ids($entity_type, $entity) {\n  $info = entity_get_info($entity_type);\n\n  // Objects being created might not have id/vid yet.\n  $id = isset($entity->{$info['entity keys']['id']}) ? $entity->{$info['entity keys']['id']} : NULL;\n  $vid = ($info['entity keys']['revision'] && isset($entity->{$info['entity keys']['revision']})) ? $entity->{$info['entity keys']['revision']} : NULL;\n\n  if (!empty($info['entity keys']['bundle'])) {\n    // Explicitly fail for malformed entities missing the bundle property.\n    if (!isset($entity->{$info['entity keys']['bundle']}) || $entity->{$info['entity keys']['bundle']} === '') {\n      throw new EntityMalformedException(t('Missing bundle property on entity of type @entity_type.', array('@entity_type' => $entity_type)));\n    }\n    $bundle = $entity->{$info['entity keys']['bundle']};\n  }\n  else {\n    // The entity type provides no bundle key: assume a single bundle, named\n    // after the entity type.\n    $bundle = $entity_type;\n  }\n\n  return array($id, $vid, $bundle);\n}\n\n/**\n * Helper function to assemble an object structure with initial ids.\n *\n * This function can be seen as reciprocal to entity_extract_ids().\n *\n * @param $entity_type\n *   The entity type; e.g. 'node' or 'user'.\n * @param $ids\n *   A numerically indexed array, as returned by entity_extract_ids(),\n *   containing these elements:\n *   0: primary id of the entity\n *   1: revision id of the entity, or NULL if $entity_type is not versioned\n *   2: bundle name of the entity, or NULL if $entity_type has no bundles\n *\n * @return\n *   An entity object, initialized with the IDs provided.\n */\nfunction entity_create_stub_entity($entity_type, $ids) {\n  $values = array();\n  $info = entity_get_info($entity_type);\n  $values[$info['entity keys']['id']] = $ids[0];\n  if (!empty($info['entity keys']['revision']) && isset($ids[1])) {\n    $values[$info['entity keys']['revision']] = $ids[1];\n  }\n  if (!empty($info['entity keys']['bundle']) && isset($ids[2])) {\n    $values[$info['entity keys']['bundle']] = $ids[2];\n  }\n  // @todo Once all entities are converted, just rely on entity_create().\n  return isset($info['entity class']) ? entity_create($entity_type, $values) : (object) $values;\n}\n\n/**\n * Loads entities from the database.\n *\n * This function should be used whenever you need to load more than one entity\n * from the database. The entities are loaded into memory and will not require\n * database access if loaded again during the same page request.\n *\n * The actual loading is done through a class that has to implement the\n * DrupalEntityControllerInterface interface. By default,\n * DrupalDefaultEntityController is used. Entity types can specify that a\n * different class should be used by setting the 'controller class' key in\n * hook_entity_info(). These classes can either implement the\n * DrupalEntityControllerInterface interface, or, most commonly, extend the\n * DrupalDefaultEntityController class. See node_entity_info() and the\n * NodeController in node.module as an example.\n *\n * @see hook_entity_info()\n * @see DrupalEntityControllerInterface\n * @see DrupalDefaultEntityController\n * @see EntityFieldQuery\n *\n * @param $entity_type\n *   The entity type to load, e.g. node or user.\n * @param $ids\n *   An array of entity IDs, or FALSE to load all entities.\n * @param $conditions\n *   (deprecated) An associative array of conditions on the base table, where\n *   the keys are the database fields and the values are the values those\n *   fields must have. Instead, it is preferable to use EntityFieldQuery to\n *   retrieve a list of entity IDs loadable by this function.\n * @param $reset\n *   Whether to reset the internal cache for the requested entity type.\n *\n * @return\n *   An array of entity objects indexed by their ids.\n *\n * @todo Remove $conditions in Drupal 8.\n */\nfunction entity_load($entity_type, $ids = FALSE, $conditions = array(), $reset = FALSE) {\n  if ($reset) {\n    entity_get_controller($entity_type)->resetCache();\n  }\n  return entity_get_controller($entity_type)->load($ids, $conditions);\n}\n\n/**\n * Loads the unchanged, i.e. not modified, entity from the database.\n *\n * Unlike entity_load() this function ensures the entity is directly loaded from\n * the database, thus bypassing any static cache. In particular, this function\n * is useful to determine changes by comparing the entity being saved to the\n * stored entity.\n *\n * @param $entity_type\n *   The entity type to load, e.g. node or user.\n * @param $id\n *   The id of the entity to load.\n *\n * @return\n *   The unchanged entity, or FALSE if the entity cannot be loaded.\n */\nfunction entity_load_unchanged($entity_type, $id) {\n  entity_get_controller($entity_type)->resetCache(array($id));\n  $result = entity_get_controller($entity_type)->load(array($id));\n  return reset($result);\n}\n\n/**\n * Deletes multiple entities permanently.\n *\n * @param $entity_type\n *   The type of the entity.\n * @param $ids\n *   An array of entity IDs of the entities to delete.\n */\nfunction entity_delete_multiple($entity_type, $ids) {\n  entity_get_controller($entity_type)->delete($ids);\n}\n\n/**\n * Constructs a new entity object, without saving it to the database.\n *\n * @param $entity_type\n *   The type of the entity.\n * @param $values\n *   An array of values to set, keyed by property name. If the entity type has\n *   bundles the bundle key has to be specified.\n *\n * @return EntityInterface\n *   A new entity object.\n */\nfunction entity_create($entity_type, array $values) {\n  $info = entity_get_info($entity_type) + array('entity class' => 'Entity');\n  $class = $info['entity class'];\n  return new $class($values, $entity_type);\n}\n\n/**\n * Gets the entity controller class for an entity type.\n */\nfunction entity_get_controller($entity_type) {\n  $controllers = &drupal_static(__FUNCTION__, array());\n  if (!isset($controllers[$entity_type])) {\n    $type_info = entity_get_info($entity_type);\n    $class = $type_info['controller class'];\n    $controllers[$entity_type] = new $class($entity_type);\n  }\n  return $controllers[$entity_type];\n}\n\n/**\n * Invokes hook_entity_prepare_view().\n *\n * If adding a new entity similar to nodes, comments or users, you should\n * invoke this function during the ENTITY_build_content() or\n * ENTITY_view_multiple() phases of rendering to allow other modules to alter\n * the objects during this phase. This is needed for situations where\n * information needs to be loaded outside of ENTITY_load() - particularly\n * when loading entities into one another - i.e. a user object into a node, due\n * to the potential for unwanted side-effects such as caching and infinite\n * recursion. By convention, entity_prepare_view() is called after\n * field_attach_prepare_view() to allow entity level hooks to act on content\n * loaded by field API.\n *\n * @see hook_entity_prepare_view()\n *\n * @param $entity_type\n *   The type of entity, i.e. 'node', 'user'.\n * @param $entities\n *   The entity objects which are being prepared for view, keyed by object ID.\n */\nfunction entity_prepare_view($entity_type, $entities) {\n  // To ensure hooks are only run once per entity, check for an\n  // entity_view_prepared flag and only process items without it.\n  // @todo: resolve this more generally for both entity and field level hooks.\n  $prepare = array();\n  foreach ($entities as $id => $entity) {\n    if (empty($entity->entity_view_prepared)) {\n      // Add this entity to the items to be prepared.\n      $prepare[$id] = $entity;\n\n      // Mark this item as prepared.\n      $entity->entity_view_prepared = TRUE;\n    }\n  }\n\n  if (!empty($prepare)) {\n    module_invoke_all('entity_prepare_view', $prepare, $entity_type);\n  }\n}\n\n/**\n * Returns the uri elements of an entity.\n *\n * @param $entity_type\n *   The entity type; e.g. 'node' or 'user'.\n * @param $entity\n *   The entity for which to generate a path.\n *\n * @return\n *   An array containing the 'path' and 'options' keys used to build the uri of\n *   the entity, and matching the signature of url(). NULL if the entity has no\n *   uri of its own.\n *\n * @todo\n *   Remove once all entity types are implementing the EntityInterface.\n */\nfunction entity_uri($entity_type, $entity) {\n  $info = entity_get_info($entity_type);\n  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);\n\n  // A bundle-specific callback takes precedence over the generic one for the\n  // entity type.\n  if (isset($info['bundles'][$bundle]['uri callback'])) {\n    $uri_callback = $info['bundles'][$bundle]['uri callback'];\n  }\n  elseif (isset($info['uri callback'])) {\n    $uri_callback = $info['uri callback'];\n  }\n  else {\n    return NULL;\n  }\n\n  // Invoke the callback to get the URI. If there is no callback, return NULL.\n  if (isset($uri_callback)) {\n    $uri = $uri_callback($entity);\n    // Pass the entity data to url() so that alter functions do not need to\n    // lookup this entity again.\n    $uri['options']['entity_type'] = $entity_type;\n    $uri['options']['entity'] = $entity;\n    return $uri;\n  }\n}\n\n/**\n * Returns the label of an entity.\n *\n * See the 'label callback' component of the hook_entity_info() return value\n * for more information.\n *\n * @param $entity_type\n *   The entity type; e.g., 'node' or 'user'.\n * @param $entity\n *   The entity for which to generate the label.\n *\n * @return\n *   The entity label, or FALSE if not found.\n *\n * @todo\n *   Remove once all entity types are implementing the EntityInterface.\n */\nfunction entity_label($entity_type, $entity) {\n  $label = FALSE;\n  $info = entity_get_info($entity_type);\n  if (isset($info['label callback'])) {\n    $label = $info['label callback']($entity_type, $entity);\n  }\n  elseif (!empty($info['entity keys']['label']) && isset($entity->{$info['entity keys']['label']})) {\n    $label = $entity->{$info['entity keys']['label']};\n  }\n\n  return $label;\n}\n\n/**\n * Helper function for attaching field API validation to entity forms.\n */\nfunction entity_form_field_validate($entity_type, $form, &$form_state) {\n  // All field attach API functions act on an entity object, but during form\n  // validation, we don't have one. $form_state contains the entity as it was\n  // prior to processing the current form submission, and we must not update it\n  // until we have fully validated the submitted input. Therefore, for\n  // validation, act on a pseudo entity created out of the form values.\n  $pseudo_entity = (object) $form_state['values'];\n  field_attach_form_validate($entity_type, $pseudo_entity, $form, $form_state);\n}\n\n/**\n * Helper function for copying submitted values to entity properties for simple entity forms.\n *\n * During the submission handling of an entity form's \"Save\", \"Preview\", and\n * possibly other buttons, the form state's entity needs to be updated with the\n * submitted form values. Each entity form implements its own builder function\n * for doing this, appropriate for the particular entity and form, whereas\n * modules may specify additional builder functions in $form['#entity_builders']\n * for copying the form values of added form elements to entity properties.\n * Many of the main entity builder functions can call this helper function to\n * re-use its logic of copying $form_state['values'][PROPERTY] values to\n * $entity->PROPERTY for all entries in $form_state['values'] that are not field\n * data, and calling field_attach_submit() to copy field data. Apart from that\n * this helper invokes any additional builder functions that have been specified\n * in $form['#entity_builders'].\n *\n * For some entity forms (e.g., forms with complex non-field data and forms that\n * simultaneously edit multiple entities), this behavior may be inappropriate,\n * so the builder function for such forms needs to implement the required\n * functionality instead of calling this function.\n */\nfunction entity_form_submit_build_entity($entity_type, $entity, $form, &$form_state) {\n  $info = entity_get_info($entity_type);\n  list(, , $bundle) = entity_extract_ids($entity_type, $entity);\n\n  // Copy top-level form values that are not for fields to entity properties,\n  // without changing existing entity properties that are not being edited by\n  // this form. Copying field values must be done using field_attach_submit().\n  $values_excluding_fields = $info['fieldable'] ? array_diff_key($form_state['values'], field_info_instances($entity_type, $bundle)) : $form_state['values'];\n  foreach ($values_excluding_fields as $key => $value) {\n    $entity->$key = $value;\n  }\n\n  // Invoke all specified builders for copying form values to entity properties.\n  if (isset($form['#entity_builders'])) {\n    foreach ($form['#entity_builders'] as $function) {\n      $function($entity_type, $entity, $form, $form_state);\n    }\n  }\n\n  // Copy field values to the entity.\n  if ($info['fieldable']) {\n    field_attach_submit($entity_type, $entity, $form, $form_state);\n  }\n}\n\n/**\n * Exception thrown when a malformed entity is passed.\n */\nclass EntityMalformedException extends Exception { }\n
===================================================================
--- core/modules/entity/entity.module	(revision 39326b2c7d629eabd652659dbc862d8296b728d0)
+++ core/modules/entity/entity.module	(revision )
@@ -449,7 +449,14 @@
  */
 function entity_form_submit_build_entity($entity_type, $entity, $form, &$form_state) {
   $info = entity_get_info($entity_type);
+  // Pull the bundle property from the submitted form values if the entity type
+  // supports bundles and the form provides a value for the bundle key.
+  if (!empty($info['entity keys']['bundle']) && isset($form_state['values'][$info['entity keys']['bundle']])) {
+    $bundle = $form_state['values'][$info['entity keys']['bundle']];
+  }
+  else {
-  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
+    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
