You are here

function mailchimp_campaign_entities_autocomplete_callback in Mailchimp 7.5

Same name and namespace in other branches
  1. 7.3 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_entities_autocomplete_callback()
  2. 7.4 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_entities_autocomplete_callback()

Callback for campaign template entity search field.

Parameters

string $type: The entity type to limit search to.

string $string: The string to search entity titles for.

Return value

array Entities with titles matching search string.

1 string reference to 'mailchimp_campaign_entities_autocomplete_callback'
mailchimp_campaign_menu in modules/mailchimp_campaign/mailchimp_campaign.module
Implements hook_menu().

File

modules/mailchimp_campaign/mailchimp_campaign.module, line 154
Module file for mailchimp_campaign.

Code

function mailchimp_campaign_entities_autocomplete_callback($type, $string = '') {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', $type)
    ->propertyCondition('title', $string, 'CONTAINS')
    ->range(0, 10);
  $entities = array();
  $result = $query
    ->execute();
  if (isset($result[$type])) {
    $entity_ids = array_keys($result[$type]);
    $entities_data = entity_load($type, $entity_ids);
    if (!empty($entities_data)) {
      foreach ($entities_data as $id => $entity) {
        $entities[$entity->title . ' [' . $id . ']'] = $entity->title;
      }
    }
  }
  drupal_json_output($entities);
}