Skip to content

Instantly share code, notes, and snippets.

@brooke-heaton
Created August 11, 2017 22:13
Show Gist options
  • Save brooke-heaton/58b51ffd07b6e699e945cfdc3bd25111 to your computer and use it in GitHub Desktop.
Save brooke-heaton/58b51ffd07b6e699e945cfdc3bd25111 to your computer and use it in GitHub Desktop.
Alters a Drupal 8 view by adding a Left Join and Where condition
<?php
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;
/**
* Implementation of hook_views_query_alter
* @param type $view
* @param type $query
*/
function eclkc_customizations_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
// This alter view is required to filter to only 'reviewed' submissions - it was not
// possible to save the view due to a bug.
if ($view->id() == 'job_center') {
$configuration = [
'type' => 'LEFT',
'table' => 'webform_submission_data',
'field' => 'sid',
'left_table' => 'webform_submission',
'left_field' => 'sid',
'operator' => '=',
];
$join_obj = \Drupal\views\Views::pluginManager('join')
->createInstance('standard', $configuration);
$rel = $query->addRelationship('webform_submission_field_job_center_posting_status', $join_obj, 'node_field_data');
$query->addTable('webform_submission_data', $rel, $join_obj, 'webform_submission_field_job_center_posting_status');
$join_obj->extra[] = array(
'field'=>'name',
'value' => 'status',
);
$join_obj->extra_type = 'AND';
$query->addWhere('', 'webform_submission_field_job_center_posting_status.value', 'reviewed', 'IN');
}
}
@rananaresh86
Copy link

rananaresh86 commented Nov 28, 2019

Need to add drupal 8 node table with my custom table using query alter using multiple fields in where conditions.
Can you help please?

@vjain1887
Copy link

Thanks for this piece of code. Saved me!!!

@morovinger
Copy link

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment