function itoggle_get_entity_info in iToggle 7.2
Same name and namespace in other branches
- 7 includes/itoggle.inc \itoggle_get_entity_info()
Loads information about defined entities and returns an array of possible boolean fields for toggling.
Parameters
$reset: A boolean indicating whether to bypass the cache.
Return value
array An array of available entity information.
3 calls to itoggle_get_entity_info()
- itoggle_views_views_data_alter in modules/
views/ itoggle_views.module - Implements hook_views_data_alter().
- _itoggle_ajax_toggle_entity in ./
itoggle.pages.inc - Change an entity property's value.
- _itoggle_ajax_toggle_field in ./
itoggle.pages.inc - Change an entity field's value.
File
- ./
itoggle.module, line 293 - iToggle core module.
Code
function itoggle_get_entity_info($reset = FALSE) {
// Use the advanced drupal_static() pattern, since this is called often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['itoggle_get_entity_info'] =& drupal_static(__FUNCTION__);
}
$allowed =& $drupal_static_fast['itoggle_get_entity_info'];
if ($reset || !is_array($allowed)) {
$cache = cache_get('itoggle_get_entity_info');
if (!$reset && isset($cache->data)) {
$allowed = $cache->data;
}
else {
$entities = entity_get_info();
$allowed = array();
foreach ($entities as $type => $info) {
$keys = array();
// Get table schema.
$schema = drupal_get_schema($info['base table']);
// Get entity metadata wrapper.
$wrapper = entity_metadata_wrapper($type);
foreach ($schema['fields'] as $cid => $val) {
// Boolean fields get stored as integers.
if ($val['type'] === 'int') {
// Avoid fields we know for a fact aren't boolean.
$avoid = array(
'created',
'changed',
'filesize',
'timestamp',
'translate',
'weight',
);
// Avoid more specific fields we know aren't boolean.
if ($type === 'user') {
$avoid[] = 'access';
$avoid[] = 'login';
$avoid[] = 'picture';
}
else {
if ($type === 'node') {
$avoid[] = 'comment';
}
else {
if ($type === 'taxonomy_vocabulary') {
$avoid[] = 'hierarchy';
}
}
}
// Proceed if field isn't in avoid list.
if (!in_array($cid, $avoid)) {
// Find field length and whether it contains the string 'id'.
$pos = strpos($cid, 'id');
$len = strlen($cid);
// Avoid fields ending in 'id'.
if ($pos === FALSE || $len - $pos > 2) {
// Try to load property info from entity metadata wrapper.
try {
$property = $wrapper
->getPropertyInfo($cid);
// If we have a label, use it.
if (isset($property['label'])) {
$label = $property['label'];
}
else {
$label = drupal_ucfirst($cid);
}
// Boolean properties = win.
if ($property['type'] !== 'boolean') {
// If not boolean, check whether it has an options list.
if (isset($property['options list']) && is_callable($property['options list'])) {
if (is_array($property['options list']) && $property['options list'][1] === 'bundleOptionsList') {
$options_list = array();
}
else {
// If options list has 2 entries, consider it a boolean.
$options_list = call_user_func($property['options list']);
}
if (!isset($options_list) || count($options_list) !== 2) {
// No go, skip to next field.
continue;
}
}
else {
// No go, skip to next field.
continue;
}
}
$keys[$cid] = $label;
} catch (EntityMetadataWrapperException $e) {
// If we can't use property info, take a guess.
$keys[$cid] = drupal_ucfirst($cid);
}
}
}
}
}
if (!empty($keys)) {
$allowed[$type] = array(
'properties' => $keys,
'base table' => $info['base table'],
'entity keys' => $info['entity keys'],
'label' => $info['label'],
);
if (isset($info['access callback'])) {
$allowed[$type]['access callback'] = $info['access callback'];
}
}
}
cache_set('itoggle_get_entity_info', $allowed, 'cache', CACHE_TEMPORARY);
}
}
return $allowed;
}