You are here

function itoggle_get_entity_info in iToggle 7

Same name and namespace in other branches
  1. 7.2 itoggle.module \itoggle_get_entity_info()

Loads information about defined entities and returns an array of possible boolean fields for toggling

Return value

array

3 calls to itoggle_get_entity_info()
itoggle_field_field_formatter_view in modules/field/itoggle_field.module
Implements hook_field_formatter_view().
itoggle_views_handler_field::init in modules/views/itoggle_views_handler.inc
Init the handler with necessary data.
itoggle_views_views_data in modules/views/itoggle_views.module
Implements hook_views_data().

File

includes/itoggle.inc, line 133
iToggle module include.

Code

function itoggle_get_entity_info() {

  // Use the advanced drupal_static() pattern, since this is called very 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 (!is_array($allowed)) {
    $cache = cache_get('itoggle_get_entity_info');
    if (isset($cache->data)) {
      $allowed = $cache->data;
    }
    else {
      $entities = entity_get_info();
      $allowed = array();
      foreach ($entities as $key => $info) {
        $keys = array();
        $schema = drupal_get_schema($info['base table']);
        foreach ($schema['fields'] as $cid => $val) {
          if ($val['type'] === 'int') {

            // avoid fields we know aren't boolean
            $avoid = array(
              'created',
              'changed',
              'filesize',
              'timestamp',
              'translate',
              'weight',
            );
            if ($key === 'user') {
              $avoid[] = 'access';
              $avoid[] = 'login';
              $avoid[] = 'picture';
            }
            else {
              if ($key === 'taxonomy_vocabulary') {
                $avoid[] = 'hierarchy';
              }
            }
            if (!in_array($cid, $avoid)) {

              // avoid keys ending in 'id'
              $pos = strpos($cid, 'id');
              $len = strlen($cid);
              if ($pos === FALSE || $len - $pos > 2) {
                $keys[] = $cid;
              }
            }
          }
        }
        if (!empty($keys)) {
          $allowed[$key] = array(
            'properties' => $keys,
            'base table' => $info['base table'],
            'entity keys' => $info['entity keys'],
          );
        }
      }

      // ok we have allowed entities
      // we still need to know for sure whether they are boolean or not
      // @TODO more entity introspection?
      cache_set('itoggle_get_entity_info', $allowed, 'cache', CACHE_TEMPORARY);
    }
  }
  return $allowed;
}