class SettingsForm in Entity Share Cron 8
Same name and namespace in other branches
- 8.2 src/Form/SettingsForm.php \Drupal\entity_share_cron\Form\SettingsForm
- 3.0.x src/Form/SettingsForm.php \Drupal\entity_share_cron\Form\SettingsForm
Module 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\entity_share_cron\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 16
Namespace
Drupal\entity_share_cron\FormView source
class SettingsForm extends ConfigFormBase {
/**
* Remote manager service.
*
* @var \Drupal\entity_share_client\Service\RemoteManagerInterface
*/
protected $remoteManager;
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\entity_share_client\Service\RemoteManagerInterface $remote_manager
* Remote manager service.
*/
public function __construct(ConfigFactoryInterface $config_factory, RemoteManagerInterface $remote_manager) {
parent::__construct($config_factory);
$this->remoteManager = $remote_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_share_client.remote_manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'entity_share_cron_settings';
}
/**
* {@inheritdoc}
*/
public function getEditableConfigNames() {
return [
'entity_share_cron.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('entity_share_cron.settings');
$form['cron_interval'] = [
'#type' => 'number',
'#title' => $this
->t('Execution interval'),
'#description' => $this
->t('Minimum interval between consecutive executions in seconds.'),
'#min' => 60,
'#step' => 60,
'#default_value' => $config
->get('cron_interval'),
];
$form['remotes'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Enabled remotes and channels'),
'#description' => $this
->t('Only selected remotes and channels will be synchronized on Cron executions. For each channel you may select which operations can be performed on synchronization.'),
'#tree' => TRUE,
];
$remotes_config = $config
->get('remotes');
foreach (Remote::loadMultiple() as $remote_id => $remote) {
$remote_config = isset($remotes_config[$remote_id]) ? $remotes_config[$remote_id] : [];
// Adds a checkbox to enable/disable remote synchronization.
$form['remotes'][$remote_id] = [
'#type' => 'container',
];
$remote_enabled_default = !empty($remote_config['enabled']);
$form['remotes'][$remote_id]['enabled'] = [
'#type' => 'checkbox',
'#title' => $remote
->label(),
'#default_value' => $remote_enabled_default,
'#ajax' => [
'callback' => [
$this,
'remoteCheckboxCallback',
],
'wrapper' => "channels-{$remote_id}",
],
];
$form['remotes'][$remote_id]['channels'] = [
'#type' => 'container',
'#prefix' => '<div id="channels-$remote_id" class="channels-container">',
'#suffix' => '</div>',
];
$remote_enabled = $form_state
->getValue([
'remotes',
$remote_id,
'enabled',
]);
if (!isset($remote_enabled) && $remote_enabled_default || $remote_enabled) {
try {
$channels = $this->remoteManager
->getChannelsInfos($remote);
} catch (ClientException $e) {
$channels = [];
\watchdog_exception('entity_share_cron', $e);
\drupal_set_message($this
->t('Could not get channels from remote %remote.', [
'%remote' => $remote
->label(),
]), 'error');
}
foreach ($channels as $channel_id => $channel_info) {
// Channel settings.
$channel_config = isset($remote_config['channels'][$channel_id]) ? $remote_config['channels'][$channel_id] : [];
$form['remotes'][$remote_id]['channels'][$channel_id] = [
'#type' => 'container',
];
$form['remotes'][$remote_id]['channels'][$channel_id]['enabled'] = [
'#type' => 'checkbox',
'#title' => $channel_info['label'],
'#default_value' => !empty($channel_config['enabled']),
];
$form['remotes'][$remote_id]['channels'][$channel_id]['operations'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'channel-operations',
],
],
'#states' => [
'visible' => [
':input[name="remotes[$remote_id][channels][$channel_id][enabled]"]' => [
'checked' => TRUE,
],
],
],
];
$form['remotes'][$remote_id]['channels'][$channel_id]['operations']['create'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Create'),
'#default_value' => isset($channel_config['operations']['create']) ? $channel_config['operations']['create'] : TRUE,
];
$form['remotes'][$remote_id]['channels'][$channel_id]['operations']['update'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Update'),
'#default_value' => isset($channel_config['operations']['update']) ? $channel_config['operations']['update'] : TRUE,
];
$form['remotes'][$remote_id]['channels'][$channel_id]['url'] = [
'#type' => 'hidden',
'#value' => $channel_info['url'],
];
$form['remotes'][$remote_id]['channels'][$channel_id]['url_uuid'] = [
'#type' => 'hidden',
'#value' => $channel_info['url_uuid'],
];
}
}
}
$form['#attached']['library'][] = 'entity_share_cron/settings_form';
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Checks if every enabled remote has at least one channel enabled.
$remotes = $form_state
->getValue('remotes');
foreach ($remotes as $remote_id => $remote_config) {
if (!empty($remote_config['enabled'])) {
// Searches for an enabled channel.
$channel_enabled = FALSE;
$channels = isset($remote_config['channels']) ? $remote_config['channels'] : [];
foreach ($channels as $channel_id => $channel_config) {
if (!empty($channel_config['enabled'])) {
$channel_enabled = TRUE;
// Checks if at least one operation is enabled.
if (!array_filter($channel_config['operations'])) {
$element =& $form['remotes'][$remote_id]['channels'][$channel_id];
$form_state
->setError($element, $this
->t('No operations enabled for channel %channel of remote %remote.', [
'%channel' => $element['enabled']['#title'],
'%remote' => $form['remotes'][$remote_id]['enabled']['#title'],
]));
}
}
}
// Shows an error if no enabled channel could be found.
if (!$channel_enabled) {
$element =& $form['remotes'][$remote_id];
$form_state
->setError($element, $this
->t('No channels enabled for remote %remote.', [
'%remote' => $element['enabled']['#title'],
]));
}
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this
->config('entity_share_cron.settings')
->set('cron_interval', $form_state
->getValue('cron_interval'))
->set('remotes', $form_state
->getValue('remotes'))
->save();
parent::submitForm($form, $form_state);
}
/**
* Ajax callback for the remotes' checkboxes.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return array
* A form element.
*/
public function remoteCheckboxCallback(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
$remote_id = $triggering_element['#parents'][1];
return $form['remotes'][$remote_id]['channels'];
}
}
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 | Remote manager service. | |
SettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | 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 | function | Ajax callback for the remotes' checkboxes. | |
SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
SettingsForm:: |
public | function |
Constructs a \Drupal\system\ConfigFormBase 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. |