You are here

function _autocomplete_widgets_get_options_node_reference in Autocomplete Widgets for Text and Number Fields 7

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

Options are retrieved from the titles of the allowed node types.

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

File

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

Code

function _autocomplete_widgets_get_options_node_reference($instance, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
  $field_name = $instance['field_name'];
  $table = 'field_data_' . $field_name;
  $column = $field_name . '_value';
  $options = array();
  $case_sensitive = !empty($instance['widget']['settings']['autocomplete_case']);
  $order = isset($instance['widget']['settings']['order']) ? $instance['widget']['settings']['order'] : '';
  $field_query = db_select($table, 'fd')
    ->fields('fd', array(
    $column,
  ));
  $node_title_query = db_select('node', 'n')
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', $instance['widget']['settings']['allowed_node_types'], 'IN')
    ->addTag('node_access');
  $field_query = db_select('node', 'n');
  if (!empty($instance['widget']['settings']['obey_access_controls'])) {

    // Add entity_field_access so that node permission are respected.
    $field_query
      ->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())) {
        $field_query
          ->condition(db_or()
          ->condition('n.status', NODE_PUBLISHED)
          ->condition('n.nid', $own_unpublished, 'IN'));
        $node_title_query
          ->condition(db_or()
          ->condition('n.status', NODE_PUBLISHED)
          ->condition('n.nid', $own_unpublished, 'IN'));
      }
      else {

        // If not, restrict the query to published nodes.
        $field_query
          ->condition('n.status', NODE_PUBLISHED);
        $node_title_query
          ->condition('n.status', NODE_PUBLISHED);
      }
    }
  }
  $field_query
    ->join($table, 'fd', 'revision_id = n.vid');
  $field_query
    ->addField('fd', $column);
  if (!empty($order)) {
    $field_query
      ->orderBy($column, $order);
    $node_title_query
      ->orderBy('title', $order);
  }
  if ($string !== '') {
    switch ($match) {
      case 'starts_with':
        $field_query
          ->condition($column, $string . '%', 'LIKE');
        $node_title_query
          ->condition('n.title', $string . '%', 'LIKE');
        break;
      case 'contains':
      default:
        $field_query
          ->condition($column, '%' . $string . '%', 'LIKE');
        $node_title_query
          ->condition('n.title', '%' . $string . '%', 'LIKE');
        break;
    }

    // @todo: can these fetch all's be replaced with fetchAssoc?
    // 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.
    $rows = $node_title_query
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);
    foreach ($rows as $row) {
      if (!$case_sensitive || $case_sensitive && strpos($row['title'], $string) !== FALSE) {
        $options[$row['title']] = $row['title'];
      }
    }
    $rows = $field_query
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);
    foreach ($rows as $row) {
      if (!$case_sensitive || $case_sensitive && strpos($row[$column], $string) !== FALSE) {
        $options[$row[$column]] = $row[$column];
      }
    }
  }

  // @todo: limit should be accounted for here...
  return $options;
}