Newer
Older
Wolfgang Ziegler
committed
<?php
/**
* @file
* Module file for the entity API.
Wolfgang Ziegler
committed
*/
module_load_include('inc', 'entity', 'modules/callbacks');
module_load_include('inc', 'entity', 'includes/entity.property');
Wolfgang Ziegler
committed
/**
* Defines status codes used for exportable entities.
*/
/**
* A bit flag used to let us know if an entity is in the database.
*/
Wolfgang Ziegler
committed
/**
* A bit flag used to let us know if an entity has been customly defined.
*/
define('ENTITY_CUSTOM', 0x01);
/**
* Deprecated, but still here for backward compatibility.
*/
Wolfgang Ziegler
committed
define('ENTITY_IN_DB', 0x01);
/**
* A bit flag used to let us know if an entity is a 'default' in code.
*/
define('ENTITY_IN_CODE', 0x02);
Wolfgang Ziegler
committed
/**
* A bit flag used to mark entities as overridden, e.g. they were originally
* definded in code and are saved now in the database. Same as
Wolfgang Ziegler
committed
* (ENTITY_CUSTOM | ENTITY_IN_CODE).
Wolfgang Ziegler
committed
*/
define('ENTITY_OVERRIDDEN', 0x03);
Wolfgang Ziegler
committed
/**
* A bit flag used to mark entities as fixed, thus not changeable for any
* user.
*/
Wolfgang Ziegler
committed
define('ENTITY_FIXED', 0x04 | 0x02);
Wolfgang Ziegler
committed
/**
* Determines whether for the given entity type a given operation is available.
*
* @param $entity_type
* The type of the entity.
* @param $op
Wolfgang Ziegler
committed
* One of 'create', 'view', 'save', 'delete', 'revision delete', 'access' or
* 'form'.
*
* @return boolean
* Whether the entity type supports the given operation.
*/
function entity_type_supports($entity_type, $op) {
$info = entity_get_info($entity_type);
$keys = array(
'view' => 'view callback',
'create' => 'creation callback',
'delete' => 'deletion callback',
Wolfgang Ziegler
committed
'revision delete' => 'revision deletion callback',
'save' => 'save callback',
'access' => 'access callback',
'form' => 'form callback'
if (isset($info[$keys[$op]])) {
return TRUE;
}
Wolfgang Ziegler
committed
if ($op == 'revision delete') {
return in_array('EntityAPIControllerInterface', class_implements($info['controller class']));
Wolfgang Ziegler
committed
if ($op == 'form') {
return (bool) entity_ui_controller($entity_type);
Wolfgang Ziegler
committed
if ($op != 'access') {
return in_array('EntityAPIControllerInterface', class_implements($info['controller class']));
}
return FALSE;
}
Wolfgang Ziegler
committed
/**
* Menu loader function: load an entity from its path.
*
* This can be used to load entities of all types in menu paths:
*
* @code
* $items['myentity/%entity_object'] = array(
* 'load arguments' => array('myentity'),
* 'title' => ...,
* 'page callback' => ...,
* 'page arguments' => array(...),
* 'access arguments' => array(...),
* );
* @endcode
*
* @param $entity_id
* The ID of the entity to load, passed by the menu URL.
* @param $entity_type
* The type of the entity to load.
* @return
* A fully loaded entity object, or FALSE in case of error.
*/
function entity_object_load($entity_id, $entity_type) {
$entities = entity_load($entity_type, array($entity_id));
return reset($entities);
}
Wolfgang Ziegler
committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* A wrapper around entity_load() to load a single entity by name or numeric id.
*
* @todo: Re-name entity_load() to entity_load_multiple() in d8 core and this
* to entity_load().
*
* @param $entity_type
* The entity type to load, e.g. node or user.
* @param $id
* The entity id, either the numeric id or the entity name. In case the entity
* type has specified a name key, both the numeric id and the name may be
* passed.
*
* @return
* The entity object, or FALSE.
*
* @see entity_load()
*/
function entity_load_single($entity_type, $id) {
$entities = entity_load($entity_type, array($id));
return reset($entities);
}
/**
* A wrapper around entity_load() to return entities keyed by name key if existing.
*
* @param $entity_type
* The entity type to load, e.g. node or user.
* @param $names
* An array of entity names or ids, or FALSE to load all entities.
* @param $conditions
* (deprecated) An associative array of conditions on the base table, where
* the keys are the database fields and the values are the values those
* fields must have. Instead, it is preferable to use EntityFieldQuery to
* retrieve a list of entity IDs loadable by this function.
*
* @return
* An array of entity objects indexed by their names (or ids if the entity
* type has no name key).
*
* @see entity_load()
*/
function entity_load_multiple_by_name($entity_type, $names = FALSE, $conditions = array()) {
$entities = entity_load($entity_type, $names, $conditions);
$info = entity_get_info($entity_type);
if (!isset($info['entity keys']['name'])) {
return $entities;
}
return entity_key_array_by_property($entities, $info['entity keys']['name']);
}
/**
* Permanently save an entity.
*
Wolfgang Ziegler
committed
* In case of failures, an exception is thrown.
*
* @param $entity_type
* The type of the entity.
* @param $entity
* The entity to save.
Wolfgang Ziegler
committed
*
* @return
* For entity types provided by the CRUD API, SAVED_NEW or SAVED_UPDATED is
* returned depending on the operation performed. If there is no information
* how to save the entity, FALSE is returned.
*
* @see entity_type_supports()
*/
Wolfgang Ziegler
committed
function entity_save($entity_type, $entity) {
$info = entity_get_info($entity_type);
if (method_exists($entity, 'save')) {
return $entity->save();
}
elseif (isset($info['save callback'])) {
$info['save callback']($entity);
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->save($entity);
}
Wolfgang Ziegler
committed
else {
return FALSE;
}
}
/**
* Permanently delete the given entity.
*
Wolfgang Ziegler
committed
* In case of failures, an exception is thrown.
*
* @param $entity_type
* The type of the entity.
* @param $id
* The uniform identifier of the entity to delete.
* @return
* FALSE, if there were no information how to delete the entity.
*
* @see entity_type_supports()
*/
function entity_delete($entity_type, $id) {
Wolfgang Ziegler
committed
return entity_delete_multiple($entity_type, array($id));
}
/**
* Permanently delete multiple entities.
*
* @param $entity_type
* The type of the entity.
* @param $ids
Wolfgang Ziegler
committed
* An array of entity ids of the entities to delete. In case the entity makes
* use of a name key, both the names or numeric ids may be passed.
* @return
* FALSE if the given entity type isn't compatible to the CRUD API.
*/
function entity_delete_multiple($entity_type, $ids) {
$info = entity_get_info($entity_type);
Wolfgang Ziegler
committed
if (isset($info['deletion callback'])) {
foreach ($ids as $id) {
$info['deletion callback']($id);
}
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
entity_get_controller($entity_type)->delete($ids);
}
else {
return FALSE;
}
}
Wolfgang Ziegler
committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Loads an entity revision.
*
* @param $entity_type
* The type of the entity.
* @param $revision_id
* The id of the revision to load.
*
* @return
* The entity object, or FALSE if there is no entity with the given revision
* id.
*/
function entity_revision_load($entity_type, $revision_id) {
$info = entity_get_info($entity_type);
if (!empty($info['entity keys']['revision'])) {
$entity_revisions = entity_load($entity_type, FALSE, array($info['entity keys']['revision'] => $revision_id));
return reset($entity_revisions);
}
return FALSE;
}
/**
* Deletes an entity revision.
*
* @param $entity_type
* The type of the entity.
* @param $revision_id
* The revision ID to delete.
*
* @return
* TRUE if the entity revision could be deleted, FALSE otherwise.
*/
function entity_revision_delete($entity_type, $revision_id) {
$info = entity_get_info($entity_type);
if (isset($info['revision deletion callback'])) {
return $info['revision deletion callback']($revision_id, $entity_type);
}
elseif (in_array('EntityAPIControllerRevisionableInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->deleteRevision($revision_id);
}
return FALSE;
}
/**
* Checks whether the given entity is the default revision.
*
Wolfgang Ziegler
committed
* Note that newly created entities will always be created in default revision,
* thus TRUE is returned for not yet saved entities.
Wolfgang Ziegler
committed
*
* @param $entity_type
* The type of the entity.
* @param $entity
* The entity object to check.
*
* @return boolean
Wolfgang Ziegler
committed
* A boolean indicating whether the entity is in default revision is returned.
* If the entity is not revisionable or is new, TRUE is returned.
Wolfgang Ziegler
committed
*
* @see entity_revision_set_default()
*/
function entity_revision_is_default($entity_type, $entity) {
$info = entity_get_info($entity_type);
if (empty($info['entity keys']['revision'])) {
return TRUE;
}
Wolfgang Ziegler
committed
// Newly created entities will always be created in default revision.
if (!empty($entity->is_new) || empty($entity->{$info['entity keys']['id']})) {
return TRUE;
}
Wolfgang Ziegler
committed
if (in_array('EntityAPIControllerRevisionableInterface', class_implements($info['controller class']))) {
$key = !empty($info['entity keys']['default revision']) ? $info['entity keys']['default revision'] : 'default_revision';
return !empty($entity->$key);
}
Wolfgang Ziegler
committed
else {
// Else, just load the default entity and compare the ID. Usually, the
// entity should be already statically cached anyway.
$default = entity_load_single($entity_type, $entity->{$info['entity keys']['id']});
return $default->{$info['entity keys']['revision']} == $entity->{$info['entity keys']['revision']};
}
Wolfgang Ziegler
committed
}
/**
* Sets a given entity revision as default revision.
*
* Note that the default revision flag will only be supported by entity types
* based upon the EntityAPIController, i.e. implementing the
* EntityAPIControllerRevisionableInterface.
*
* @param $entity_type
* The type of the entity.
* @param $entity
* The entity revision to update.
*
* @see entity_revision_is_default()
*/
function entity_revision_set_default($entity_type, $entity) {
$info = entity_get_info($entity_type);
if (!empty($info['entity keys']['revision'])) {
$key = !empty($info['entity keys']['default revision']) ? $info['entity keys']['default revision'] : 'default_revision';
$entity->$key = TRUE;
}
}
/**
* Create a new entity object.
*
* @param $entity_type
* The type of the entity.
* @param $values
* An array of values to set, keyed by property name. If the entity type has
* bundles the bundle key has to be specified.
* @return
* A new instance of the entity type or FALSE if there is no information for
* the given entity type.
*
* @see entity_type_supports()
*/
Wolfgang Ziegler
committed
function entity_create($entity_type, array $values) {
$info = entity_get_info($entity_type);
if (isset($info['creation callback'])) {
return $info['creation callback']($values, $entity_type);
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->create($values);
}
return FALSE;
}
Wolfgang Ziegler
committed
/**
* Exports an entity.
*
* Note: Currently, this only works for entity types provided with the entity
* CRUD API.
*
Wolfgang Ziegler
committed
* @param $entity_type
* The type of the entity.
* @param $entity
* The entity to export.
* @param $prefix
* An optional prefix for each line.
Wolfgang Ziegler
committed
* @return
Wolfgang Ziegler
committed
* The exported entity as serialized string. The format is determined by the
* respective entity controller, e.g. it is JSON for the EntityAPIController.
* The output is suitable for entity_import().
Wolfgang Ziegler
committed
*/
function entity_export($entity_type, $entity, $prefix = '') {
Wolfgang Ziegler
committed
if (method_exists($entity, 'export')) {
return $entity->export($prefix);
Wolfgang Ziegler
committed
}
Wolfgang Ziegler
committed
$info = entity_get_info($entity_type);
if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->export($entity, $prefix);
Wolfgang Ziegler
committed
}
}
Wolfgang Ziegler
committed
/**
* Imports an entity.
*
* Note: Currently, this only works for entity types provided with the entity
* CRUD API.
*
Wolfgang Ziegler
committed
* @param $entity_type
* The type of the entity.
* @param string $export
* The string containing the serialized entity as produced by
* entity_export().
* @return
* The imported entity object not yet saved.
*/
function entity_import($entity_type, $export) {
$info = entity_get_info($entity_type);
if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->import($export);
}
}
Wolfgang Ziegler
committed
/**
* Checks whether an entity type is fieldable.
*
* @param $entity_type
* The type of the entity.
*
* @return
* TRUE if the entity type is fieldable, FALSE otherwise.
*/
function entity_type_is_fieldable($entity_type) {
$info = entity_get_info($entity_type);
return !empty($info['fieldable']);
}
/**
* Builds a structured array representing the entity's content.
*
* The content built for the entity will vary depending on the $view_mode
* parameter.
*
* Note: Currently, this only works for entity types provided with the entity
* CRUD API.
*
* @param $entity_type
* The type of the entity.
* @param $entity
* An entity object.
* @param $view_mode
* A view mode as used by this entity type, e.g. 'full', 'teaser'...
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
* @return
* The renderable array.
*/
function entity_build_content($entity_type, $entity, $view_mode = 'full', $langcode = NULL) {
$info = entity_get_info($entity_type);
if (method_exists($entity, 'buildContent')) {
return $entity->buildContent($view_mode, $langcode);
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->buildContent($entity, $view_mode, $langcode);
}
}
/**
Wolfgang Ziegler
committed
* Returns the entity identifier, i.e. the entities name or numeric id.
*
Wolfgang Ziegler
committed
* Unlike entity_extract_ids() this function returns the name of the entity
* instead of the numeric id, in case the entity type has specified a name key.
*
* @param $entity_type
* The type of the entity.
* @param $entity
* An entity object.
*
* @see entity_extract_ids()
*/
function entity_id($entity_type, $entity) {
Wolfgang Ziegler
committed
if (method_exists($entity, 'identifier')) {
return $entity->identifier();
}
Wolfgang Ziegler
committed
$info = entity_get_info($entity_type);
$key = isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'];
return isset($entity->$key) ? $entity->$key : NULL;
}
/**
* Generate an array for rendering the given entities.
*
Wolfgang Ziegler
committed
* Entities being viewed, are generally expected to be fully-loaded entity
* objects, thus have their name or id key set. However, it is possible to
* view a single entity without any id, e.g. for generating a preview during
* creation.
*
* @param $entity_type
* The type of the entity.
* @param $entities
Wolfgang Ziegler
committed
* An array of entities to render.
* @param $view_mode
* A view mode as used by this entity type, e.g. 'full', 'teaser'...
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
Wolfgang Ziegler
committed
* @param $page
* (optional) If set will control if the entity is rendered: if TRUE
* the entity will be rendered without its title, so that it can be embeded
* in another context. If FALSE the entity will be displayed with its title
* in a mode suitable for lists.
* If unset, the page mode will be enabled if the current path is the URI
* of the entity, as returned by entity_uri().
* This parameter is only supported for entities which controller is a
* EntityAPIControllerInterface.
* @return
Wolfgang Ziegler
committed
* The renderable array, keyed by the entity type and by entity identifiers,
* for which the entity name is used if existing - see entity_id(). If there
* is no information on how to view an entity, FALSE is returned.
*/
Wolfgang Ziegler
committed
function entity_view($entity_type, $entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
$info = entity_get_info($entity_type);
if (isset($info['view callback'])) {
Wolfgang Ziegler
committed
$entities = entity_key_array_by_property($entities, $info['entity keys']['id']);
return $info['view callback']($entities, $view_mode, $langcode, $entity_type);
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
Wolfgang Ziegler
committed
return entity_get_controller($entity_type)->view($entities, $view_mode, $langcode, $page);
}
Wolfgang Ziegler
committed
return FALSE;
}
Wolfgang Ziegler
committed
/**
* Determines whether the given user has access to an entity.
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create' or
* 'delete'.
* @param $entity_type
* The entity type of the entity to check for.
* @param $entity
* Optionally an entity to check access for. If no entity is given, it will be
* determined whether access is allowed for all entities of the given type.
* @param $account
* The user to check for. Leave it to NULL to check for the global user.
*
* @return boolean
* Whether access is allowed or not. If the entity type does not specify any
* access information, NULL is returned.
*
* @see entity_type_supports()
Wolfgang Ziegler
committed
*/
function entity_access($op, $entity_type, $entity = NULL, $account = NULL) {
if (($info = entity_get_info()) && isset($info[$entity_type]['access callback'])) {
return $info[$entity_type]['access callback']($op, $entity, $account, $entity_type);
}
}
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/**
* Gets the edit form for any entity.
*
* This helper makes use of drupal_get_form() and the regular form builder
* function of the entity type to retrieve and process the form as usual.
*
* In order to use this helper to show an entity add form, the new entity object
* can be created via entity_create() or entity_property_values_create_entity().
*
* @param $entity_type
* The type of the entity.
* @param $entity
* The entity to show the edit form for.
* @return
* The renderable array of the form. If there is no entity form or missing
* metadata, FALSE is returned.
*
* @see entity_type_supports()
*/
function entity_form($entity_type, $entity) {
$info = entity_get_info($entity_type);
if (isset($info['form callback'])) {
return $info['form callback']($entity, $entity_type);
}
// If there is an UI controller, the providing module has to implement the
// entity form using entity_ui_get_form().
elseif (entity_ui_controller($entity_type)) {
return entity_metadata_form_entity_ui($entity, $entity_type);
}
return FALSE;
}
Wolfgang Ziegler
committed
/**
* Converts an array of entities to be keyed by the values of a given property.
*
* @param array $entities
* The array of entities to convert.
* @param $property
* The name of entity property, by which the array should be keyed. To get
* reasonable results, the property has to have unique values.
*
* @return array
* The same entities in the same order, but keyed by their $property values.
*/
function entity_key_array_by_property(array $entities, $property) {
$ret = array();
foreach ($entities as $entity) {
$key = isset($entity->$property) ? $entity->$property : NULL;
$ret[$key] = $entity;
}
return $ret;
}
/**
* Get the entity info for the entity types provided via the entity CRUD API.
*
* @return
* An array in the same format as entity_get_info(), containing the entities
* whose controller class implements the EntityAPIControllerInterface.
*/
function entity_crud_get_info() {
$types = array();
foreach (entity_get_info() as $type => $info) {
if (isset($info['controller class']) && in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
$types[$type] = $info;
}
}
return $types;
}
Wolfgang Ziegler
committed
/**
* Checks if a given entity has a certain exportable status.
Wolfgang Ziegler
committed
*
* @param $entity_type
* The type of the entity.
* @param $entity
* The entity to check the status on.
* @param $status
Wolfgang Ziegler
committed
* The constant status like ENTITY_CUSTOM, ENTITY_IN_CODE, ENTITY_OVERRIDDEN
Wolfgang Ziegler
committed
* or ENTITY_FIXED.
*
* @return
* TRUE if the entity has the status, FALSE otherwise.
*/
function entity_has_status($entity_type, $entity, $status) {
$info = entity_get_info($entity_type);
$status_key = empty($info['entity keys']['status']) ? 'status' : $info['entity keys']['status'];
Wolfgang Ziegler
committed
return isset($entity->{$status_key}) && ($entity->{$status_key} & $status) == $status;
Wolfgang Ziegler
committed
}
Wolfgang Ziegler
committed
/**
* Export a variable. Copied from ctools.
*
* This is a replacement for var_export(), allowing us to more nicely
* format exports. It will recurse down into arrays and will try to
* properly export bools when it can.
*/
function entity_var_export($var, $prefix = '') {
if (is_array($var)) {
if (empty($var)) {
$output = 'array()';
}
else {
$output = "array(\n";
foreach ($var as $key => $value) {
$output .= " '$key' => " . entity_var_export($value, ' ') . ",\n";
}
$output .= ')';
}
}
Wolfgang Ziegler
committed
elseif (is_bool($var)) {
Wolfgang Ziegler
committed
$output = $var ? 'TRUE' : 'FALSE';
}
else {
$output = var_export($var, TRUE);
}
if ($prefix) {
$output = str_replace("\n", "\n$prefix", $output);
}
return $output;
}
/**
* Export a variable in pretty formatted JSON.
*/
function entity_var_json_export($var, $prefix = '') {
if (is_array($var) && $var) {
// Defines whether we use a JSON array or object.
$use_array = ($var == array_values($var));
$output = $use_array ? "[" : "{";
foreach ($var as $key => $value) {
if ($use_array) {
$values[] = entity_var_json_export($value, ' ');
}
else {
$values[] = entity_var_json_export((string) $key, ' ') . ' : ' . entity_var_json_export($value, ' ');
}
}
// Use several lines for long content. However for objects with a single
// entry keep the key in the first line.
Wolfgang Ziegler
committed
if (strlen($content = implode(', ', $values)) > 70 && ($use_array || count($values) > 1)) {
$output .= "\n " . implode(",\n ", $values) . "\n";
}
elseif (strpos($content, "\n") !== FALSE) {
Wolfgang Ziegler
committed
$output .= " " . $content . "\n";
}
else {
Wolfgang Ziegler
committed
$output .= " " . $content . ' ';
}
Wolfgang Ziegler
committed
$output .= $use_array ? ']' : '}';
}
else {
$output = drupal_json_encode($var);
}
if ($prefix) {
$output = str_replace("\n", "\n$prefix", $output);
}
return $output;
}
Wolfgang Ziegler
committed
/**
Wolfgang Ziegler
committed
* Rebuild the default entities provided in code.
*
* Exportable entities provided in code get saved to the database once a module
* providing defaults in code is activated. This allows module and entity_load()
* to easily deal with exportable entities just by relying on the database.
Wolfgang Ziegler
committed
*
Wolfgang Ziegler
committed
* The defaults get rebuilt if the cache is cleared or new modules providing
* defaults are enabled, such that the defaults in the database are up to date.
* A default entity gets updated with the latest defaults in code during rebuild
* as long as the default has not been overridden. Once a module providing
* defaults is disabled, its default entities get removed from the database
* unless they have been overridden. In that case the overridden entity is left
* in the database, but its status gets updated to 'custom'.
*
* @param $entity_types
* (optional) If specified, only the defaults of the given entity types are
* rebuilt.
Wolfgang Ziegler
committed
*/
Wolfgang Ziegler
committed
function entity_defaults_rebuild($entity_types = NULL) {
if (!isset($entity_types)) {
$entity_types = array();
foreach (entity_crud_get_info() as $type => $info) {
if (!empty($info['exportable'])) {
$entity_types[] = $type;
}
};
Wolfgang Ziegler
committed
}
foreach ($entity_types as $type) {
_entity_defaults_rebuild($type);
Wolfgang Ziegler
committed
}
}
/**
* Actually rebuild the defaults of a given entity type.
Wolfgang Ziegler
committed
*/
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])) {
Wolfgang Ziegler
committed
$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);
}
Wolfgang Ziegler
committed
unset($entity->is_rebuild);
}
}
// Load all existing entities.
$existing_entities = entity_load_multiple_by_name($entity_type, array_keys($entities));
Wolfgang Ziegler
committed
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);
}
Wolfgang Ziegler
committed
// The entity is overridden, so we do not need to save the default.
unset($entities[$name]);
}
Wolfgang Ziegler
committed
}
// Save defaults.
Wolfgang Ziegler
committed
$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']};
Wolfgang Ziegler
committed
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;
Wolfgang Ziegler
committed
// Keep original entities for hook_{entity_type}_defaults_rebuild()
// implementations.
$originals[$name] = $entity->original;
$entity->{$keys['status']} |= ENTITY_IN_CODE;
Wolfgang Ziegler
committed
$entity->is_rebuild = TRUE;
entity_save($entity_type, $entity);
Wolfgang Ziegler
committed
unset($entity->is_rebuild);
Wolfgang Ziegler
committed
}
Wolfgang Ziegler
committed
Wolfgang Ziegler
committed
// 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);
Wolfgang Ziegler
committed
lock_release('entity_rebuild_' . $entity_type);
}
}
/**
* Implements hook_modules_enabled().
*/
function entity_modules_enabled($modules) {
foreach (_entity_modules_get_default_types($modules) as $type) {
_entity_defaults_rebuild($type);
Wolfgang Ziegler
committed
}
}
/**
* Implements hook_modules_disabled().
*/
function entity_modules_disabled($modules) {
Wolfgang Ziegler
committed
foreach (_entity_modules_get_default_types($modules) as $entity_type) {
$info = entity_get_info($entity_type);
Wolfgang Ziegler
committed
// Do nothing if the module providing the entity type has been disabled too.
if (isset($info['module']) && in_array($info['module'], $modules)) {
return;
}
Wolfgang Ziegler
committed
$keys = $info['entity keys'] + array('module' => 'module', 'status' => 'status', 'name' => $info['entity keys']['id']);
// Remove entities provided in code by one of the disabled modules.
Wolfgang Ziegler
committed
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type, '=')
->propertyCondition($keys['module'], $modules, 'IN')
->propertyCondition($keys['status'], array(ENTITY_IN_CODE, ENTITY_FIXED), 'IN');
$result = $query->execute();
if (isset($result[$entity_type])) {
$entities = entity_load($entity_type, array_keys($result[$entity_type]));
entity_delete_multiple($entity_type, array_keys($entities));
Wolfgang Ziegler
committed
}
Wolfgang Ziegler
committed
// Update overridden entities to be now custom.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type, '=')
->propertyCondition($keys['module'], $modules, 'IN')
->propertyCondition($keys['status'], ENTITY_OVERRIDDEN, '=');
$result = $query->execute();
if (isset($result[$entity_type])) {
foreach (entity_load($entity_type, array_keys($result[$entity_type])) as $name => $entity) {
$entity->{$keys['status']} = ENTITY_CUSTOM;
$entity->{$keys['module']} = NULL;
entity_save($entity_type, $entity);
Wolfgang Ziegler
committed
}
}
Wolfgang Ziegler
committed
// Rebuild the remaining defaults so any alterations of the disabled modules
// are gone.
_entity_defaults_rebuild($entity_type);
}
Wolfgang Ziegler
committed
}
/**
* Gets all entity types for which defaults are provided by the $modules.
*/
function _entity_modules_get_default_types($modules) {
$types = array();
foreach (entity_crud_get_info() as $entity_type => $info) {
if (!empty($info['exportable'])) {
$hook = isset($info['export']['default hook']) ? $info['export']['default hook'] : 'default_' . $entity_type;
foreach ($modules as $module) {
if (module_hook($module, $hook) || module_hook($module, $hook . '_alter')) {
$types[] = $entity_type;
}
Wolfgang Ziegler
committed
}
}
}
Wolfgang Ziegler
committed
return $types;
}
Wolfgang Ziegler
committed
/**
* Defines schema fields required for exportable entities.
Wolfgang Ziegler
committed
*
* Warning: Do not call this function in your module's hook_schema()
* implementation or update functions. It is not safe to call functions of
* dependencies at this point. Instead of calling the function, just copy over
* the content.
* For more details see the issue http://drupal.org/node/1122812.
Wolfgang Ziegler
committed
*/
function entity_exportable_schema_fields($module_col = 'module', $status_col = 'status') {
return array(
$status_col => array(
'type' => 'int',
'not null' => TRUE,
Wolfgang Ziegler
committed
// Set the default to ENTITY_CUSTOM without using the constant as it is
// not safe to use it at this point.
'default' => 0x01,
Wolfgang Ziegler
committed
'size' => 'tiny',
'description' => 'The exportable status of the entity.',
),
$module_col => array(
'description' => 'The name of the providing module if the entity has been defined in code.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
);
}
Wolfgang Ziegler
committed
/**
* Implements hook_flush_caches().
*/
function entity_flush_caches() {
entity_property_info_cache_clear();
// Re-build defaults in code, however skip it on the admin modules page. In
// case of enabling or disabling modules we already rebuild defaults in
// entity_modules_enabled() and entity_modules_disabled(), so we do not need
// to do it again.
if (current_path() != 'admin/modules/list/confirm') {
entity_defaults_rebuild();
}
Wolfgang Ziegler
committed
}
/**
* Implements hook_theme().
*/
function entity_theme() {
return array(
'entity_status' => array(
'variables' => array('status' => NULL, 'html' => TRUE),
),
'entity' => array(
'render element' => 'elements',
'template' => 'entity',
),
Wolfgang Ziegler
committed
'entity_ui_overview_item' => array(
'variables' => array('label' => NULL, 'entity_type' => NULL, 'url' => FALSE, 'name' => FALSE),
'file' => 'includes/entity.ui.inc'
Wolfgang Ziegler
committed
),
);
}
/**
* Themes the exportable status of an entity.
*/
function theme_entity_status($variables) {
$status = $variables['status'];
$html = $variables['html'];
Wolfgang Ziegler
committed
if (($status & ENTITY_FIXED) == ENTITY_FIXED) {
$label = t('Fixed');
Wolfgang Ziegler
committed
$help = t('The configuration is fixed and cannot be changed.');
return $html ? "<span class='entity-status-fixed' title='$help'>" . $label . "</span>" : $label;
}
Wolfgang Ziegler
committed
elseif (($status & ENTITY_OVERRIDDEN) == ENTITY_OVERRIDDEN) {
$label = t('Overridden');
Wolfgang Ziegler
committed
$help = t('This configuration is provided by a module, but has been changed.');
return $html ? "<span class='entity-status-overridden' title='$help'>" . $label . "</span>" : $label;
}
elseif ($status & ENTITY_IN_CODE) {
$label = t('Default');
$help = t('A module provides this configuration.');
return $html ? "<span class='entity-status-default' title='$help'>" . $label . "</span>" : $label;
}
Wolfgang Ziegler
committed
elseif ($status & ENTITY_CUSTOM) {
$label = t('Custom');