Project

General

Profile

Paste
Download (7.39 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / entityreference / plugins / selection / EntityReference_SelectionHandler_Views.class.php @ 56aebcb7

1
<?php
2

    
3
/**
4
 * Entity handler for Views.
5
 */
6
class EntityReference_SelectionHandler_Views implements EntityReference_SelectionHandler {
7

    
8
  /**
9
   * Implements EntityReferenceHandler::getInstance().
10
   */
11
  public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
12
    return new EntityReference_SelectionHandler_Views($field, $instance);
13
  }
14

    
15
  protected function __construct($field, $instance) {
16
    $this->field = $field;
17
    $this->instance = $instance;
18
  }
19

    
20
  /**
21
   * Implements EntityReferenceHandler::settingsForm().
22
   */
23
  public static function settingsForm($field, $instance) {
24
    $view_settings = empty($field['settings']['handler_settings']['view']) ? '' : $field['settings']['handler_settings']['view'];
25
    $displays = views_get_applicable_views('entityreference display');
26
    // Filter views that list the entity type we want, and group the separate
27
    // displays by view.
28
    $entity_info = entity_get_info($field['settings']['target_type']);
29
    $options = array();
30
    foreach ($displays as $data) {
31
      list($view, $display_id) = $data;
32
      if ($view->base_table == $entity_info['base table']) {
33
        $options[$view->name . ':' . $display_id] = $view->name . ' - ' . $view->display[$display_id]->display_title;
34
      }
35
    }
36

    
37
    // The value of the 'view_and_display' select below will need to be split
38
    // into 'view_name' and 'view_display' in the final submitted values, so
39
    // we massage the data at validate time on the wrapping element (not
40
    // ideal).
41
    $form['view']['#element_validate'] = array('entityreference_view_settings_validate');
42

    
43
    if ($options) {
44
      $default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL;
45
      $form['view']['view_and_display'] = array(
46
        '#type' => 'select',
47
        '#title' => t('View used to select the entities'),
48
        '#required' => TRUE,
49
        '#options' => $options,
50
        '#default_value' => $default,
51
        '#description' => '<p>' . t('Choose the view and display that select the entities that can be referenced.<br />Only views with a display of type "Entity Reference" are eligible.') . '</p>',
52
      );
53

    
54
      $default = !empty($view_settings['args']) ? implode(', ', $view_settings['args']) : '';
55
      $form['view']['args'] = array(
56
        '#type' => 'textfield',
57
        '#title' => t('View arguments'),
58
        '#default_value' => $default,
59
        '#required' => FALSE,
60
        '#description' => t('Provide a comma separated list of arguments to pass to the view.'),
61
      );
62
    }
63
    else {
64
      $form['view']['no_view_help'] = array(
65
        '#markup' => '<p>' . t('No eligible views were found. <a href="@create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href="@existing">existing view</a>.', array(
66
          '@create' => url('admin/structure/views/add'),
67
          '@existing' => url('admin/structure/views'),
68
        )) . '</p>',
69
      );
70
    }
71
    return $form;
72
  }
73

    
74
  protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) {
75
    $view_name = $this->field['settings']['handler_settings']['view']['view_name'];
76
    $display_name = $this->field['settings']['handler_settings']['view']['display_name'];
77
    $args = $this->field['settings']['handler_settings']['view']['args'];
78
    $entity_type = $this->field['settings']['target_type'];
79

    
80
    // Check that the view is valid and the display still exists.
81
    $this->view = views_get_view($view_name);
82
    if (!$this->view || !isset($this->view->display[$display_name]) || !$this->view->access($display_name)) {
83
      watchdog('entityreference', 'The view %view_name is no longer eligible for the %field_name field.', array('%view_name' => $view_name, '%field_name' => $this->instance['label']), WATCHDOG_WARNING);
84
      return FALSE;
85
    }
86
    $this->view->set_display($display_name);
87

    
88
    // Make sure the query is not cached.
89
    $this->view->is_cacheable = FALSE;
90

    
91
    // Pass options to the display handler to make them available later.
92
    $entityreference_options = array(
93
      'match' => $match,
94
      'match_operator' => $match_operator,
95
      'limit' => $limit,
96
      'ids' => $ids,
97
    );
98
    $this->view->display_handler->set_option('entityreference_options', $entityreference_options);
99
    return TRUE;
100
  }
101

    
102
  /**
103
   * Implements EntityReferenceHandler::getReferencableEntities().
104
   */
105
  public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
106
    $display_name = $this->field['settings']['handler_settings']['view']['display_name'];
107
    $args = $this->field['settings']['handler_settings']['view']['args'];
108
    $result = array();
109
    if ($this->initializeView($match, $match_operator, $limit)) {
110
      // Get the results.
111
      $result = $this->view->execute_display($display_name, $args);
112
    }
113

    
114
    $return = array();
115
    if ($result) {
116
      $target_type = $this->field['settings']['target_type'];
117
      $entities = entity_load($target_type, array_keys($result));
118
      foreach($entities as $entity) {
119
        list($id,, $bundle) = entity_extract_ids($target_type, $entity);
120
        $return[$bundle][$id] = $result[$id];
121
      }
122
    }
123
    return $return;
124
  }
125

    
126
  /**
127
   * Implements EntityReferenceHandler::countReferencableEntities().
128
   */
129
  function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') {
130
    $this->getReferencableEntities($match, $match_operator);
131
    return $this->view->total_items;
132
  }
133

    
134
  function validateReferencableEntities(array $ids) {
135
    $display_name = $this->field['settings']['handler_settings']['view']['display_name'];
136
    $args = $this->field['settings']['handler_settings']['view']['args'];
137
    $result = array();
138
    if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) {
139
      // Get the results.
140
      $entities = $this->view->execute_display($display_name, $args);
141
      if (!empty($entities)) {
142
        $result = array_keys($entities);
143
      }
144
    }
145
    return $result;
146
  }
147

    
148
  /**
149
   * Implements EntityReferenceHandler::validateAutocompleteInput().
150
   */
151
  public function validateAutocompleteInput($input, &$element, &$form_state, $form) {
152
    return NULL;
153
  }
154

    
155
  /**
156
   * Implements EntityReferenceHandler::getLabel().
157
   */
158
  public function getLabel($entity) {
159
    return entity_label($this->field['settings']['target_type'], $entity);
160
  }
161

    
162
  /**
163
   * Implements EntityReferenceHandler::entityFieldQueryAlter().
164
   */
165
  public function entityFieldQueryAlter(SelectQueryInterface $query) {
166

    
167
  }
168

    
169
}
170

    
171
function entityreference_view_settings_validate($element, &$form_state, $form) {
172
  // Split view name and display name from the 'view_and_display' value.
173
  if (!empty($element['view_and_display']['#value'])) {
174
    list($view, $display) = explode(':', $element['view_and_display']['#value']);
175
  }
176
  else {
177
    form_error($element, t('The views entity selection mode requires a view.'));
178
    return;
179
  }
180

    
181
  // Explode the 'args' string into an actual array. Beware, explode() turns an
182
  // empty string into an array with one empty string. We'll need an empty array
183
  // instead.
184
  $args_string = trim($element['args']['#value']);
185
  if ($args_string === '') {
186
    $args = array();
187
  }
188
  else {
189
    // array_map is called to trim whitespaces from the arguments.
190
    $args = array_map('trim', explode(',', $args_string));
191
  }
192

    
193
  $value = array('view_name' => $view, 'display_name' => $display, 'args' => $args);
194
  form_set_value($element, $value, $form_state);
195
}