You are here

public function ContentAccessPageForm::buildForm in Content Access 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/ContentAccessPageForm.php, line 83

Class

ContentAccessPageForm
Node Access settings form.

Namespace

Drupal\content_access\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
  $defaults = [];
  foreach (_content_access_get_operations() as $op => $label) {
    $defaults[$op] = content_access_per_node_setting($op, $node);
  }
  $this
    ->roleBasedForm($form, $defaults, $node
    ->getType());

  // Add an after_build handler that disables checkboxes, which are enforced
  // by permissions.
  $build_info = $form_state
    ->getBuildInfo();
  $build_info['files'][] = [
    'module' => 'content_access',
    'type' => 'inc',
    'name' => 'content_access.admin',
  ];
  $form_state
    ->setBuildInfo($build_info);
  foreach ([
    'update',
    'update_own',
    'delete',
    'delete_own',
  ] as $op) {
    $form['per_role'][$op]['#process'][] = '::forcePermissions';
  }

  // ACL form.
  if ($this->moduleHandler
    ->moduleExists('acl')) {

    // This is disabled when there is no node passed.
    $form['acl'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('User access control lists'),
      '#description' => $this
        ->t('These settings allow you to grant access to specific users.'),
      '#collapsible' => TRUE,
      '#tree' => TRUE,
    ];
    foreach ([
      'view',
      'update',
      'delete',
    ] as $op) {
      $acl_id = content_access_get_acl_id($node, $op);
      $view = (int) ($op == 'view');
      $update = (int) ($op == 'update');
      acl_node_add_acl($node
        ->id(), $acl_id, $view, $update, (int) ($op == 'delete'), content_access_get_settings('priority', $node
        ->getType()));
      $form['acl'][$op] = acl_edit_form($form_state, $acl_id, $this
        ->t('Grant @op access', [
        '@op' => $op,
      ]));
      $post_acl_id = $this
        ->getRequest()->request
        ->get('acl_' . $acl_id, NULL);
      $form['acl'][$op]['#collapsed'] = !isset($post_acl_id) && !unserialize($form['acl'][$op]['user_list']['#default_value']);
    }
  }
  $storage['node'] = $node;
  $form_state
    ->setStorage($storage);
  $form['reset'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Reset to defaults'),
    '#weight' => 10,
    '#submit' => [
      '::pageResetSubmit',
    ],
    '#access' => !empty(content_access_get_per_node_settings($node)),
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
    '#weight' => 10,
  ];

  // @todo not true anymore?
  // http://drupal.org/update/modules/6/7#hook_node_access_records
  if (!$node
    ->isPublished()) {
    $this
      ->messenger()
      ->addError($this
      ->t("Warning: Your content is not published, so this settings are not taken into account as long as the content remains unpublished."));
  }
  return $form;
}