View source
<?php
namespace Drupal\easy_social\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Plugin\Context\ContextInterface;
use Drupal\Core\Extension\ModuleHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
class EasySocialSettingsForm extends ConfigFormBase {
protected $moduleHandler;
public function __construct(ConfigFactory $config_factory, ModuleHandler $module_handler) {
parent::__construct($config_factory);
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('module_handler'));
}
public function getFormID() {
return 'easy_social_settings';
}
protected function getEditableConfigNames() {
return [
'easy_social.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') {
$config = $this
->config('easy_social.settings');
if ($widgets = easy_social_get_widgets()) {
$widget_options = array();
foreach ($widgets as $machine_name => $widget) {
$widget_options[$machine_name] = $widget['name'];
}
$form['widgets'] = array(
'#type' => 'checkboxes',
'#title' => t('Enabled widgets'),
'#description' => t('Select the social sharing widgets you would like to enable globally. This setting can be overriden per block and entity type.'),
'#default_value' => (array) $config
->get('global.widgets'),
'#options' => $widget_options,
);
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['advanced']['async'] = array(
'#type' => 'checkbox',
'#title' => t('Load javascript asynchronously'),
'#description' => t('This is recommended for performance purposes.'),
'#default_value' => $config
->get('global.async'),
);
}
else {
$form['widgets_empty'] = array(
'#prefix' => '<div class="empty">',
'#suffix' => '</div>',
'#markup' => t('There are no widgets defined.'),
);
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = $this
->config('easy_social.settings');
$config
->set('global.widgets', $form_state
->getValue('widgets'))
->set('global.async', $form_state
->getValue('async'))
->save();
}
}