CurrentUserOrPermission.php in Ubercart 8.4
File
uc_order/src/Plugin/views/argument_validator/CurrentUserOrPermission.php
View source
<?php
namespace Drupal\uc_order\Plugin\views\argument_validator;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\User;
use Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase;
class CurrentUserOrPermission extends ArgumentValidatorPluginBase {
protected function defineOptions() {
$options = parent::defineOptions();
$options['perm'] = [
'default' => 'view all orders',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$options = [];
$permissions = \Drupal::service('user.permissions')
->getPermissions();
foreach ($permissions as $name => $permission) {
$options[$permission['provider']][$name] = $permission['title'];
}
$form['perm'] = [
'#type' => 'select',
'#options' => $options,
'#title' => $this
->t('Permission'),
'#default_value' => $this->options['perm'],
'#description' => $this
->t('Users with the selected permission flag will be able to bypass validation.'),
];
}
public function validateArgument($argument) {
if (!User::load($argument)) {
return FALSE;
}
if (\Drupal::currentUser()
->id() == $argument) {
return TRUE;
}
if (\Drupal::currentUser()
->hasPermission($this->options['perm'])) {
return TRUE;
}
return FALSE;
}
}