WebformEntitySettingsAccessForm.php in Webform 6.x
File
src/EntitySettings/WebformEntitySettingsAccessForm.php
View source
<?php
namespace Drupal\webform\EntitySettings;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformEntitySettingsAccessForm extends WebformEntitySettingsBaseForm {
protected $accessRulesManager;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->accessRulesManager = $container
->get('webform.access_rules_manager');
return $instance;
}
public function form(array $form, FormStateInterface $form_state) {
$webform = $this->entity;
$form['access']['#tree'] = TRUE;
$access = $webform
->getAccessRules() + $this->accessRulesManager
->getDefaultAccessRules();
$access_rules = $this->accessRulesManager
->getAccessRulesInfo();
foreach ($access_rules as $access_rule => $info) {
$form['access'][$access_rule] = [
'#type' => $access_rule === 'create' ? 'fieldset' : 'details',
'#title' => $info['title'],
'#open' => $access[$access_rule]['roles'] || $access[$access_rule]['users'] ? TRUE : FALSE,
'#description' => $info['description'],
'#help' => FALSE,
];
$form['access'][$access_rule]['roles'] = [
'#type' => 'webform_roles',
'#title' => $this
->t('Roles'),
'#include_anonymous' => !in_array($access_rule, [
'update_any',
'delete_any',
'purge_any',
]) ? TRUE : FALSE,
'#default_value' => $access[$access_rule]['roles'],
];
$form['access'][$access_rule]['users'] = [
'#type' => 'webform_users',
'#title' => $this
->t('Users'),
'#default_value' => $access[$access_rule]['users'] ? $this->entityTypeManager
->getStorage('user')
->loadMultiple($access[$access_rule]['users']) : [],
];
$form['access'][$access_rule]['permissions'] = [
'#type' => 'webform_permissions',
'#title' => $this
->t('Permissions'),
'#multiple' => TRUE,
'#select2' => TRUE,
'#default_value' => $access[$access_rule]['permissions'],
];
}
return parent::form($form, $form_state);
}
public function save(array $form, FormStateInterface $form_state) {
$access = $form_state
->getValue('access');
$webform = $this
->getEntity();
$webform
->setAccessRules($access);
parent::save($form, $form_state);
}
}