class SettingsForm in Opigno Moxtra 8
Same name and namespace in other branches
- 3.x src/Form/SettingsForm.php \Drupal\opigno_moxtra\Form\SettingsForm
Implements the Opigno Moxtra settings 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\opigno_moxtra\Form\SettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SettingsForm
1 string reference to 'SettingsForm'
File
- src/
Form/ SettingsForm.php, line 20
Namespace
Drupal\opigno_moxtra\FormView source
class SettingsForm extends ConfigFormBase {
/**
* Batch options.
*
* @var array
*/
protected $batch;
/**
* The keyvalue storage.
*
* @var \Drupal\Core\KeyValueStore\KeyValueFactory
*/
protected $keyValueStorage;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $request;
/**
* Time.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructs a SettingsForm object.
*/
public function __construct(ConfigFactoryInterface $config_factory, TimeInterface $time, KeyValueFactory $key_value, RequestStack $request_stack) {
parent::__construct($config_factory);
$this->time = $time;
$this->keyValueStorage = $key_value
->get('opigno_moxtra');
$this->request = $request_stack;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('datetime.time'), $container
->get('keyvalue'), $container
->get('request_stack'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'opigno_moxtra_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'opigno_moxtra.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('opigno_moxtra.settings');
$org_id = $config
->get('org_id');
$form['content'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'row',
],
],
];
$form['content']['left'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'col-4',
],
],
];
$form['content']['right'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'col-8',
],
],
];
$form['content']['right']['title'] = [
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => $this
->t('Moxtra API Settings'),
];
if (empty($org_id)) {
$form['content']['left']['text'] = [
'#type' => 'container',
'text' => [
'#markup' => $this
->t('You need to register on'),
],
];
$renew_title = $this
->t('Moxtra');
$renew_uri = 'https://moxtra.com/';
$renew_url = Url::fromUri($renew_uri)
->setOptions([
'target' => '_blank',
]);
$renew = Link::fromTextAndUrl($renew_title, $renew_url)
->toRenderable();
$form['content']['left']['renew'] = $renew;
}
else {
$form['content']['left']['text'] = [
'#type' => 'container',
'text' => [
'#markup' => $this
->t('Add all users to Moxtra'),
],
];
$form['content']['left']['text']['add'] = [
'#type' => 'submit',
'#attributes' => [
'name' => 'add',
],
'#value' => $this
->t('Register all'),
'#submit' => [
[
$this,
'registerAllSubmit',
],
],
];
}
$form['content']['right']['info'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'moxtra-info',
'col-8',
],
],
];
$form['content']['right']['info']['moxtra_url'] = [
'#type' => 'textfield',
'#title' => $this
->t('Your Moxtra URL'),
'#default_value' => $config
->get('url'),
'#description' => $this
->t('Example: @url', [
'@url' => 'https://opigno-dev.moxtra.com',
]),
];
$form['content']['right']['info']['org_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Organization ID'),
'#default_value' => $config
->get('org_id'),
];
$form['content']['right']['info']['client_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Client ID'),
'#default_value' => $config
->get('client_id'),
];
$form['content']['right']['info']['client_secret'] = [
'#type' => 'password',
'#title' => $this
->t('Client Secret'),
'#default_value' => $config
->get('client_secret'),
'#post_render' => [
[
$this,
'rebuildPass',
],
],
];
$form['content']['right']['info']['moxtra_login'] = [
'#type' => 'textfield',
'#title' => $this
->t('Email'),
'#default_value' => $config
->get('email'),
];
$form['content']['right']['info']['agreement'] = [
'#type' => 'checkbox',
'#required' => TRUE,
'#title' => $this
->t('I understand and expressly Consent to the transfer of the following data to Moxtra Inc., a company located in the United States of America : URL of my website, email address of the website administrator, name of the website, total number of users of the website and my username, ID and timezone'),
'#wrapper_attributes' => [
'class' => [
'inline-checkbox',
],
],
'#default_value' => $config
->get('agreement'),
];
$form['content']['right']['info']['save'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#submit' => [
[
$this,
'settingsSubmitForm',
],
],
];
$form['#attached'] = [
'library' => [
'opigno_moxtra/settings_form',
],
];
$form['#attached']['library'][] = 'opigno_moxtra/settings_form';
$form['#attached']['library'][] = 'opigno_moxtra/moxtra.js';
return $form;
}
/**
* Rebuild moxtra client secret field.
*/
public function rebuildPass($field, $form) {
$default_value = isset($form['#default_value']) ? $form['#default_value'] : NULL;
$field = $field
->__toString();
$field = str_replace('type="password"', 'type="password" value="' . $default_value . '" ', $field);
return $field;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function settingsSubmitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = \Drupal::configFactory()
->getEditable('opigno_moxtra.settings');
$config
->setData([]);
$config
->save();
$connector = \Drupal::service('opigno_moxtra.connector');
$token = $connector
->getToken(1, TRUE);
$this
->config('opigno_moxtra.settings')
->set('url', $form_state
->getValue('moxtra_url'))
->set('client_id', $form_state
->getValue('client_id'))
->set('client_secret', $form_state
->getValue('client_secret'))
->set('email', $form_state
->getValue('moxtra_login'))
->set('org_id', $form_state
->getValue('org_id'))
->set('agreement', $form_state
->getValue('agreement'))
->set('status', !empty($token))
->save();
$prefix = $this->keyValueStorage
->get('prefix');
if (empty($prefix)) {
$prefix = $this->request
->getCurrentRequest()
->getHost();
$this->keyValueStorage
->set('prefix', $prefix);
}
}
public function registerAllSubmit(array &$form, FormStateInterface $form_state) {
$this
->setBatch();
}
/**
* Set operation.
*
* @param array $data
* Process data.
*/
public function setOperation(array $data) {
$this->batch['operations'][] = [
[
$this,
'processItem',
],
$data,
];
}
/**
* Process item of batch.
*
* @param array $data
* The mail of new user.
* Master user.
* @param array $context
* Context.
*/
public static function processItem($item, array &$context) {
$moxtra = \Drupal::service('opigno_moxtra.moxtra_api');
if (empty($context['results'])) {
$context['results'] = [];
}
if (!empty($item->uid)) {
$account = User::load($item->uid);
$moxtra
->setUser($account);
}
$context['results'][] = $item->uid;
}
/**
* Set the batch.
*/
public function setBatch() {
$database = \Drupal::database();
$query = $database
->select('users', 'u');
$query
->fields('u', [
'uid',
]);
$users = $query
->execute()
->fetchAll();
$batch_builder = (new BatchBuilder())
->setTitle($this
->t('Register Moxtra users'))
->setFinishCallback([
$this,
'finishBatch',
]);
foreach ($users as $key => $user) {
$batch_builder
->addOperation([
$this,
'processItem',
], [
'users' => $user,
]);
}
batch_set($batch_builder
->toArray());
}
/**
* Finished callback of batch api.
*
* @param bool $success
* Success or not.
* @param array $results
* The results.
* @param array $operations
* The operations.
*/
public function finishBatch(bool $success, array $results, array $operations) {
if ($success) {
$message = \Drupal::translation()
->formatPlural(count($results), 'One user processed.', '@count users processed.');
}
else {
$message = $this
->t('Finished with an error.');
}
\Drupal::messenger()
->addMessage($message);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
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. | |
SettingsForm:: |
protected | property | Batch options. | |
SettingsForm:: |
protected | property | The keyvalue storage. | |
SettingsForm:: |
protected | property | The request stack. | |
SettingsForm:: |
protected | property | Time. | |
SettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | function | Finished callback of batch api. | |
SettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SettingsForm:: |
public static | function | Process item of batch. | |
SettingsForm:: |
public | function | Rebuild moxtra client secret field. | |
SettingsForm:: |
public | function | ||
SettingsForm:: |
public | function | Set the batch. | |
SettingsForm:: |
public | function | Set operation. | |
SettingsForm:: |
public | function | ||
SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
SettingsForm:: |
public | function |
Constructs a SettingsForm object. Overrides ConfigFormBase:: |
|
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. |