How can you add a multiple value field to an entity? Edit: My question is similar to How to create multi-value, multi-bundle entity reference fields in code?, except the field doesn't need to reference multiple bundles.

We have an existing view of custom entities.  The view needs to now be filtered based on some related fields that are not part of the entity (or any entity).  Kind of like adding a Taxonomy Reference field to a Content Type.  What is the best way to add those fields to the entity?

I see that many modules use attachLoad to attach fields to an entity, but when I try my implementation those fields don't show up in my list of Fields or Filters in Views.

This is a screenshot of the checkboxes field:



This is my controller:

class ContactsController extends EntityAPIController {

  public function attachLoad(&amp;$queried_entities, $revision_id = FALSE)
  {
    foreach ($queried_entities as $key =&gt; $entity)
    {
      $queried_entities[$key]-&gt;foo = "bar";
    }
    parent::attachLoad($queried_entities, $revision_id);
  }

}


And this is my hook_entity_info:

function contacts_entity_info() {
  $return = array(
    'contacts' =&gt; array(
      'label' =&gt; t('Contact'),
      'entity class' =&gt; 'Contacts',
      'controller class' =&gt; 'ContactsController',
      'views controller class' =&gt; 'ContactsViewsController',
      'base table' =&gt; 'contacts',
      'fieldable' =&gt; TRUE,
      'entity keys' =&gt; array(
        'id' =&gt; 'ckid',
        'bundle' =&gt; array(),
      ),
      'bundle keys' =&gt; array(),
      'bundles' =&gt; array(),
      'load hook' =&gt; 'contacts_load',
      'view modes' =&gt; array(
        'full' =&gt; array(
          'label' =&gt; t('Default'),
          'custom settings' =&gt; FALSE,
        ),
      ),
      'label callback' =&gt; 'entity_class_label',
      'uri callback' =&gt; 'entity_class_uri',
      'module' =&gt; 'contacts',
      'access callback' =&gt; 'contacts_access',
    ),
  );

  return $return;
}


This is a suggested implementation of EntityDefaultViewsController:

/**
* EntityTest Views Controller class.
*/
class ContactsViewsController extends EntityDefaultViewsController {

  /**
   * Add extra fields to views_data().
   */
  public function views_data() {
    $data = parent::views_data();

    // Add your custom data here
    $data['contacts_topics']['table'] = array(
      'group' =&gt; t('Contacts Topics'),
      'base' =&gt; array(
        'field' =&gt; 'ckid',
        'title' =&gt; t('Contacts Topics'),
      ),
      'join' =&gt; array(
        'contacts' =&gt; array(
          'left_field' =&gt; 'ckid',
          'field' =&gt; 'ckid',
        ),
      ),
    );

    $data['contacts_topics'] = array(
      'ckid' =&gt; array(
        'title' =&gt; 'Contact Entity ID',
        'field' =&gt; array(
          'handler' =&gt; 'views_handler_field_numeric',
          'click sortable' =&gt; TRUE,
        ),
        'sort' =&gt; array(
          'handler' =&gt; 'views_handler_sort',
        ),
        'filter' =&gt; array(
          'handler' =&gt; 'views_handler_filter_numeric',
        ),
        'relationship' =&gt; array(
          'base' =&gt; 'contacts',
          'base field' =&gt; 'ckid',
          'handler' =&gt; 'views_handler_relationship',
          'label' =&gt; t('Contact entity relationship'),
          'title' =&gt; t('Conact entity relationship'),
        ),
      ),
    );

    return $data;
  }
}


Unfortunately I don't see anything in the Views SQL that shows that any of these attempts to add the fields or join the topics table to the contacts table are working.
How to attach a field to an entity programically solved this problem for me.  It's not an exact duplicate, but the answer and the comments pointed me to the right direction.

So this code creates the field and the instance and it attaches it to the entity very nicely:

function contacts_enable()
{
  if (!field_info_field('related_topics'))
  {
    $field = array(
      'field_name' =&gt; 'related_topics',
      'type' =&gt; 'list_text',
      'cardinality' =&gt; -1,
      'settings' =&gt; array(
        'allowed_values_function' =&gt; '_contacts_get_related_topics',
      ),
    );

    field_create_field($field);

    $instance = array(
      'field_name' =&gt; 'related_topics',
      'entity_type' =&gt; 'contacts',
      'label' =&gt; 'Related Topics',
      'bundle' =&gt; 'contacts',
      'default_value' =&gt; NULL,
      'settings' =&gt; array(
        'user_register_form' =&gt; FALSE,
      ),
      'widget' =&gt; array(
        'active' =&gt; 1,
        'module' =&gt; 'options',
        'settings' =&gt; array(),
        'type' =&gt; 'options_buttons',
        'weight' =&gt; -1,
      ),
    );

    field_create_instance($instance);
  }
}