class SchemeForm in Taxonomy Access Control Lite 8
Builds the scheme configuration form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\tac_lite\Form\SchemeForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SchemeForm
3 files declare their use of SchemeForm
- tac_lite.module in ./
tac_lite.module - Control access to site content based on taxonomy, roles and users.
- tac_lite_create.module in tac_lite_create/
tac_lite_create.module - Control term visibility in node edit form.
- UserAccessForm.php in src/
Form/ UserAccessForm.php
File
- src/
Form/ SchemeForm.php, line 14
Namespace
Drupal\tac_lite\FormView source
class SchemeForm extends ConfigFormBase {
private $scheme;
/**
* {@inheritdoc}
*/
public function getEditableConfigNames() {
return [
'tac_lite.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'tac_lite_admin_scheme_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $scheme = NULL) {
$settings = $this->configFactory
->get('tac_lite.settings');
$this->scheme = $scheme;
$vids = $settings
->get('tac_lite_categories');
$roles = user_roles();
$config = self::tacLiteConfig($scheme);
$form['#tac_lite_config'] = $config;
if (count($vids)) {
$form['tac_lite_config_scheme_' . $scheme] = [
'#tree' => TRUE,
];
$form['tac_lite_config_scheme_' . $scheme]['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Scheme name'),
'#description' => $this
->t("A human-readable name for administrators to see. For example, 'read' or 'read and write'."),
'#default_value' => $config['name'],
'#required' => TRUE,
];
// Currently, only view, update and delete are supported by node_access.
$options = [
'grant_view' => 'view',
'grant_update' => 'update',
'grant_delete' => 'delete',
];
$form['tac_lite_config_scheme_' . $scheme]['perms'] = [
'#type' => 'select',
'#title' => $this
->t('Permissions'),
'#multiple' => TRUE,
'#options' => $options,
'#default_value' => $config['perms'],
'#description' => $this
->t('Select which permissions are granted by this scheme. <br/>Note when granting update, it is best to enable visibility on all terms. Otherwise a user may unknowingly remove invisible terms while editing a node.'),
'#required' => FALSE,
];
$form['tac_lite_config_scheme_' . $scheme]['unpublished'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Apply to unpublished content'),
'#description' => $this
->t('If checked, permissions in this scheme will apply to unpublished content. If this scheme includes the view permission, then <strong>unpublished nodes will be visible</strong> to users whose roles would grant them access to the published node.'),
'#default_value' => $config['unpublished'],
];
$form['tac_lite_config_scheme_' . $scheme]['term_visibility'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Visibility'),
'#description' => $this
->t('If checked, this scheme determines whether a user can view <strong>terms</strong>. Note the <em>view</em> permission in the select field above refers to <strong>node</strong> visibility. This checkbox refers to <strong>term</strong> visibility, for example in a content edit form or tag cloud.'),
'#default_value' => $config['term_visibility'],
];
$form['helptext'] = [
'#type' => 'markup',
'#markup' => $this
->t('To grant to an individual user, visit the <em>access by taxonomy</em> tab on the account edit page.'),
'#prefix' => '<p>',
'#suffix' => '</p>',
];
$form['helptext2'] = [
'#type' => 'markup',
'#markup' => $this
->t('To grant by role, select the terms below.'),
'#prefix' => '<p>',
'#suffix' => '</p>',
];
$vocabularies = Vocabulary::loadMultiple();
$all_defaults = $settings
->get('tac_lite_grants_scheme_' . $scheme);
$form['tac_lite_grants_scheme_' . $scheme] = [
'#tree' => TRUE,
];
foreach ($roles as $rid => $role) {
$role_name = $role
->get('label');
$form['tac_lite_grants_scheme_' . $scheme][$rid] = [
'#type' => 'details',
'#tree' => TRUE,
'#title' => Html::escape($this
->t('Grant permission by role: :role', [
':role' => $role_name,
])),
'#open' => TRUE,
];
$defaults = isset($all_defaults[$rid]) ? $all_defaults[$rid] : NULL;
foreach ($vids as $vid) {
// Build a taxonomy select form element for this vocab.
$v = $vocabularies[$vid];
$default_values = isset($defaults[$vid]) ? $defaults[$vid] : NULL;
$form['tac_lite_grants_scheme_' . $scheme][$rid][$vid] = self::tacLiteTermSelect($v, $default_values);
}
}
$form['tac_lite_rebuild'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Rebuild content permissions now'),
// Default false because usually only needed after scheme has
// been changed.
'#default_value' => FALSE,
'#description' => $this
->t('Do this once, after you have fully configured access by taxonomy.'),
'#weight' => 9,
];
}
else {
$form['tac_lite_help'] = [
'#type' => 'markup',
'#prefix' => '<p>',
'#suffix' => '</p>',
'#markup' => $this
->t('First, select one or more vocabularies on the <a href=:url>settings tab</a>. Then, return to this page to complete configuration.', [
':url' => Url::fromRoute('tac_lite.administration')
->toString(),
]),
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state, $scheme = NULL) {
$scheme = $this->scheme;
$values = $form_state
->getValues();
$this
->config('tac_lite.settings')
->set('menu_rebuild_needed', TRUE)
->set('tac_lite_config_scheme_' . $scheme, $values['tac_lite_config_scheme_' . $scheme])
->set('tac_lite_grants_scheme_' . $scheme, $values['tac_lite_grants_scheme_' . $scheme])
->save();
if ($values['tac_lite_rebuild']) {
node_access_rebuild(TRUE);
}
else {
$this
->messenger()
->addWarning($this
->t('Do not forget to <a href=:url>rebuild node access permissions</a> after you have configured taxonomy-based access.', [
':url' => Url::fromRoute('node.configure_rebuild_confirm')
->toString(),
]));
}
parent::submitForm($form, $form_state);
}
/**
* Helper function to get configuration of scheme.
*/
public static function tacLiteConfig($scheme) {
$settings = \Drupal::config('tac_lite.settings');
$config = $settings
->get('tac_lite_config_scheme_' . $scheme);
$config['name'] = !empty($config['name']) ? $config['name'] : NULL;
$config['perms'] = !empty($config['perms']) ? $config['perms'] : [];
$config += [
'term_visibility' => isset($config['perms']['grant_view']) && $config['perms']['grant_view'],
'unpublished' => FALSE,
'realm' => 'tac_lite_scheme_' . $scheme,
];
return $config;
}
/**
* Helper function to build a taxonomy term select element for a form.
*
* @param object $v
* A vocabulary object containing a vid and name.
* @param array $default_values
* An array of values to use for the default_value argument for this
* form element.
*/
public static function tacLiteTermSelect($v, $default_values = []) {
$tree = \Drupal::service('entity_type.manager')
->getStorage('taxonomy_term')
->loadTree($v
->get('vid'));
$options = [
0 => '<none>',
];
if ($tree) {
foreach ($tree as $term) {
$choice = new \stdClass();
$choice->option = [
$term->tid => str_repeat('-', $term->depth) . $term->name,
];
$options[] = $choice;
}
}
$field_array = [
'#type' => 'select',
'#title' => $v
->get('name'),
'#default_value' => $default_values,
'#options' => $options,
'#multiple' => TRUE,
'#description' => $v
->get('description'),
];
return $field_array;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SchemeForm:: |
private | property | ||
SchemeForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SchemeForm:: |
public | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SchemeForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SchemeForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SchemeForm:: |
public static | function | Helper function to get configuration of scheme. | |
SchemeForm:: |
public static | function | Helper function to build a taxonomy term select element for a form. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |