SimpleAccessViewsAccess.php in Simple Access 8.3
File
src/views/access/SimpleAccessViewsAccess.php
View source
<?php
namespace Drupal\simple_access\views\access;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Symfony\Component\Routing\Route;
class SimpleAccessViewsAccess extends AccessPluginBase {
public function access(AccountInterface $account) {
$roles = array_keys($account->roles);
$roles[] = $account
->id() ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
$groups = simple_access_groups_from_roles($roles);
return array_intersect(array_filter($this->options['groups']), $groups);
}
public function alterRouteDefinition(Route $route) {
}
public function get_access_callback() {
return [
'simple_access_groups_check_user',
[
array_filter($this->options['groups']),
],
];
}
public function summaryTitle() {
$count = count($this->options['groups']);
if ($count < 1) {
return t('No group(s) selected');
}
elseif ($count > 1) {
return t('Multiple groups');
}
else {
$gids = array_map([
'simple_access_views_plugin_group',
'_map_groups',
], simple_access_get_groups());
$gid = array_shift($this->options['groups']);
return $gids[$gid];
}
}
public function defineOptions() {
$options = parent::defineOptions();
$options['groups'] = [
'default' => [],
];
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$options = array_map([
'simple_access_views_plugin_group',
'_map_groups',
], simple_access_get_groups());
$form['groups'] = [
'#type' => 'checkboxes',
'#title' => t('Simple access groups'),
'#default_value' => $this->options['groups'],
'#options' => $options,
'#description' => $this
->t('Only the checked simple access groups will be able to access this display. Note that users with "access all views" can see any view, regardless of role.'),
];
}
public function _map_groups($a) {
return $a['name'];
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
if (!array_filter($form_state['values']['access_options']['groups'])) {
$form_state
->setError($form['groups'], $this
->t('You must select at least one group if type is "by group"'));
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
$form_state['values']['access_options']['groups'] = array_filter($form_state['values']['access_options']['groups']);
}
}
Classes
Name |
Description |
SimpleAccessViewsAccess |
Plugin annotation
@ViewsAccess(
id = "simple_access_group",
title = @Translation("Simple Access Group"),
help = @Translation("Will be available to all users.")
) |