You are here

public function YamlFormEntityAccessForm::form in YAML Form 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/YamlFormEntityAccessForm.php, line 17

Class

YamlFormEntityAccessForm
Provides a form to manage access.

Namespace

Drupal\yamlform

Code

public function form(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
  $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);
}