synonyms_views_handler_field_synonyms.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @file
  4. * Definition of synonyms_handler_field_synonyms class.
  5. */
  6. /**
  7. * Views field handler for displaying synonyms of an entity.
  8. */
  9. class synonyms_views_handler_field_synonyms extends views_handler_field {
  10. function option_definition() {
  11. $options = parent::option_definition();
  12. $options['list'] = array(
  13. 'default' => 'ul',
  14. );
  15. $options['separator'] = array(
  16. 'default' => '',
  17. 'translatable' => TRUE,
  18. );
  19. return $options;
  20. }
  21. function options_form(&$form, &$form_state) {
  22. parent::options_form($form, $form_state);
  23. $form['list'] = array(
  24. '#type' => 'radios',
  25. '#title' => t('Display type'),
  26. '#options' => array(
  27. 'ul' => t('Unordered list'),
  28. 'ol' => t('Ordered list'),
  29. 'separator' => t('Simple separator'),
  30. ),
  31. '#default_value' => $this->options['list'],
  32. );
  33. $form['separator'] = array(
  34. '#type' => 'textfield',
  35. '#title' => t('Separator'),
  36. '#default_value' => $this->options['separator'],
  37. '#dependency' => array(
  38. 'radio:options[list]' => array('separator'),
  39. ),
  40. );
  41. }
  42. function pre_render(&$values) {
  43. // Load all the queried entities in a single batch.
  44. $entity_ids = array();
  45. foreach ($values as $value) {
  46. $entity_ids[] = $value->{$this->definition['real field']};
  47. }
  48. $entity_ids = array_unique($entity_ids);
  49. if (!empty($entity_ids)) {
  50. $entities = entity_load($this->definition['synonyms entity type'], $entity_ids);
  51. foreach ($values as &$value) {
  52. $value->synonyms_entity = $entities[$value->{$this->definition['real field']}];
  53. unset($value);
  54. }
  55. }
  56. parent::pre_render($values);
  57. }
  58. function get_value($values, $field = NULL) {
  59. $property = $this->field;
  60. $wrapper = entity_metadata_wrapper($this->definition['synonyms entity type'], $values->synonyms_entity);
  61. $synonyms = $wrapper->$property->value(array('sanitize' => TRUE));
  62. if (empty($synonyms)) {
  63. $synonyms = '';
  64. }
  65. else {
  66. switch ($this->options['list']) {
  67. case 'ol':
  68. case 'ul':
  69. $synonyms = theme('item_list', array(
  70. 'type' => $this->options['list'],
  71. 'items' => $synonyms,
  72. ));
  73. break;
  74. case 'separator':
  75. $synonyms = implode($this->options['separator'], $synonyms);
  76. break;
  77. }
  78. }
  79. return $synonyms;
  80. }
  81. function sanitize_value($value, $type = NULL) {
  82. if (is_null($type)) {
  83. $type = 'xss_admin';
  84. }
  85. return parent::sanitize_value($value, $type);
  86. }
  87. }