entity_translation_handler_filter_translation_exists.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * Contains an entity type filter handler.
  5. */
  6. /**
  7. * This handler determines if a translation exists for a particular translation.
  8. */
  9. class entity_translation_handler_filter_translation_exists extends views_handler_filter_locale_language {
  10. /**
  11. * Add a 'entity_type' option definition.
  12. * @see views_handler_field::option:definition()
  13. */
  14. function option_definition() {
  15. $options = parent::option_definition();
  16. $options['entity_type'] = array('default' => '', 'translatable' => FALSE);
  17. $options['use_filter'] = array('default' => '', 'translatable' => FALSE);
  18. $options['filter'] = array('default' => '', 'translatable' => FALSE);
  19. return $options;
  20. }
  21. /**
  22. * Override the default title for the operators.
  23. */
  24. function operators() {
  25. $operators = parent::operators();
  26. $operators['in']['title'] = t('Translation exists');
  27. $operators['not in']['title'] = t('Translation doesn\'t exist');
  28. return $operators;
  29. }
  30. /**
  31. * Add option for setting entity type either directly or through a filter.
  32. * @see views_handler_field::options_form()
  33. */
  34. function options_form(&$form, &$form_state) {
  35. parent::options_form($form, $form_state);
  36. $filters = $this->get_entity_type_filters();
  37. if (!empty($filters)) {
  38. $form['use_filter'] = array(
  39. '#type' => 'checkbox',
  40. '#title' => t('Use an entity type filter.'),
  41. '#default_value' => $this->options['use_filter'],
  42. );
  43. $form['filter'] = array(
  44. '#type' => 'select',
  45. '#title' => t('Filter'),
  46. '#options' => $filters,
  47. '#dependency' => array(
  48. 'edit-options-use-filter' => array(1)
  49. ),
  50. '#default_value' => $this->options['filter'],
  51. );
  52. }
  53. $form['entity_type'] = array(
  54. '#title' => t('Entity type'),
  55. '#type' => 'select',
  56. '#options' => $this->get_allowed_types(),
  57. '#dependency' => array(
  58. 'edit-options-use-filter' => array(0)
  59. ),
  60. '#default_value' => $this->options['entity_type'],
  61. '#description' => t('You have to filter on a particular entity type when you use this filter'),
  62. );
  63. }
  64. /**
  65. * Get all available entity type filters that can be used to build the query.
  66. */
  67. function get_entity_type_filters() {
  68. // We need to build the query to know about the available fields.
  69. $this->view->build();
  70. $filters = array();
  71. foreach ($this->view->filter as $key => $filter) {
  72. // Break if we encounter our filter, the filter must be before this one.
  73. if ($filter == $this) {
  74. break;
  75. }
  76. if ($filter instanceof entity_translation_handler_filter_entity_type && count($filter->value) == 1 && empty($filter->options['expose']['multiple'])) {
  77. $filters[$key] = $filter->value_title;
  78. }
  79. }
  80. return $filters;
  81. }
  82. /**
  83. * Get entity types managed by entity translation.
  84. */
  85. function get_allowed_types() {
  86. $allowed_types_options = variable_get('entity_translation_entity_types');
  87. $allowed_types = array();
  88. $entity_info = entity_get_info();
  89. foreach ($allowed_types_options as $key => $allowed) {
  90. if ($allowed) {
  91. $allowed_types[$key] = $entity_info[$key]['label'];
  92. }
  93. }
  94. return $allowed_types;
  95. }
  96. /**
  97. * Override the default behaviour of the handler.
  98. */
  99. function query() {
  100. $this->ensure_my_table();
  101. // We need a subquery to determine not in.
  102. if ($this->operator == 'not in') {
  103. $entity_type = 'node';
  104. if ($this->options['use_filter'] && isset($this->view->filter[$this->options['filter']])) {
  105. $filter = $this->view->filter[$this->options['filter']];
  106. $entity_type = current($filter->value);
  107. }
  108. else {
  109. $this->query->add_where($this->options['group'], "$this->table_alias.entity_type", $this->options['entity_type'], '=');
  110. $entity_type = $this->options['entity_type'];
  111. }
  112. $query = db_select('entity_translation', 'es')
  113. ->condition('entity_type', $entity_type)
  114. ->condition('language', $this->value);
  115. $query->addField('es', 'entity_id');
  116. $this->query->add_where($this->options['group'], "$this->table_alias.entity_id", $query, $this->operator);
  117. }
  118. // We can determine if a translation exists without a subquery.
  119. else {
  120. $value = array_keys($this->value);
  121. $this->query->add_where($this->options['group'], "$this->table_alias.source", '', '<>');
  122. $this->query->add_where($this->options['group'], "$this->table_alias.language", array_values($this->value), $this->operator);
  123. }
  124. }
  125. }