You are here

public function ContentAccessAdminSettingsForm::submitForm in Content Access 8

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/ContentAccessAdminSettingsForm.php, line 120

Class

ContentAccessAdminSettingsForm
Node Access settings form.

Namespace

Drupal\content_access\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $storage = $form_state
    ->getStorage();
  $roles = array_keys(user_roles());
  $roles_permissions = user_role_permissions($roles);
  $permissions = $this->permissionHandler
    ->getPermissions();
  $node_type = $storage['node_type'];

  // Remove disabled modules permissions, so they can't raise exception
  // in ::savePermissions().
  foreach ($roles_permissions as $rid => $role_permissions) {
    foreach ($role_permissions as $permission => $value) {
      if (!array_key_exists($permission, $permissions)) {
        unset($roles_permissions[$rid][$permission]);
      }
    }
  }
  foreach ([
    'update',
    'update_own',
    'delete',
    'delete_own',
  ] as $op) {
    foreach ($values[$op] as $rid => $value) {
      $permission = content_access_get_permission_by_op($op, $node_type);
      if ($value) {
        $roles_permissions[$rid][$permission] = TRUE;
      }
      else {
        $roles_permissions[$rid][$permission] = FALSE;
      }
    }

    // Don't save the setting, so its default value (get permission) is
    // applied always.
    unset($values[$op]);
  }
  $this
    ->savePermissions($roles_permissions);

  // Update content access settings.
  $settings = content_access_get_settings('all', $node_type);
  foreach (content_access_available_settings() as $setting) {
    if (isset($values[$setting])) {
      $settings[$setting] = is_array($values[$setting]) ? array_keys(array_filter($values[$setting])) : $values[$setting];
    }
  }
  content_access_set_settings($settings, $node_type);

  // Mass update the nodes, but only if necessary.
  if (content_access_get_settings('per_node', $node_type) || content_access_get_settings('view', $node_type) != $form['per_role']['view']['#default_value'] || content_access_get_settings('view_own', $node_type) != $form['per_role']['view_own']['#default_value'] || content_access_get_settings('priority', $node_type) != $form['advanced']['priority']['#default_value'] || content_access_get_settings('per_node', $node_type) != $form['node']['per_node']['#default_value']) {

    // If per node has been disabled and we use the ACL integration, we have
    // to remove possible ACLs now.
    if (!content_access_get_settings('per_node', $node_type) && $form['node']['per_node']['#default_value'] && $this->moduleHandler
      ->moduleExists('acl')) {
      _content_access_remove_acls($node_type);
    }
    if (content_access_mass_update([
      $node_type,
    ])) {
      $node_types = node_type_get_names();

      // This does not gurantee a rebuild.
      $this
        ->messenger()
        ->addMessage($this
        ->t('Permissions have been changed for the content type @types.<br />You may have to <a href=":rebuild">rebuild permissions</a> for your changes to take effect.', [
        '@types' => $node_types[$node_type],
        ':rebuild' => Url::fromRoute('node.configure_rebuild_confirm')
          ->toString(),
      ]));
    }
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('No change.'));
  }
}