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(&$queried_entities, $revision_id = FALSE)
{
foreach ($queried_entities as $key => $entity)
{
$queried_entities[$key]->foo = "bar";
}
parent::attachLoad($queried_entities, $revision_id);
}
}
And this is my hook_entity_info:
function contacts_entity_info() {
$return = array(
'contacts' => array(
'label' => t('Contact'),
'entity class' => 'Contacts',
'controller class' => 'ContactsController',
'views controller class' => 'ContactsViewsController',
'base table' => 'contacts',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'ckid',
'bundle' => array(),
),
'bundle keys' => array(),
'bundles' => array(),
'load hook' => 'contacts_load',
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'contacts',
'access callback' => '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' => t('Contacts Topics'),
'base' => array(
'field' => 'ckid',
'title' => t('Contacts Topics'),
),
'join' => array(
'contacts' => array(
'left_field' => 'ckid',
'field' => 'ckid',
),
),
);
$data['contacts_topics'] = array(
'ckid' => array(
'title' => 'Contact Entity ID',
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'relationship' => array(
'base' => 'contacts',
'base field' => 'ckid',
'handler' => 'views_handler_relationship',
'label' => t('Contact entity relationship'),
'title' => 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.