3 namespace Drupal\views\Plugin\views\join;
6 * Join handler for relationships that join with a subquery as the left field.
10 * LEFT JOIN node node_term_data ON ([YOUR SUBQUERY HERE]) = node_term_data.nid
13 * Join definition: same as \Drupal\views\Plugin\views\join\JoinPluginBase,
15 * - left_query: The subquery to use in the left side of the join clause.
17 * @ingroup views_join_handlers
18 * @ViewsJoin("subquery")
20 class Subquery extends JoinPluginBase {
23 * Constructs a Subquery object.
25 public function __construct(array $configuration, $plugin_id, $plugin_definition) {
26 parent::__construct($configuration, $plugin_id, $plugin_definition);
28 $this->left_query = $this->configuration['left_query'];
32 * Builds the SQL for the join this object represents.
34 * @param \Drupal\Core\Database\Query\SelectInterface $select_query
35 * The select query object.
36 * @param string $table
37 * The base table to join.
38 * @param \Drupal\views\Plugin\views\query\QueryPluginBase $view_query
39 * The source views query.
41 public function buildJoin($select_query, $table, $view_query) {
42 if (empty($this->configuration['table formula'])) {
43 $right_table = "{" . $this->table . "}";
46 $right_table = $this->configuration['table formula'];
49 // Add our join condition, using a subquery on the left instead of a field.
50 $condition = "($this->left_query) = $table[alias].$this->field";
54 // This is just copied verbatim from the parent class, which itself has a
55 // bug: https://www.drupal.org/node/1118100.
56 if (isset($this->extra)) {
57 if (is_array($this->extra)) {
59 foreach ($this->extra as $info) {
60 // Figure out the table name. Remember, only use aliases provided
61 // if at all possible.
63 if (!array_key_exists('table', $info)) {
64 $join_table = $table['alias'] . '.';
66 elseif (isset($info['table'])) {
67 $join_table = $info['table'] . '.';
70 $placeholder = ':views_join_condition_' . $select_query->nextPlaceholder();
72 if (is_array($info['value'])) {
73 $operator = !empty($info['operator']) ? $info['operator'] : 'IN';
74 // Transform from IN() notation to = notation if just one value.
75 if (count($info['value']) == 1) {
76 $info['value'] = array_shift($info['value']);
77 $operator = $operator == 'NOT IN' ? '!=' : '=';
81 $operator = !empty($info['operator']) ? $info['operator'] : '=';
84 $extras[] = "$join_table$info[field] $operator $placeholder";
85 $arguments[$placeholder] = $info['value'];
89 if (count($extras) == 1) {
90 $condition .= ' AND ' . array_shift($extras);
93 $condition .= ' AND (' . implode(' ' . $this->extraOperator . ' ', $extras) . ')';
97 elseif ($this->extra && is_string($this->extra)) {
98 $condition .= " AND ($this->extra)";
102 $select_query->addJoin($this->type, $right_table, $table['alias'], $condition, $arguments);