abstract.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * An array of referencable entities, which keys are entity ids and
  23. * values (safe HTML) labels to be displayed to the user.
  24. */
  25. public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0);
  26. /**
  27. * Count entities that are referencable by a given field.
  28. */
  29. public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS');
  30. /**
  31. * Validate that entities can be referenced by this field.
  32. *
  33. * @return
  34. * An array of entity ids that are valid.
  35. */
  36. public function validateReferencableEntities(array $ids);
  37. /**
  38. * Validate Input from autocomplete widget that has no Id.
  39. *
  40. * @see _entityreference_autocomplete_validate()
  41. *
  42. * @param $input
  43. * Single string from autocomplete widget.
  44. * @param $element
  45. * The form element to set a form error.
  46. * @return
  47. * Value of a matching entity id, or NULL if none.
  48. */
  49. public function validateAutocompleteInput($input, &$element, &$form_state, $form);
  50. /**
  51. * Give the handler a chance to alter the SelectQuery generated by EntityFieldQuery.
  52. */
  53. public function entityFieldQueryAlter(SelectQueryInterface $query);
  54. /**
  55. * Return the label of a given entity.
  56. */
  57. public function getLabel($entity);
  58. /**
  59. * Generate a settings form for this handler.
  60. */
  61. public static function settingsForm($field, $instance);
  62. }
  63. /**
  64. * A null implementation of EntityReference_SelectionHandler.
  65. */
  66. class EntityReference_SelectionHandler_Broken implements EntityReference_SelectionHandler {
  67. public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
  68. return new EntityReference_SelectionHandler_Broken($field, $instance, $entity_type, $entity);
  69. }
  70. protected function __construct($field, $instance) {
  71. $this->field = $field;
  72. $this->instance = $instance;
  73. }
  74. public static function settingsForm($field, $instance) {
  75. $form['selection_handler'] = array(
  76. '#markup' => t('The selected selection handler is broken.'),
  77. );
  78. return $form;
  79. }
  80. public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
  81. return array();
  82. }
  83. public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
  84. return 0;
  85. }
  86. public function validateReferencableEntities(array $ids) {
  87. return array();
  88. }
  89. public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
  90. return NULL;
  91. }
  92. public function entityFieldQueryAlter(SelectQueryInterface $query) {}
  93. public function getLabel($entity) {
  94. return '';
  95. }
  96. }