function ctools_content_autocomplete_entity

Helper function for autocompletion of entity titles.

1 string reference to 'ctools_content_autocomplete_entity'
ctools_content_menu in includes/content.menu.inc
@file Contains menu item registration for the content tool.

File

includes/content.menu.inc, line 26

Code

function ctools_content_autocomplete_entity($entity_type, $string = '') {
    if ($string != '') {
        $entity_info = entity_get_info($entity_type);
        if (!module_exists('entity')) {
            module_load_include('inc', 'ctools', 'includes/entity-access');
            _ctools_entity_access($entity_info, $entity_type);
        }
        // We must query all ids, because if every one of the 10 don't have access
        // the user may never be able to autocomplete a node title.
        $preg_matches = array();
        $matches = array();
        $match = preg_match('/\\[id: (\\d+)\\]/', $string, $preg_matches);
        if (!$match) {
            $match = preg_match('/^id: (\\d+)/', $string, $preg_matches);
        }
        // If an ID match was found, use that ID rather than the whole string.
        if ($match) {
            $entity_id = $preg_matches[1];
            $results = _ctools_getReferencableEntities($entity_type, $entity_info, $entity_id, '=', 1);
        }
        else {
            // We cannot find results if the entity doesn't have a label to search.
            if (!isset($entity_info['entity keys']['label'])) {
                drupal_json_output(array(
                    "[id: NULL]" => '<span class="autocomplete_title">' . t('Entity Type !entity_type does not support autocomplete search.', array(
                        '!entity_type' => $entity_type,
                    )) . '</span>',
                ));
                return;
            }
            $results = _ctools_getReferencableEntities($entity_type, $entity_info, $string, 'LIKE', 10);
        }
        foreach ($results as $entity_id => $result) {
            $matches[$result['label'] . " [id: {$entity_id}]"] = '<span class="autocomplete_title">' . check_plain($result['label']) . '</span>';
            $matches[$result['label'] . " [id: {$entity_id}]"] .= isset($result['bundle']) ? ' <span class="autocomplete_bundle">(' . check_plain($result['bundle']) . ')</span>' : '';
        }
        drupal_json_output($matches);
    }
}