entity_reference.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @file
  4. * Contains the CER plugin for Entity Reference fields.
  5. */
  6. class CerEntityReferenceField extends CerField {
  7. /**
  8. * Implements CerField::getTargetType().
  9. */
  10. public function getTargetType() {
  11. return $this->settings['target_type'];
  12. }
  13. /**
  14. * @override CerField::getTargetBundles().
  15. */
  16. public function getTargetBundles() {
  17. $bundles = array();
  18. // If the reference field is using a view, load the view and see if it's filtering by the entity
  19. // type's bundle filter. If it is, the filter values are the target bundles. Otherwise,
  20. // assume that all bundles can be referenced.
  21. //
  22. // @todo Support contextual filters?
  23. //
  24. // NOTE: Selection handlers (i.e., $field['settings']['handler']) are plugins owned by
  25. // Entity Reference. There is no method defined to get an array of referenceable
  26. // bundles, but hopefully, if CER gains enough traction in the community, such a
  27. // method can be added to the EntityReference_SelectionHandler interface. This
  28. // function could then be deprecated, which would be a more flexible, future-proof
  29. // method of finding a field's target bundles.
  30. //
  31. if ($this->settings['handler'] == 'views') {
  32. $view = views_get_view($this->settings['handler_settings']['view']['view_name']);
  33. $view->set_display($this->settings['handler_settings']['view']['display_name']);
  34. $info = entity_get_info($this->getTargetType());
  35. if (isset($info['entity keys']['bundle'])) {
  36. $handler = $view->display_handler->get_handler('filter', $info['entity keys']['bundle']);
  37. if ($handler) {
  38. $bundles = $handler->value;
  39. }
  40. }
  41. }
  42. else {
  43. $bundles = $this->settings['handler_settings']['target_bundles'];
  44. }
  45. return ($bundles ? $bundles : parent::getTargetBundles());
  46. }
  47. }