View source
<?php
namespace Drupal\entity_share_cron\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_share_client\Entity\Remote;
use Drupal\entity_share_client\Service\RemoteManagerInterface;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SettingsForm extends ConfigFormBase {
protected $remoteManager;
public function __construct(ConfigFactoryInterface $config_factory, RemoteManagerInterface $remote_manager) {
parent::__construct($config_factory);
$this->remoteManager = $remote_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_share_client.remote_manager'));
}
public function getFormId() {
return 'entity_share_cron_settings';
}
public function getEditableConfigNames() {
return [
'entity_share_cron.settings',
];
}
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] : [];
$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_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);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$remotes = $form_state
->getValue('remotes');
foreach ($remotes as $remote_id => $remote_config) {
if (!empty($remote_config['enabled'])) {
$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;
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'],
]));
}
}
}
if (!$channel_enabled) {
$element =& $form['remotes'][$remote_id];
$form_state
->setError($element, $this
->t('No channels enabled for remote %remote.', [
'%remote' => $element['enabled']['#title'],
]));
}
}
}
}
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);
}
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'];
}
}