synonyms_views_handler_filter_entityreference_synonyms.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @file
  4. * Definition of synonyms_views_handler_filter_entityreference_synonyms class.
  5. */
  6. /**
  7. * Definition of synonyms friendly entity reference field filter.
  8. */
  9. class synonyms_views_handler_filter_entityreference_synonyms extends views_handler_filter_numeric {
  10. function option_definition() {
  11. $options = parent::option_definition();
  12. $options['type'] = array(
  13. 'default' => 'numeric',
  14. );
  15. return $options;
  16. }
  17. function extra_options_form(&$form, &$form_state) {
  18. parent::extra_options_form($form, $form_state);
  19. $form['type'] = array(
  20. '#type' => 'radios',
  21. '#title' => t('Type'),
  22. '#options' => array(
  23. 'numeric' => t('Numeric'),
  24. 'synonyms_autocomplete' => t('Synonyms friendly autocomplete'),
  25. 'synonyms_select' => t('Synonyms friendly select list'),
  26. ),
  27. '#default_value' => $this->options['type'],
  28. );
  29. }
  30. function has_extra_options() {
  31. return TRUE;
  32. }
  33. function operators() {
  34. $operators = parent::operators();
  35. switch ($this->options['type']) {
  36. case 'synonyms_autocomplete':
  37. case 'synonyms_select':
  38. // Only "equals" and "not equals" make sense, other operators are rather
  39. // funky when it comes to IDs and not simple integers.
  40. $operators = array_intersect_key($operators, drupal_map_assoc(array(
  41. '=', '!=',
  42. )));
  43. break;
  44. }
  45. return $operators;
  46. }
  47. function value_form(&$form, &$form_state) {
  48. parent::value_form($form, $form_state);
  49. if (isset($form['value']['#type'])) {
  50. $element = &$form['value'];
  51. }
  52. elseif (isset($form['value']['value']['#type'])) {
  53. $element = &$form['value']['value'];
  54. }
  55. if (isset($element)) {
  56. $field = field_info_field($this->definition['field_name']);
  57. $entity_type = array_keys($field['bundles']);
  58. $entity_type = reset($entity_type);
  59. $bundle = reset($field['bundles'][$entity_type]);
  60. $instance = field_info_instance($entity_type, $field['field_name'], $bundle);
  61. switch ($this->options['type']) {
  62. case 'synonyms_autocomplete':
  63. $widget = field_info_widget_settings('synonyms_autocomplete_entity');
  64. $element['#autocomplete_path'] = $widget['synonyms_autocomplete_path'] . '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'];
  65. $element['#attached']['js'][drupal_get_path('module', 'synonyms') . '/js/synonyms-autocomplete.js'] = array();
  66. $element['#attributes']['class'][] = 'synonyms-autocomplete';
  67. break;
  68. case 'synonyms_select':
  69. $element['#type'] = 'select';
  70. $element['#options'] = synonyms_select_entity_options($field, $instance);
  71. if (!$this->is_exposed()) {
  72. $element['#empty_option'] = t('- None -');
  73. }
  74. $element['#element_validate'][] = 'synonyms_select_validate';
  75. $element['#element_validate'][] = 'synonyms_select_views_entityreference_filter_validate';
  76. unset($element['#size']);
  77. break;
  78. }
  79. }
  80. }
  81. function query() {
  82. switch ($this->options['type']) {
  83. case 'synonyms_autocomplete':
  84. $field = field_info_field($this->definition['field_name']);
  85. $this->value['value'] = synonyms_get_entity_by_synonym($field['settings']['target_type'], $this->value['value'], synonyms_field_target_bundles($field));
  86. break;
  87. }
  88. parent::query();
  89. }
  90. function admin_summary() {
  91. if ($this->is_a_group()) {
  92. return t('grouped');
  93. }
  94. if (!empty($this->options['exposed'])) {
  95. return t('exposed');
  96. }
  97. switch ($this->options['type']) {
  98. case 'numeric':
  99. case 'synonyms_autocomplete':
  100. return parent::admin_summary();
  101. break;
  102. case 'synonyms_select':
  103. $field = field_info_field($this->definition['field_name']);
  104. $entity = entity_load($field['settings']['target_type'], array($this->value['value']));
  105. $entity = reset($entity);
  106. if (is_object($entity)) {
  107. $label = entity_label($field['settings']['target_type'], $entity);
  108. $options = $this->operator_options('short');
  109. return check_plain($options[$this->operator]) . ' ' . check_plain($label);
  110. }
  111. break;
  112. }
  113. }
  114. }
  115. /**
  116. * Form element validate handler.
  117. *
  118. * Simply convert select value from an array (as mostly operated within Field
  119. * module) to a single value.
  120. */
  121. function synonyms_select_views_entityreference_filter_validate($element, &$form_state) {
  122. $value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  123. $value = reset($value);
  124. form_set_value($element, $value, $form_state);
  125. }