You are here

function LinkitPluginEntity::autocomplete_callback in Linkit 7.2

The autocomplete callback function for the Linkit Entity plugin.

Return value

An associative array whose values are an associative array containing:

  • title: A string to use as the search result label.
  • description: (optional) A string with additional information about the result item.
  • path: The URL to the item.
  • group: (optional) A string with the group name for the result item. Best practice is to use the plugin name as group name.
  • addClass: (optional) A string with classes to add to the result row.

Overrides LinkitPluginInterface::autocomplete_callback

1 call to LinkitPluginEntity::autocomplete_callback()
LinkitPluginTaxonomy_Term::autocomplete_callback in plugins/linkit_plugins/linkit-plugin-taxonomy_term.class.php
The autocomplete callback function for the Linkit Taxonomy term plugin.
1 method overrides LinkitPluginEntity::autocomplete_callback()
LinkitPluginTaxonomy_Term::autocomplete_callback in plugins/linkit_plugins/linkit-plugin-taxonomy_term.class.php
The autocomplete callback function for the Linkit Taxonomy term plugin.

File

plugins/linkit_plugins/linkit-plugin-entity.class.php, line 204
Define Linkit entity plugin.

Class

LinkitPluginEntity
@file Define Linkit entity plugin.

Code

function autocomplete_callback() {
  $matches = array();

  // Get the EntityFieldQuery instance.
  $this
    ->getQueryInstance();

  // Add the search condition to the query object.
  $this->query
    ->propertyCondition($this->entity_field_label, '%' . db_like($this->search_string) . '%', 'LIKE')
    ->addTag('linkit_entity_autocomplete')
    ->addTag('linkit_' . $this->plugin['entity_type'] . '_autocomplete');

  // Add access tag for the query.
  // There is also a runtime access check that uses entity_access().
  $this->query
    ->addTag($this->plugin['entity_type'] . '_access');

  // Bundle check.
  if (isset($this->entity_key_bundle) && isset($this->conf['bundles'])) {
    if ($bundles = array_filter($this->conf['bundles'])) {
      $this->query
        ->propertyCondition($this->entity_key_bundle, $bundles, 'IN');
    }
  }

  // Execute the query.
  $result = $this->query
    ->execute();
  if (!isset($result[$this->plugin['entity_type']])) {
    return array();
  }
  $ids = array_keys($result[$this->plugin['entity_type']]);

  // Load all the entities with all the ids we got.
  $entities = entity_load($this->plugin['entity_type'], $ids);
  foreach ($entities as $entity) {

    // Check the access againt the definded entity access callback.
    if (entity_access('view', $this->plugin['entity_type'], $entity) === FALSE) {
      continue;
    }
    $matches[] = array(
      'title' => $this
        ->buildLabel($entity),
      'description' => $this
        ->buildDescription($entity),
      'path' => $this
        ->buildPath($entity),
      'group' => $this
        ->buildGroup($entity),
      'addClass' => $this
        ->buildRowClass($entity),
    );
  }
  return $matches;
}