ViewsAccessCallback.php in Views Access Callback 8
File
src/Plugin/views/access/ViewsAccessCallback.php
View source
<?php
namespace Drupal\views_access_callback\Plugin\views\access;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Utility\Html;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
class ViewsAccessCallback extends AccessPluginBase {
protected $usesOptions = TRUE;
protected $moduleHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'));
}
public function access(AccountInterface $account) {
return views_access_callback_access_callback($this->view->storage
->id(), $this->view->current_display, $this->options['views_access_callback']);
}
public function alterRouteDefinition(Route $route) {
$bool = views_access_callback_access_callback($this->view->storage
->id(), $this->view->current_display, $this->options['views_access_callback']) ? 'TRUE' : 'FALSE';
$route
->setRequirement('_access', $bool);
}
public function summaryTitle() {
return $this
->t('Callback function') . ': ' . $this->options['views_access_callback'];
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['views_access_callback'] = array(
'default' => 'TRUE',
);
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$callbacks = array();
$module_info = system_get_info('module');
foreach ($this->moduleHandler
->getImplementations('views_access_callbacks') as $module) {
$functions = $this->moduleHandler
->invoke($module, 'views_access_callbacks');
foreach ($functions as $function => $name) {
$callbacks[$module_info[$module]['name']][$function] = Html::escape($name);
}
}
ksort($callbacks);
$form['views_access_callback'] = array(
'#type' => 'select',
'#options' => $callbacks,
'#title' => $this
->t('Callbacks'),
'#default_value' => $this->options['views_access_callback'],
'#description' => $this
->t('Only users for which selected callback function returns TRUE will be able to access this display.'),
);
}
}