entity_views_handler_field_options.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @file
  4. * Contains the entity_views_handler_field_options class.
  5. */
  6. /**
  7. * A handler to provide proper displays for values chosen from a set of options.
  8. *
  9. * This handler may only be used in conjunction with data selection based Views
  10. * tables or other base tables using a query plugin that supports data
  11. * selection.
  12. *
  13. * @see entity_views_field_definition()
  14. * @ingroup views_field_handlers
  15. */
  16. class entity_views_handler_field_options extends views_handler_field {
  17. /**
  18. * Stores the entity type of the result entities.
  19. */
  20. public $entity_type;
  21. /**
  22. * Stores the result entities' metadata wrappers.
  23. */
  24. public $wrappers = array();
  25. /**
  26. * Stores the current value when rendering list fields.
  27. */
  28. public $current_value;
  29. /**
  30. * The key / name mapping for the options.
  31. */
  32. public $option_list;
  33. /**
  34. * Overridden to add the field for the entity ID (if necessary).
  35. */
  36. public function query() {
  37. EntityFieldHandlerHelper::query($this);
  38. }
  39. /**
  40. * Adds a click-sort to the query.
  41. */
  42. public function click_sort($order) {
  43. EntityFieldHandlerHelper::click_sort($this, $order);
  44. }
  45. /**
  46. * Load the entities for all rows that are about to be displayed.
  47. */
  48. public function pre_render(&$values) {
  49. parent::pre_render($values);
  50. EntityFieldHandlerHelper::pre_render($this, $values);
  51. }
  52. /**
  53. * Overridden to use a metadata wrapper.
  54. */
  55. public function get_value($values, $field = NULL) {
  56. return EntityFieldHandlerHelper::get_value($this, $values, $field);
  57. }
  58. /**
  59. * Specifies the options this handler uses.
  60. */
  61. public function option_definition() {
  62. $options = parent::option_definition();
  63. $options += EntityFieldHandlerHelper::option_definition($this);
  64. $options['format_name'] = array('default' => TRUE);
  65. return $options;
  66. }
  67. /**
  68. * Returns an option form for setting this handler's options.
  69. */
  70. public function options_form(&$form, &$form_state) {
  71. parent::options_form($form, $form_state);
  72. EntityFieldHandlerHelper::options_form($this, $form, $form_state);
  73. $form['format_name'] = array(
  74. '#title' => t('Use human-readable name'),
  75. '#type' => 'checkbox',
  76. '#description' => t("If this is checked, the values' names will be displayed instead of their internal identifiers."),
  77. '#default_value' => $this->options['format_name'],
  78. '#weight' => -5,
  79. );
  80. }
  81. public function render($values) {
  82. return EntityFieldHandlerHelper::render($this, $values);
  83. }
  84. /**
  85. * Render a single field value.
  86. */
  87. public function render_single_value($value, $values) {
  88. if (!isset($this->option_list)) {
  89. $this->option_list = array();
  90. $callback = $this->definition['options callback'];
  91. if (is_callable($callback['function'])) {
  92. // If a selector is used, get the name of the selected field.
  93. $field_name = EntityFieldHandlerHelper::get_selector_field_name($this->real_field);
  94. $this->option_list = call_user_func($callback['function'], $field_name, $callback['info'], 'view');
  95. }
  96. }
  97. if ($this->options['format_name'] && isset($this->option_list[$value])) {
  98. $value = $this->option_list[$value];
  99. }
  100. // Sanitization is handled by the wrapper, see
  101. // EntityFieldHandlerHelper::get_value().
  102. return $value;
  103. }
  104. }