function ctools_export_load_object in Chaos Tool Suite (ctools) 7
Same name and namespace in other branches
- 6 includes/export.inc \ctools_export_load_object()
Load some number of exportable objects.
This function will cache the objects, load subsidiary objects if necessary, check default objects in code and properly set them up. It will cache the results so that multiple calls to load the same objects will not cause problems.
It attempts to reduce, as much as possible, the number of queries involved.
Parameters
$table: The name of the table to be loaded from. Data is expected to be in the schema to make all this work.
$type: A string to notify the loader what the argument is
- all: load all items. This is the default. $args is unused.
- names: $args will be an array of specific named objects to load.
- conditions: $args will be a keyed array of conditions. The conditions must be in the schema for this table or errors will result.
$args: An array of arguments whose actual use is defined by the $type argument.
12 calls to ctools_export_load_object()
- ctools_export_crud_load in includes/
export.inc - Load a single exportable object.
- ctools_export_crud_load_all in includes/
export.inc - Load all exportable objects of a given type.
- ctools_export_crud_load_multiple in includes/
export.inc - Load multiple exportable objects.
- page_manager_export_task_handler_load in page_manager/
page_manager.module - Loads page manager handler for export.
- page_manager_handler_check_machine_name in page_manager/
page_manager.admin.inc
1 string reference to 'ctools_export_load_object'
- ctools_export_load_object_reset in includes/
export.inc - Reset all static caches in ctools_export_load_object() or static caches for a given table in ctools_export_load_object().
File
- includes/
export.inc, line 387 - Contains code to make it easier to have exportable objects.
Code
function ctools_export_load_object($table, $type = 'all', $args = array()) {
$cache =& drupal_static(__FUNCTION__);
$cache_table_exists =& drupal_static(__FUNCTION__ . '_table_exists', array());
$cached_database =& drupal_static('ctools_export_load_object_all');
if (!array_key_exists($table, $cache_table_exists)) {
$cache_table_exists[$table] = db_table_exists($table);
}
$schema = ctools_export_get_schema($table);
if (empty($schema) || !$cache_table_exists[$table]) {
return array();
}
$export = $schema['export'];
if (!isset($cache[$table])) {
$cache[$table] = array();
}
// If fetching all and cached all, we've done so and we are finished.
if ($type == 'all' && !empty($cached_database[$table])) {
return $cache[$table];
}
$return = array();
// Don't load anything we've already cached.
if ($type == 'names' && !empty($args)) {
foreach ($args as $id => $name) {
if (isset($cache[$table][$name])) {
$return[$name] = $cache[$table][$name];
unset($args[$id]);
}
}
// If nothing left to load, return the result.
if (empty($args)) {
return $return;
}
}
// Build the query.
$query = db_select($table, 't__0')
->fields('t__0');
$alias_count = 1;
if (!empty($schema['join'])) {
foreach ($schema['join'] as $join_key => $join) {
if ($join_schema = drupal_get_schema($join['table'])) {
$query
->join($join['table'], 't__' . $alias_count, 't__0.' . $join['left_key'] . ' = ' . 't__' . $alias_count . '.' . $join['right_key']);
$query
->fields('t__' . $alias_count);
$alias_count++;
// Allow joining tables to alter the query through a callback.
if (isset($join['callback']) && function_exists($join['callback'])) {
$join['callback']($query, $schema, $join_schema);
}
}
}
}
$conditions = array();
$query_args = array();
// If they passed in names, add them to the query.
if ($type == 'names') {
$query
->condition($export['key'], $args, 'IN');
}
elseif ($type == 'conditions') {
foreach ($args as $key => $value) {
if (isset($schema['fields'][$key])) {
$query
->condition($key, $value);
}
}
}
$result = $query
->execute();
$status = variable_get($export['status'], array());
// Unpack the results of the query onto objects and cache them.
foreach ($result as $data) {
if (isset($schema['export']['object factory']) && function_exists($schema['export']['object factory'])) {
$object = $schema['export']['object factory']($schema, $data);
}
else {
$object = _ctools_export_unpack_object($schema, $data, $export['object']);
}
$object->table = $table;
$object->{$export['export type string']} = t('Normal');
$object->export_type = EXPORT_IN_DATABASE;
// Determine if default object is enabled or disabled.
if (isset($status[$object->{$export['key']}])) {
$object->disabled = $status[$object->{$export['key']}];
}
$cache[$table][$object->{$export['key']}] = $object;
if ($type == 'conditions') {
$return[$object->{$export['key']}] = $object;
}
}
// Load subrecords.
if (isset($export['subrecords callback']) && function_exists($export['subrecords callback'])) {
$export['subrecords callback']($cache[$table]);
}
if ($type == 'names' && !empty($args) && !empty($export['cache defaults'])) {
$defaults = _ctools_export_get_some_defaults($table, $export, $args);
}
else {
$defaults = _ctools_export_get_defaults($table, $export);
}
if ($defaults) {
foreach ($defaults as $object) {
if ($type == 'conditions') {
// If this does not match all of our conditions, skip it.
foreach ($args as $key => $value) {
if (!isset($object->{$key})) {
continue 2;
}
if (is_array($value)) {
if (!in_array($object->{$key}, $value)) {
continue 2;
}
}
elseif ($object->{$key} != $value) {
continue 2;
}
}
}
elseif ($type == 'names') {
if (!in_array($object->{$export['key']}, $args)) {
continue;
}
}
// Determine if default object is enabled or disabled.
if (isset($status[$object->{$export['key']}])) {
$object->disabled = $status[$object->{$export['key']}];
}
if (!empty($cache[$table][$object->{$export['key']}])) {
$cache[$table][$object->{$export['key']}]->{$export['export type string']} = t('Overridden');
$cache[$table][$object->{$export['key']}]->export_type |= EXPORT_IN_CODE;
$cache[$table][$object->{$export['key']}]->export_module = isset($object->export_module) ? $object->export_module : NULL;
if ($type == 'conditions') {
$return[$object->{$export['key']}] = $cache[$table][$object->{$export['key']}];
}
}
else {
$object->{$export['export type string']} = t('Default');
$object->export_type = EXPORT_IN_CODE;
$object->in_code_only = TRUE;
$object->table = $table;
$cache[$table][$object->{$export['key']}] = $object;
if ($type == 'conditions') {
$return[$object->{$export['key']}] = $object;
}
}
}
}
// If fetching all, we've done so and we are finished.
if ($type == 'all') {
$cached_database[$table] = TRUE;
return $cache[$table];
}
if ($type == 'names') {
foreach ($args as $name) {
if (isset($cache[$table][$name])) {
$return[$name] = $cache[$table][$name];
}
}
}
return $return;
}