You are here

function _autocomplete_widgets_get_options_flddata in Autocomplete Widgets for Text and Number Fields 7

Same name and namespace in other branches
  1. 6 autocomplete_widgets.common.inc \_autocomplete_widgets_get_options_flddata()

Fetch an array of options for the given widget (field data).

Options are retrieved from existing values for the field.

1 call to _autocomplete_widgets_get_options_flddata()
_autocomplete_widgets_get_options in ./autocomplete_widgets.common.inc
Fetch an array of options for the given widget.

File

./autocomplete_widgets.common.inc, line 121
Common functions for Autocomplete Widgets module.

Code

function _autocomplete_widgets_get_options_flddata($instance, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
  $table = 'field_data_' . $instance['field_name'];
  $column = $instance['field_name'] . '_value';
  $order = isset($instance['widget']['settings']['order']) ? $instance['widget']['settings']['order'] : '';
  $case_sensitive = !empty($instance['widget']['settings']['autocomplete_case']);
  $select = db_select('node', 'n');
  if (!empty($instance['widget']['settings']['obey_access_controls'])) {

    // Add entity_field_access so that node permission are respected.
    $select
      ->addTag('node_access');
    if (!user_access('bypass node access')) {

      // If the user is able to view their own unpublished nodes, allow them
      // to see these in addition to published nodes. Check that they actually
      // have some unpublished nodes to view before adding the condition.
      if (user_access('view own unpublished content') && ($own_unpublished = db_query('SELECT nid FROM {node} WHERE uid = :uid AND status = :status', array(
        ':uid' => $GLOBALS['user']->uid,
        ':status' => NODE_NOT_PUBLISHED,
      ))
        ->fetchCol())) {
        $select
          ->condition(db_or()
          ->condition('n.status', NODE_PUBLISHED)
          ->condition('n.nid', $own_unpublished, 'IN'));
      }
      else {

        // If not, restrict the query to published nodes.
        $select
          ->condition('n.status', NODE_PUBLISHED);
      }
    }
  }
  $select
    ->join($table, 'fd', 'revision_id = n.vid');
  $select
    ->addField('fd', $column);
  if ($string !== '') {
    switch ($match) {
      case 'equals':
        $select
          ->condition($column, $string);
        break;
      case 'starts_with':
        $select
          ->condition($column, $string . '%', 'LIKE');
        break;
      case 'contains':
      default:
        $select
          ->condition($column, '%' . $string . '%', 'LIKE');
        break;
    }
  }
  elseif (isset($keys) && is_array($keys)) {
    $select
      ->condition($column, $keys, 'IN');
  }
  if (!empty($limit)) {
    $select
      ->range(0, $limit);
  }
  if (!empty($order)) {
    $select
      ->orderBy($column, $order);
  }
  $rows = $select
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);
  $options = array();
  foreach ($rows as $row) {

    // MySQL does not do case sensitive text comparisons with Drupal's default
    // colation (utf8_general_ci) so we deal with it here after the fact.
    if (!$case_sensitive || $case_sensitive && strpos($row[$column], $string) !== FALSE) {
      $options[$row[$column]] = $row[$column];
    }
  }
  return $options;
}