ChartsConfigForm.php in Charts 5.0.x
File
src/Form/ChartsConfigForm.php
View source
<?php
namespace Drupal\charts\Form;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ChartsConfigForm extends ConfigFormBase {
protected $config;
protected $cacheTagsInvalidator;
public function __construct(ConfigFactoryInterface $config_factory, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
parent::__construct($config_factory);
$this->config = $this->configFactory
->getEditable('charts.settings');
$this->cacheTagsInvalidator = $cache_tags_invalidator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('cache_tags.invalidator'));
}
public function getFormId() {
return 'charts_form_base';
}
protected function getEditableConfigNames() {
return [
'charts.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$default_config = $this->config
->get('charts_default_settings') ?: [];
$form['help'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this
->t('The settings on this page are used to set
<strong>default</strong> settings. They do not affect existing charts.
To make a new chart, create a new view and select the display format of
"Chart." Or use a Charts Block and add your own data inside that block.
You can also attach a Chart field to your content (or other entity)
type and add your data within the Chart field.'),
];
$form['settings'] = [
'#type' => 'charts_settings',
'#used_in' => 'config_form',
'#required' => TRUE,
'#default_value' => $default_config,
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$settings = $form_state
->getValue('settings');
if (isset($settings['defaults'])) {
unset($settings['defaults']);
}
$this->config
->set('charts_default_settings', $settings);
$this->config
->save();
$this->cacheTagsInvalidator
->invalidateTags($this->config
->getCacheTags());
parent::submitForm($form, $form_state);
}
}