Use dropdown in views exposed filter on text fields with entity not node? A very kind soul posted some code here for a small module that allows drop down fields in views for text fields on nodes

I was wondering how I can alter this so it works on an entity I created?

This is the particular field name on this example field_exact_location

My entity type is called "site_object"

function dd_city_form_views_exposed_form_alter(&amp;$form, &amp;$form_state) {
$field_id = 'field_exact_location';

// Only alter forms with the necessary field
if (isset($form[$field_id .'_value'])) {
// Build a query to get all node ids having the specified field
$query = new EntityFieldQuery();
$results = $query-&gt;entityCondition('entity_type', 'node')
                  -&gt;fieldCondition($field_id, 'value', 'NULL', '!=')
                  -&gt;fieldOrderBy($field_id, 'value', 'ASC')                    
                  -&gt;execute();

// Attach the field values to the nodes
$nodes = $results['node'];
field_attach_load('node', $nodes, FIELD_LOAD_CURRENT, array('field_id' =&gt; $field_id));

// Add a default so the filter is optional
$options = array('' =&gt; '&lt;select&gt;');

// Buld the options array based on the query results, overwriting duplicate entries
foreach($nodes as $nid =&gt; $node) {
  $value = $node-&gt;field_exact_location['und'][0]['value'];
  $options[$value] = $value;
}

// Alter the field
$form[$field_id .'_value']['#type'] = 'select';
$form[$field_id .'_value']['#options'] = $options;
$form[$field_id .'_value']['#size'] = 1;
}
}
Think I found a solution at last. It works in my local test environment for the exposed filters in views:



function dd_city_form_views_exposed_form_alter(&amp;$form, &amp;$form_state) {
  $field_id = 'field_selected_field';

  // Only alter forms with the necessary field
  if (isset($form[$field_id .'_value'])) {
    // Build a query to get all node ids having the specified field
    $query = new EntityFieldQuery();
    $results = $query-&gt;entityCondition('entity_type', 'model')
                      -&gt;fieldCondition($field_id, 'value', 'NULL', '!=')
                      -&gt;fieldOrderBy($field_id, 'value', 'ASC')                   
                      -&gt;execute();

    // Attach the field values to the nodes
    $entities = $results['model'];
    field_attach_load('model', $entities, FIELD_LOAD_CURRENT, array('field_id' =&gt; $field_id));

    // Add a default so the filter is optional
    $options = array('' =&gt; '&lt;select&gt;');

    // Buld the options array based on the query results, overwriting duplicate entries
    foreach($entities as $id =&gt; $entity) {
      $value = $entity-&gt;{$field_id}['und'][0]['value'];
      $options[$value] = $value;
   }

    // Alter the field
    $form[$field_id .'_value']['#type'] = 'select';
    $form[$field_id .'_value']['#options'] = $options;
    $form[$field_id .'_value']['#size'] = 1;
  }
}


I only had to change a tiny bit more, than I posted before.
Summary for the above code:


Rename 'field_selected_field' to your field. Example: If it is called 'city',
then rename it to 'field_city'.
Change 'model' to the name of your entity. In your case 'site_object'
These are the places: 


$results = $query->entityCondition('entity_type', 'model')
$entities = $results['model'];
field_attach_load('model', $entities, FIELD_LOAD_CURRENT, array('field_id' => $field_id));

Save the file to your module, upload it and enable it in Drupal.


A few screenshots of my test environment:

Here my example configuration for a view with an entity named 'model' with the field 'selected_field'.



If you go to the respective view (in my case /modeltestview), you get the following (the screenshot is only part of the screen). The left part is the starting part,you get the right part if you click on the dropdown list. To show there actually is something in the dropdown list.