abstract.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @file
  4. * Abstraction of the selection logic of an entity reference field.
  5. *
  6. * Implementations that wish to provide an implementation of this should
  7. * register it using CTools' plugin system.
  8. */
  9. interface EntityReference_SelectionHandler {
  10. /**
  11. * Factory function: create a new instance of this handler for a given field.
  12. *
  13. * @param $field
  14. * A field datastructure.
  15. * @return EntityReferenceHandler
  16. */
  17. public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL);
  18. /**
  19. * Return a list of referencable entities.
  20. *
  21. * @return
  22. * A nested array of entities, the first level is keyed by the
  23. * entity bundle, which contains an array of entity labels (safe HTML),
  24. * keyed by the entity ID.
  25. */
  26. public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0);
  27. /**
  28. * Count entities that are referencable by a given field.
  29. */
  30. public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS');
  31. /**
  32. * Validate that entities can be referenced by this field.
  33. *
  34. * @return
  35. * An array of entity ids that are valid.
  36. */
  37. public function validateReferencableEntities(array $ids);
  38. /**
  39. * Validate Input from autocomplete widget that has no Id.
  40. *
  41. * @see _entityreference_autocomplete_validate()
  42. *
  43. * @param $input
  44. * Single string from autocomplete widget.
  45. * @param $element
  46. * The form element to set a form error.
  47. * @return
  48. * Value of a matching entity id, or NULL if none.
  49. */
  50. public function validateAutocompleteInput($input, &$element, &$form_state, $form);
  51. /**
  52. * Give the handler a chance to alter the SelectQuery generated by EntityFieldQuery.
  53. */
  54. public function entityFieldQueryAlter(SelectQueryInterface $query);
  55. /**
  56. * Return the label of a given entity.
  57. */
  58. public function getLabel($entity);
  59. /**
  60. * Generate a settings form for this handler.
  61. */
  62. public static function settingsForm($field, $instance);
  63. }
  64. /**
  65. * A null implementation of EntityReference_SelectionHandler.
  66. */
  67. class EntityReference_SelectionHandler_Broken implements EntityReference_SelectionHandler {
  68. public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
  69. return new EntityReference_SelectionHandler_Broken($field, $instance, $entity_type, $entity);
  70. }
  71. protected function __construct($field, $instance) {
  72. $this->field = $field;
  73. $this->instance = $instance;
  74. }
  75. public static function settingsForm($field, $instance) {
  76. $form['selection_handler'] = array(
  77. '#markup' => t('The selected selection handler is broken.'),
  78. );
  79. return $form;
  80. }
  81. public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
  82. return array();
  83. }
  84. public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
  85. return 0;
  86. }
  87. public function validateReferencableEntities(array $ids) {
  88. return array();
  89. }
  90. public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
  91. return NULL;
  92. }
  93. public function entityFieldQueryAlter(SelectQueryInterface $query) {}
  94. public function getLabel($entity) {
  95. return '';
  96. }
  97. }