ConnectionAccess.php in RedHen CRM 8
File
modules/redhen_connection/src/Plugin/views/access/ConnectionAccess.php
View source
<?php
namespace Drupal\redhen_connection\Plugin\views\access;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
class ConnectionAccess extends AccessPluginBase {
protected $usesOptions = TRUE;
protected $routeMatch;
protected $connectionAccessCheck;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->routeMatch = $container
->get('current_route_match');
$instance->connectionAccessCheck = $container
->get('redhen_connection.access_check');
return $instance;
}
public function getOperations() {
return [
'view' => $this
->t('View'),
'view label' => $this
->t('View label'),
'update' => $this
->t('Update'),
'delete' => $this
->t('Delete'),
];
}
public function access(AccountInterface $account) {
return $this->connectionAccessCheck
->access($this->routeMatch, $account);
}
public function alterRouteDefinition(Route $route) {
$operation = $this->options['operation'] ?? 'view';
$route
->setRequirement('_redhen_connection_access', $operation);
}
public function summaryTitle() {
$operation = $this->options['operation'] ?? 'view';
$operations = $this
->getOperations();
return $operations[$operation];
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['operation'] = [
'default' => 'view',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$operation = $this->options['operation'] ?? 'view';
$operations = $this
->getOperations();
$form['operation'] = [
'#type' => 'select',
'#options' => $operations,
'#title' => $this
->t('Operation'),
'#default_value' => $operation,
'#description' => $this
->t('Only users with the ability to perform this operation on the RedHen entity in the url path will be able to access this display.<br>Note: this only applies to routes that contain {redhen_contact} or {redhen_org} parameters.'),
];
}
}