function _ctools_getReferencableEntities

Private function to get referencable entities. Based on code from the Entity Reference module.

1 call to _ctools_getReferencableEntities()
ctools_content_autocomplete_entity in includes/content.menu.inc
Helper function for autocompletion of entity titles.

File

includes/content.menu.inc, line 123

Code

function _ctools_getReferencableEntities($entity_type, $entity_info, $match = NULL, $match_operator = 'LIKE', $limit = 0) {
    global $user;
    $account = $user;
    $options = array();
    // We're an entity ID, return the id.
    if (is_numeric($match) && $match_operator == '=') {
        if ($entity = array_shift(entity_load($entity_type, array(
            $match,
        )))) {
            if (isset($entity_info['access callback']) && function_exists($entity_info['access callback'])) {
                if ($entity_info['access callback']('view', $entity, $account, $entity_type)) {
                    $label = entity_label($entity_type, $entity);
                    return array(
                        $match => array(
                            'label' => !empty($label) ? $label : $entity->{$entity_info['entity keys']['id']},
                            'bundle' => !empty($entity_info['entity keys']['bundle']) ? check_plain($entity->{$entity_info['entity keys']['bundle']}) : NULL,
                        ),
                    );
                }
            }
        }
        // If you don't have access, or an access callback or a valid entity, just
        // Return back the Entity ID.
        return array(
            $match => array(
                'label' => $match,
                'bundle' => NULL,
            ),
        );
    }
    // We have matches, build a query to fetch the result.
    if ($query = _ctools_buildQuery($entity_type, $entity_info, $match, $match_operator)) {
        if ($limit > 0) {
            $query->range(0, $limit);
        }
        $results = $query->execute();
        if (!empty($results)) {
            foreach ($results as $record) {
                $entities = entity_load($entity_type, array(
                    $record->{$entity_info['entity keys']['id']},
                ));
                $entity = array_shift($entities);
                if (isset($entity_info['access callback']) && function_exists($entity_info['access callback'])) {
                    if ($entity_info['access callback']('view', $entity, $account, $entity_type)) {
                        $label = entity_label($entity_type, $entity);
                        $options[$record->{$entity_info['entity keys']['id']}] = array(
                            'label' => !empty($label) ? $label : $entity->{$entity_info['entity keys']['id']},
                            'bundle' => !empty($entity_info['entity keys']['bundle']) ? check_plain($entity->{$entity_info['entity keys']['bundle']}) : NULL,
                        );
                    }
                }
            }
        }
        return $options;
    }
    return array();
}