View source
<?php
namespace Drupal\prod_check\Plugin\ProdCheck\Settings;
use Drupal\Core\Form\FormStateInterface;
use Drupal\prod_check\Plugin\ProdCheck\ProdCheckBase;
use Drupal\user\UserInterface;
class UserRegister extends ProdCheckBase {
protected $current;
protected $options;
public function init() {
$this->current = $this->configFactory
->get('user.settings')
->get('register');
$this->options = [
UserInterface::REGISTER_ADMINISTRATORS_ONLY => $this
->t('Administrators only'),
UserInterface::REGISTER_VISITORS => $this
->t('Visitors'),
UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => $this
->t('Visitors, but administrator approval is required'),
];
}
public function state() {
$options = $this->configuration['options'];
return !empty($options[$this->current]);
}
public function successMessages() {
return [
'value' => $this->options[$this->current],
'description' => $this
->generateDescription($this
->title(), 'entity.user.admin_form'),
];
}
public function failMessages() {
$link_array = $this
->generateLinkArray($this
->title(), 'entity.user.admin_form');
return [
'value' => $this->options[$this->current],
'description' => $this
->t('Your %link settings are set to "@current". Are you sure this is what you want and did not mean to use @options? With improperly setup access rights, this can be dangerous...', [
'%link' => implode($link_array),
'@current' => $this->options[$this->current],
'@options' => '"' . implode('" ' . t('or') . ' "', $this
->getSelectedOptions()) . '"',
]),
];
}
public function defaultConfiguration() {
$configuration = parent::defaultConfiguration();
$configuration['options'] = [
UserInterface::REGISTER_ADMINISTRATORS_ONLY => UserInterface::REGISTER_ADMINISTRATORS_ONLY,
UserInterface::REGISTER_VISITORS => 0,
UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
];
return $configuration;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['options'] = [
'#type' => 'checkboxes',
'#title' => t('Valid user registration options'),
'#default_value' => $this->configuration['options'],
'#options' => $this->options,
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['options'] = $form_state
->getValue('options');
}
protected function getSelectedOptions() {
$selected_options = [];
foreach ($this->configuration['options'] as $option) {
if (!empty($option)) {
$selected_options[] = $this->options[$option];
}
}
return $selected_options;
}
}