Last active
December 7, 2018 10:16
-
-
Save JeromeChevalier/4287fc3d877a014102fdbce9292ccf4d to your computer and use it in GitHub Desktop.
Build a views filter to have a IN clause #drupal8
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 | |
/** | |
* Created by PhpStorm. | |
* User: jeromechevalier | |
* Date: 29/10/2018 | |
* Time: 14:15 | |
*/ | |
namespace Drupal\portal_users\Plugin\views\filter; | |
use Drupal\user\Entity\User; | |
use Drupal\views\Annotation\ViewsFilter; | |
use Drupal\views\Plugin\views\filter\FilterPluginBase; | |
use Drupal\views\Plugin\views\join\JoinPluginBase; | |
/** | |
* Class RetailerFilter | |
* | |
* This class automatically filter the view content based on the value retailer_id of the user connected | |
* | |
* @ingroup views_filter_handlers | |
* | |
* @ViewsFilter("location_filter") | |
* | |
*/ | |
class LocationFilter extends FilterPluginBase | |
{ | |
/** | |
* @return array | |
*/ | |
protected function operators() | |
{ | |
$operators = [ | |
'IN' => [ | |
'title' => $this->t('Filter download by allowed site'), | |
'method' => 'applyLocationFilter', | |
'short' => $this->t('Allowed site only '), | |
'values' => '1', | |
], | |
]; | |
return $operators; | |
} | |
function query() | |
{ | |
$user = User::load(\Drupal::currentUser()->id()); | |
// Manage the fact that Retailer Administrator have an access to all retailer sites declare | |
// for this reason we don't check inside user property, but load all available sites with the right Retailer ID | |
if ($user->hasRole('retailer_administrator')) { | |
$siteQuery = \Drupal::entityQuery('site_entity'); | |
$retailerID = \Drupal::service('portal_users.retailers_service')->getCurrentUserRetailerId(); | |
$siteQuery->condition('field_site_retailer',$retailerID); | |
$sitesId = $siteQuery->execute(); | |
} | |
else { | |
$tempList = $user->get('field_user_sites')->referencedEntities(); | |
$sitesId = array(); | |
if (count($tempList) > 0) { | |
foreach ($tempList as $site) { | |
$sitesId[] = $site->id(); | |
} | |
} | |
} | |
$sitesId = \Drupal::service('portal_users.retailers_service')->getAllowedLocationsId(); | |
$this->query->addField('download_forecast','id','',['function' => 'groupby']); // Add the field to be able to do the group by SQL command | |
$this->query->addTable('download_forecast__physical_location'); | |
// Filter download that correspond to an allowed site for the current user | |
$this->query->addWhere($this->options['group'], 'download_forecast__physical_location.physical_location_target_id', $sitesId, 'IN'); | |
} | |
} |
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 | |
/** | |
* Implements hook_views_data(). | |
* Add the Retailer Filter to list of filter available for Views | |
*/ | |
function portal_users_views_data() | |
{ | |
$data = []; | |
$data['views']['location_filter'] = [ | |
'title' => t('Location Filter'), | |
'filter' => [ | |
'title' => t('Filter to have only available site'), | |
'group' => 'SRAI-Portal', | |
'help' => t('Filter to have only available site'), | |
'id' => 'location_filter' | |
], | |
]; | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment