function _entity_defaults_rebuild in Entity API 7
Actually rebuild the defaults of a given entity type.
3 calls to _entity_defaults_rebuild()
- entity_defaults_rebuild in ./
entity.module - Rebuild the default entities provided in code.
- entity_modules_disabled in ./
entity.module - Implements hook_modules_disabled().
- entity_modules_enabled in ./
entity.module - Implements hook_modules_enabled().
File
- ./
entity.module, line 875
Code
function _entity_defaults_rebuild($entity_type) {
if (lock_acquire('entity_rebuild_' . $entity_type)) {
$info = entity_get_info($entity_type);
$hook = isset($info['export']['default hook']) ? $info['export']['default hook'] : 'default_' . $entity_type;
$keys = $info['entity keys'] + array(
'module' => 'module',
'status' => 'status',
'name' => $info['entity keys']['id'],
);
// Check for the existence of the module and status columns.
if (!in_array($keys['status'], $info['schema_fields_sql']['base table']) || !in_array($keys['module'], $info['schema_fields_sql']['base table'])) {
trigger_error("Missing database columns for the exportable entity {$entity_type} as defined by entity_exportable_schema_fields(). Update the according module and run update.php!", E_USER_WARNING);
return;
}
// Invoke the hook and collect default entities.
$entities = array();
foreach (module_implements($hook) as $module) {
foreach ((array) module_invoke($module, $hook) as $name => $entity) {
$entity->{$keys['name']} = $name;
$entity->{$keys['module']} = $module;
$entities[$name] = $entity;
}
}
drupal_alter($hook, $entities);
// Check for defaults that disappeared.
$existing_defaults = entity_load_multiple_by_name($entity_type, FALSE, array(
$keys['status'] => array(
ENTITY_OVERRIDDEN,
ENTITY_IN_CODE,
ENTITY_FIXED,
),
));
foreach ($existing_defaults as $name => $entity) {
if (empty($entities[$name])) {
$entity->is_rebuild = TRUE;
if (entity_has_status($entity_type, $entity, ENTITY_OVERRIDDEN)) {
$entity->{$keys['status']} = ENTITY_CUSTOM;
entity_save($entity_type, $entity);
}
else {
entity_delete($entity_type, $name);
}
unset($entity->is_rebuild);
}
}
// Load all existing entities.
$existing_entities = entity_load_multiple_by_name($entity_type, array_keys($entities));
foreach ($existing_entities as $name => $entity) {
if (entity_has_status($entity_type, $entity, ENTITY_CUSTOM)) {
// If the entity already exists but is not yet marked as overridden, we
// have to update the status.
if (!entity_has_status($entity_type, $entity, ENTITY_OVERRIDDEN)) {
$entity->{$keys['status']} |= ENTITY_OVERRIDDEN;
$entity->{$keys['module']} = $entities[$name]->{$keys['module']};
$entity->is_rebuild = TRUE;
entity_save($entity_type, $entity);
unset($entity->is_rebuild);
}
// The entity is overridden, so we do not need to save the default.
unset($entities[$name]);
}
}
// Save defaults.
$originals = array();
foreach ($entities as $name => $entity) {
if (!empty($existing_entities[$name])) {
// Make sure we are updating the existing default.
$entity->{$keys['id']} = $existing_entities[$name]->{$keys['id']};
unset($entity->is_new);
}
// Pre-populate $entity->original as we already have it. So we avoid
// loading it again.
$entity->original = !empty($existing_entities[$name]) ? $existing_entities[$name] : FALSE;
// Keep original entities for hook_{entity_type}_defaults_rebuild()
// implementations.
$originals[$name] = $entity->original;
if (!isset($entity->{$keys['status']})) {
$entity->{$keys['status']} = ENTITY_IN_CODE;
}
else {
$entity->{$keys['status']} |= ENTITY_IN_CODE;
}
$entity->is_rebuild = TRUE;
entity_save($entity_type, $entity);
unset($entity->is_rebuild);
}
// Invoke an entity type-specific hook so modules may apply changes, e.g.
// efficiently rebuild caches.
module_invoke_all($entity_type . '_defaults_rebuild', $entities, $originals);
lock_release('entity_rebuild_' . $entity_type);
}
}