You are here

class ConfigForm in Nodeaccess 8

Same name and namespace in other branches
  1. 8.2 src/Form/ConfigForm.php \Drupal\nodeaccess\Form\ConfigForm

Builds the configuration form.

Hierarchy

Expanded class hierarchy of ConfigForm

1 string reference to 'ConfigForm'
nodeaccess.routing.yml in ./nodeaccess.routing.yml
nodeaccess.routing.yml

File

src/Form/ConfigForm.php, line 13

Namespace

Drupal\nodeaccess\Form
View source
class ConfigForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getEditableConfigNames() {
    return [
      'nodeaccess.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'nodeaccess_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $settings = $this
      ->config('nodeaccess.settings');
    $role_alias = $settings
      ->get('role_alias');
    $allowed_grants = $settings
      ->get('grants');
    $allowed_types = $settings
      ->get('allowed_types');
    $role_header = [
      $this
        ->t('Allow Role'),
      $this
        ->t('Alias'),
      $this
        ->t('Weight'),
    ];
    $header = [
      $this
        ->t('ROLE'),
      $this
        ->t('VIEW'),
      $this
        ->t('EDIT'),
      $this
        ->t('DELETE'),
    ];
    $node_types = NodeType::loadMultiple();
    $roles = user_roles();
    $form['priority'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Give node grants priority'),
      '#default_value' => $settings
        ->get('priority'),
      '#description' => $this
        ->t('If you are only using this access control module, you can safely ignore this. If you are using multiple access control modules, and you want the grants given on individual nodes to override any grants given by other modules, you should check this box.'),
    ];

    // Select whether to preserve hidden grants.
    $form['preserve'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Preserve hidden grants'),
      '#default_value' => $settings
        ->get('preserve'),
      '#description' => '<small>' . $this
        ->t('If you check this box, any hidden grants are preserved when you save grants. Otherwise all grants users are not allowed to view or edit are revoked on save.') . '</small>',
    ];

    // Select permissions you want to allow users to view and edit.
    $form['grants'] = [
      '#type' => 'details',
      '#open' => FALSE,
      '#title' => $this
        ->t('Allowed Grants'),
      '#tree' => TRUE,
      '#description' => '<small>' . $this
        ->t('The selected grants will be listed on individual node grants. If you wish for certain grants to be hidden from users on the node grants tab, make sure they are not selected here.') . '</small>',
    ];
    $form['grants']['view'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('View'),
      '#default_value' => $allowed_grants['view'],
    ];
    $form['grants']['edit'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Edit'),
      '#default_value' => $allowed_grants['edit'],
    ];
    $form['grants']['delete'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Delete'),
      '#default_value' => $allowed_grants['delete'],
    ];

    // Select roles the permissions of which you want to allow users to
    // view and edit, and the aliases and weights of those roles.
    $form['role'] = [
      '#type' => 'details',
      '#open' => FALSE,
      '#title' => $this
        ->t('Allowed Roles'),
      '#tree' => TRUE,
      '#description' => $this
        ->t('The selected roles will be listed on individual node grants. If you wish for certain roles to be hidden from users on the node grants tab, make sure they are not selected here. You may also provide an alias for each role to be displayed to the user and a weight to order them by. This is useful if your roles have machine-readable names not intended for human users.'),
    ];
    $form['role']['alias'] = [
      '#type' => 'table',
      '#header' => $role_header,
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'role-weight',
        ],
      ],
    ];
    foreach ($role_alias as $id => $role) {
      $form['role']['alias'][$id]['allow'] = [
        '#type' => 'checkbox',
        '#title' => HTML::escape($role['name']),
        '#default_value' => $role['allow'],
      ];
      $form['role']['alias'][$id]['alias'] = [
        '#type' => 'textfield',
        '#default_value' => $role['alias'],
        '#size' => 50,
        '#maxlength' => 50,
      ];
      $form['role']['alias'][$id]['weight'] = [
        '#type' => 'weight',
        '#default_value' => $role['weight'],
        '#delta' => 10,
        '#attributes' => [
          'class' => [
            'role-weight',
          ],
        ],
      ];
      $form['role']['alias'][$id]['name'] = [
        '#type' => 'hidden',
        '#value' => $role['name'],
      ];
      $form['role']['alias'][$id]['#weight'] = $role['weight'];
      $form['role']['alias'][$id]['#attributes']['class'][] = 'draggable';
    }

    // Generate fieldsets for each node type.
    foreach ($node_types as $type => $bundle) {
      $user_perms = $settings
        ->get($type);
      $form[$type] = [
        '#type' => 'details',
        '#open' => FALSE,
        '#title' => $bundle
          ->label(),
        '#tree' => TRUE,
        '#description' => $this
          ->t('The settings selected for the node author will define what permissions the node author has. This cannot be changed on individual node grants.'),
      ];
      $form[$type]['show'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Show grant tab for this node type'),
        '#default_value' => isset($allowed_types[$type]) ? $allowed_types[$type] : 0,
      ];
      $form[$type]['user_permissions'] = [
        '#type' => 'table',
        '#header' => $header,
      ];

      // Set default role permissions for node type.
      foreach ($roles as $id => $role) {
        $form[$type]['user_permissions'][$id] = [
          'label' => [
            '#plain_text' => $role
              ->label(),
          ],
          'grant_view' => [
            '#type' => 'checkbox',
            '#default_value' => isset($user_perms[$id]['grant_view']) ? $user_perms[$id]['grant_view'] : 0,
          ],
          'grant_update' => [
            '#type' => 'checkbox',
            '#default_value' => isset($user_perms[$id]['grant_update']) ? $user_perms[$id]['grant_update'] : 0,
          ],
          'grant_delete' => [
            '#type' => 'checkbox',
            '#default_value' => isset($user_perms[$id]['grant_delete']) ? $user_perms[$id]['grant_delete'] : 0,
          ],
        ];
      }
      $form[$type]['user_permissions']['author'] = [
        'label' => [
          '#plain_text' => $this
            ->t('Author'),
        ],
        'grant_view' => [
          '#type' => 'checkbox',
          '#default_value' => isset($user_perms['author']['grant_view']) ? $user_perms['author']['grant_view'] : 0,
        ],
        'grant_update' => [
          '#type' => 'checkbox',
          '#default_value' => isset($user_perms['author']['grant_update']) ? $user_perms['author']['grant_update'] : 0,
        ],
        'grant_delete' => [
          '#type' => 'checkbox',
          '#default_value' => isset($user_perms['author']['grant_delete']) ? $user_perms['author']['grant_delete'] : 0,
        ],
      ];
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Update configuration.
    $values = $form_state
      ->getValues();
    $node_types = NodeType::loadMultiple();
    $allowed_types = [];
    $settings = $this
      ->config('nodeaccess.settings')
      ->set('priority', $values['priority'])
      ->set('preserve', $values['preserve'])
      ->set('grants', $values['grants']);
    foreach ($node_types as $type => $bundle) {
      $config = $values[$type]['user_permissions'];
      $allowed_types[$type] = $values[$type]['show'];
      $settings
        ->set($type, $config);
    }
    $settings
      ->set('allowed_types', $allowed_types);

    // Save allowed roles, role aliases and weights.
    $settings
      ->set('role_alias', $values['role']['alias']);
    $settings
      ->save();
    node_access_needs_rebuild(TRUE);
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ConfigForm::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ConfigForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ConfigForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.