ViewExecutable::build

public ViewExecutable::build($display_id = NULL)

Builds the query for the view.

Parameters

string $display_id: The display ID of the view.

Return value

bool|null TRUE if the view build process was successful, FALSE if setting the display fails or NULL if the view has been built already.

File

core/modules/views/src/ViewExecutable.php, line 1183

Class

ViewExecutable
Represents a view as a whole.

Namespace

Drupal\views

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
public function build($display_id = NULL) {
  if (!empty($this->built)) {
    return;
  }
 
  if (empty($this->current_display) || $display_id) {
    if (!$this->setDisplay($display_id)) {
      return FALSE;
    }
  }
 
  // Let modules modify the view just prior to building it.
  $module_handler = \Drupal::moduleHandler();
  $module_handler->invokeAll('views_pre_build', array($this));
 
  // Attempt to load from cache.
  // @todo Load a build_info from cache.
 
  $start = microtime(TRUE);
  // If that fails, let's build!
  $this->build_info = array(
    'query' => '',
    'count_query' => '',
    'query_args' => array(),
  );
 
  $this->initQuery();
 
  // Call a module hook and see if it wants to present us with a
  // pre-built query or instruct us not to build the query for
  // some reason.
  // @todo: Implement this. Use the same mechanism Panels uses.
 
  // Run through our handlers and ensure they have necessary information.
  $this->initHandlers();
 
  // Let the handlers interact with each other if they really want.
  $this->_preQuery();
 
  if ($this->display_handler->usesExposed()) {
    /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */
    $exposed_form = $this->display_handler->getPlugin('exposed_form');
    $this->exposed_widgets = $exposed_form->renderExposedForm();
    if (FormState::hasAnyErrors() || !empty($this->build_info['abort'])) {
      $this->built = TRUE;
      // Don't execute the query, $form_state, but rendering will still be executed to display the empty text.
      $this->executed = TRUE;
      return empty($this->build_info['fail']);
    }
  }
 
  // Build all the relationships first thing.
  $this->_build('relationship');
 
  // Set the filtering groups.
  if (!empty($this->filter)) {
    $filter_groups = $this->display_handler->getOption('filter_groups');
    if ($filter_groups) {
      $this->query->setGroupOperator($filter_groups['operator']);
      foreach ($filter_groups['groups'] as $id => $operator) {
        $this->query->setWhereGroup($operator, $id);
      }
    }
  }
 
  // Build all the filters.
  $this->_build('filter');
 
  $this->build_sort = TRUE;
 
  // Arguments can, in fact, cause this whole thing to abort.
  if (!$this->_buildArguments()) {
    $this->build_time = microtime(TRUE) - $start;
    $this->attachDisplays();
    return $this->built;
  }
 
  // Initialize the style; arguments may have changed which style we use,
  // so waiting as long as possible is important. But we need to know
  // about the style when we go to build fields.
  if (!$this->initStyle()) {
    $this->build_info['fail'] = TRUE;
    return FALSE;
  }
 
  if ($this->style_plugin->usesFields()) {
    $this->_build('field');
  }
 
  // Build our sort criteria if we were instructed to do so.
  if (!empty($this->build_sort)) {
    // Allow the style handler to deal with sorting.
    if ($this->style_plugin->buildSort()) {
      $this->_build('sort');
    }
    // allow the plugin to build second sorts as well.
    $this->style_plugin->buildSortPost();
  }
 
  // Allow area handlers to affect the query.
  $this->_build('header');
  $this->_build('footer');
  $this->_build('empty');
 
  // Allow display handler to affect the query:
  $this->display_handler->query($this->display_handler->useGroupBy());
 
  // Allow style handler to affect the query:
  $this->style_plugin->query($this->display_handler->useGroupBy());
 
  // Allow exposed form to affect the query:
  if (isset($exposed_form)) {
    $exposed_form->query();
  }
 
  if (\Drupal::config('views.settings')->get('sql_signature')) {
    $this->query->addSignature($this);
  }
 
  // Let modules modify the query just prior to finalizing it.
  $this->query->alter($this);
 
  // Only build the query if we weren't interrupted.
  if (empty($this->built)) {
    // Build the necessary info to execute the query.
    $this->query->build($this);
  }
 
  $this->built = TRUE;
  $this->build_time = microtime(TRUE) - $start;
 
  // Attach displays
  $this->attachDisplays();
 
  // Let modules modify the view just after building it.
  $module_handler->invokeAll('views_post_build', array($this));
 
  return TRUE;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.