Created
August 11, 2017 22:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this piece of code. Saved me!!!