Projet

Général

Profil

Paste
Télécharger (4,64 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entityreference / views / entityreference_plugin_display.inc @ 59ae487e

1
<?php
2

    
3
/**
4
 * @file
5
 * Handler for entityreference_plugin_display.
6
 */
7
class entityreference_plugin_display extends views_plugin_display {
8

    
9
  function option_definition() {
10
    $options = parent::option_definition();
11

    
12
    // Force the style plugin to 'entityreference_style' and the row plugin to
13
    // 'fields'.
14
    $options['style_plugin']['default'] = 'entityreference_style';
15
    $options['defaults']['default']['style_plugin'] = FALSE;
16
    $options['defaults']['default']['style_options'] = FALSE;
17
    $options['row_plugin']['default'] = 'entityreference_fields';
18
    $options['defaults']['default']['row_plugin'] = FALSE;
19
    $options['defaults']['default']['row_options'] = FALSE;
20

    
21
    // Set the display title to an empty string (not used in this display type).
22
    $options['title']['default'] = '';
23
    $options['defaults']['default']['title'] = FALSE;
24

    
25
    return $options;
26
  }
27

    
28
  function get_style_type() {
29
    return 'entityreference';
30
  }
31

    
32
  function execute() {
33
    return $this->view->render($this->display->id);
34
  }
35

    
36
  function render() {
37
    if (!empty($this->view->result) || !empty($this->view->style_plugin->definition['even empty'])) {
38
      return $this->view->style_plugin->render($this->view->result);
39
    }
40
    return '';
41
  }
42

    
43
  function uses_exposed() {
44
    return FALSE;
45
  }
46

    
47
  function query() {
48
    $options = $this->get_option('entityreference_options');
49

    
50
    // Play nice with Views UI 'preview' : if the view is not executed through
51
    // EntityReference_SelectionHandler_Views::getReferencableEntities(),
52
    // don't alter the query.
53
    if (empty($options)) {
54
      return;
55
    }
56

    
57
    // Make sure the id field is included in the results, and save its alias
58
    // so that references_plugin_style can retrieve it.
59
    $this->id_field_alias = $id_field = $this->view->query->add_field($this->view->base_table, $this->view->base_field);
60
    if (strpos($id_field, '.') === FALSE) {
61
      $id_field = $this->view->base_table . '.' . $this->id_field_alias;
62
    }
63

    
64
    // Restrict the autocomplete options based on what's been typed already.
65
    if (isset($options['match'])) {
66
      $style_options = $this->get_option('style_options');
67
      $value = db_like($options['match']) . '%';
68
      if ($options['match_operator'] != 'STARTS_WITH') {
69
        $value = '%' . $value;
70
      }
71

    
72
      // Multiple search fields are OR'd together
73
      $conditions = db_or();
74

    
75
      // Build the condition using the selected search fields
76
      foreach ($style_options['search_fields'] as $field_alias) {
77
        if (!empty($field_alias)) {
78

    
79
          // Get the table and field names for the checked field.
80
          if (empty($this->view->field[$field_alias]->field_info)) {
81
            $field = $this->view->query->fields[$this->view->field[$field_alias]->field_alias];
82
          }
83
          else {
84
            $field_table = $this->view->query->ensure_table($this->view->field[$field_alias]->table, $this->view->field[$field_alias]->relationship);
85
            $this->view->query->add_field($field_table, $this->view->field[$field_alias]->real_field, $this->view->field[$field_alias]->field, array());
86
            $field = $this->view->query->fields[$this->view->field[$field_alias]->field];
87
          }
88
          // Add an OR condition for the field
89
          $conditions->condition($field['table'] . '.' . $field['field'], $value, 'LIKE');
90
        }
91
      }
92

    
93
      $this->view->query->add_where(NULL, $conditions);
94
    }
95

    
96
    // Add an IN condition for validation.
97
    if (!empty($options['ids'])) {
98
      $this->view->query->add_where(NULL, $id_field, $options['ids']);
99
    }
100

    
101
    $this->view->set_items_per_page($options['limit']);
102
  }
103

    
104
  /**
105
   * Extend the default validation.
106
   */
107
  function validate() {
108
    $errors = parent::validate();
109
    // Verify that search fields are set up.
110
    $style_options = $this->get_option('style_options');
111
    if (!isset($style_options['search_fields'])) {
112
      $errors[] = t('Display "@display" needs a selected search fields to work properly. See the settings for the Entity Reference list format.', array('@display' => $this->display->display_title));
113
    }
114
    else {
115
      // Verify that the search fields used actually exist.
116
      //$fields = array_keys($this->view->get_items('field'));
117
      $fields = array_keys($this->handlers['field']);
118
      foreach ($style_options['search_fields'] as $field_alias => $enabled) {
119
        if ($enabled && !in_array($field_alias, $fields)) {
120
          $errors[] = t('Display "@display" uses field %field as search field, but the field is no longer present. See the settings for the Entity Reference list format.', array('@display' => $this->display->display_title, '%field' => $field_alias));
121
        }
122
      }
123
    }
124
    return $errors;
125
  }
126
}