entity_views_handler_field_field.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @file
  4. * Contains the entity_views_handler_field_field class.
  5. */
  6. /**
  7. * A handler to provide proper displays for Field API fields.
  8. *
  9. * Overrides the default Views handler to retrieve the data from an entity via
  10. * data selection.
  11. *
  12. * This handler may only be used in conjunction with data selection based Views
  13. * tables or other base tables using a query plugin that supports data
  14. * selection.
  15. *
  16. * @see entity_views_field_definition()
  17. * @ingroup views_field_handlers
  18. */
  19. class entity_views_handler_field_field extends views_handler_field_field {
  20. /**
  21. * Stores the entity type of the result entities.
  22. */
  23. public $entity_type;
  24. /**
  25. * Stores the result entities' metadata wrappers.
  26. */
  27. public $wrappers = array();
  28. /**
  29. * The entity for which this field is currently rendered.
  30. */
  31. public $entity;
  32. /**
  33. * Return TRUE if the user has access to view this field.
  34. */
  35. public function access() {
  36. return field_access('view', $this->field_info, $this->definition['entity type']);
  37. }
  38. /**
  39. * Overridden to add the field for the entity ID (if necessary).
  40. */
  41. public function query($use_groupby = FALSE) {
  42. EntityFieldHandlerHelper::query($this);
  43. }
  44. /**
  45. * Adds a click-sort to the query.
  46. */
  47. public function click_sort($order) {
  48. EntityFieldHandlerHelper::click_sort($this, $order);
  49. }
  50. /**
  51. * Override so it doesn't do any harm (or, anything at all).
  52. */
  53. public function post_execute(&$values) { }
  54. /**
  55. * Load the entities for all rows that are about to be displayed.
  56. */
  57. public function pre_render(&$values) {
  58. parent::pre_render($values);
  59. EntityFieldHandlerHelper::pre_render($this, $values, TRUE);
  60. }
  61. /**
  62. * Overridden to get the items our way.
  63. */
  64. public function get_items($values) {
  65. $items = array();
  66. // Set the entity type for the parent handler.
  67. $values->_field_data[$this->field_alias]['entity_type'] = $this->entity_type;
  68. // We need special handling for lists of entities as the base.
  69. $entities = EntityFieldHandlerHelper::get_value($this, $values, 'entity object');
  70. if (!is_array($entities)) {
  71. $entities = $entities ? array($entities) : array();
  72. }
  73. foreach ($entities as $entity) {
  74. // Only try to render the field if it is even present on this bundle.
  75. // Otherwise, field_view_field() will trigger a fatal.
  76. list (, , $bundle) = entity_extract_ids($this->entity_type, $entity);
  77. if (field_info_instance($this->entity_type, $this->definition['field_name'], $bundle)) {
  78. // Set the currently rendered entity.
  79. $values->_field_data[$this->field_alias]['entity'] = $entity;
  80. $items = array_merge($items, $this->set_items($values, $this->view->row_index));
  81. }
  82. }
  83. return $items;
  84. }
  85. /**
  86. * Overridden to force displaying multiple values in a single row.
  87. */
  88. function multiple_options_form(&$form, &$form_state) {
  89. parent::multiple_options_form($form, $form_state);
  90. $form['group_rows']['#default_value'] = TRUE;
  91. $form['group_rows']['#disabled'] = TRUE;
  92. }
  93. }