View source
<?php
namespace Drupal\yamlform;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\User;
class YamlFormEntityAccessForm extends EntityForm {
public function form(array $form, FormStateInterface $form_state) {
$yamlform = $this->entity;
$access = $yamlform
->getAccessRules();
$permissions = [
'create' => $this
->t('Create form submissions'),
'view_any' => $this
->t('View all form submissions'),
'update_any' => $this
->t('Update all form submissions'),
'delete_any' => $this
->t('Delete all form submissions'),
'purge_any' => $this
->t('Purge all form submissions'),
'view_own' => $this
->t('View own form submissions'),
'update_own' => $this
->t('Update own form submissions'),
'delete_own' => $this
->t('Delete own form submissions'),
];
$form['access']['#tree'] = TRUE;
foreach ($permissions as $name => $title) {
$form['access'][$name] = [
'#type' => $name === 'create' ? 'fieldset' : 'details',
'#title' => $title,
'#open' => $access[$name]['roles'] || $access[$name]['users'] ? TRUE : FALSE,
];
$form['access'][$name]['roles'] = [
'#type' => 'yamlform_roles',
'#title' => $this
->t('Roles'),
'#include_anonymous' => $name == 'create' ? TRUE : FALSE,
'#default_value' => $access[$name]['roles'],
];
$form['access'][$name]['users'] = [
'#type' => 'yamlform_users',
'#title' => $this
->t('Users'),
'#default_value' => $access[$name]['users'] ? User::loadMultiple($access[$name]['users']) : [],
];
}
return parent::form($form, $form_state);
}
protected function actionsElement(array $form, FormStateInterface $form_state) {
$element = parent::actionsElement($form, $form_state);
unset($element['delete']);
return $element;
}
public function save(array $form, FormStateInterface $form_state) {
$access = $form_state
->getValue('access');
$yamlform = $this
->getEntity();
$yamlform
->setAccessRules($access);
$yamlform
->save();
$this
->logger('yamlform')
->notice('Form access @label saved.', [
'@label' => $yamlform
->label(),
]);
drupal_set_message($this
->t('Form access %label saved.', [
'%label' => $yamlform
->label(),
]));
}
}