Hi, I have installed last dev version, all seems ok, but when I switch "Entity selection mode" from Simple to view (and viceversa) I receive this error in my error_log:

PHP Fatal error: Class EntityReference_SelectionHandler_Views contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (EntityReference_SelectionHandler::validateAutocompleteInput) in /home/webmaster/htdocs/mysite/sites/all/modules/entityreference/plugins/selection/EntityReference_SelectionHandler_Views.class.php on line 150, referer: http://XXXX.XXX/admin/structure/types/manage/macchina-asta/fields/field_...

Thanks.

M.

Comments

chertzog’s picture

Title: PHP Fatal error » Using a view to select reference-able entities fails

Yeah, Using a view to select the reference-able entities doesnt work (at the moment). This is because if you use a view to select the entities, you could allow multiple types of entities and because this module requires that you set up a 1 to 1 relationship between two fields, there is no way to guarantee that with a view.

This is something that i would like to get working, but at the moment I have no idea how to handle.

Unlike CNR where it only works with node, you can find out the bundle of the node with just the ID, with entities you can have multiple entities with the same ID number, they just have different entity types/bundles.

Patches are welcome....

phenaproxima’s picture

I'm writing a quick patch for this, as mentioned in http://drupal.org/node/1923566.

For now, said patch will simply treat Views-based entity reference fields as black boxes, and not filter by entity type or bundle. But perhaps that could be made more robust later by checking the base table of the view, or something like that. Another approach could be to override Entity Reference's display plugin and replace it with a version that provides additional methods for getting the available entity type(s) and bundle(s), which could in turn be called by CER.

phenaproxima’s picture

phenaproxima’s picture

Regarding this issue, and a more robust solution - here is a bit of non-functional proof-of-concept code I wrote to illustrate my idea in #2, and hopefully present a possible solution to the problem, if only in a very basic way:

/**
 * Checks if a field can reference entities of a certain type and bundle, either
 * directly or through Views.
 *
 * @param $field
 *  Info array for an entity reference field, returned by field_info_field().
 * @param $entity_type
 *  The entity type to test against.
 * @param $bundle
 *  Optional bundle to test against.
 *
 * @return boolean
 */
function _cer_field_can_reference($field, $entity_type, $bundle = NULL) {
  if ($field['settings']['handler'] == 'views') {
    $view = views_get_view($field['settings']['handler_settings']['view']['view_name']);
    $view->set_display($field['settings']['handler_settings']['view']['display_name']);

    $entity_info = entity_get_info($entity_type);

    if ($view->base_table != $entity_info['base table']) {
      return FALSE;
    }
    elseif (isset($bundle)) {
      $bundle_key = $entity_info['entity keys']['bundle'];
      $filters = $view->display_handler->get_option('filters');

      return isset($filters[$bundle_key]) ? in_array($bundle, $filters[$bundle_key]['value']) : TRUE;
    }
    else {
      return TRUE;
    }
  }
  else {
    if ($field['settings']['target_type'] != $entity_type) {
      return FALSE;
    }
    elseif (isset($bundle) && !in_array($field['settings']['handler_settings']['target_bundles'])) {
      return FALSE;
    }
    else {
      return TRUE;
    }
  }
}
phenaproxima’s picture

New patch submitted (and this one isn't a quick let's-hope-this-works hack job): http://drupal.org/node/1923566#comment-7235840

phenaproxima’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Active » Needs review

Fixed in the 2.x branch (git only, at the time of this writing).

chertzog’s picture

Status: Needs review » Closed (fixed)

7.x-1.x is deprecated.