easy_social-8.x-3.x-dev/contrib/easy_social_example/src/Form/SettingsForm.php
contrib/easy_social_example/src/Form/SettingsForm.php
<?php
namespace Drupal\easy_social_example\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure user settings for this site.
*/
final class SettingsForm extends ConfigFormBase {
/**
* Config settings.
*
* @var string
*/
private const SETTINGS = 'easy_social.example';
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return [self::SETTINGS];
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'easy_social_example_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->configFactory->get(self::SETTINGS);
$form['help'] = [
'#markup' => t('For more information, please check out the official @example share widget <a href="@url" target="_blank">documentation</a>', ['@example' => t('Example'), '@url' => '#']),
'#weight' => -99,
];
$form['size'] = [
'#type' => 'checkbox',
'#title' => t('Large button'),
'#default_value' => $config->get('size'),
];
$form['foo'] = [
'#type' => 'textfield',
'#title' => t('Foo'),
'#default_value' => $config->get('foo'),
];
$form['baz'] = [
'#type' => 'checkbox',
'#title' => t('Baz'),
'#default_value' => $config->get('baz'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
parent::submitForm($form, $form_state);
$this->configFactory->get(self::SETTINGS)
->set('size', $form_state['values']['size'])
->set('foo', $form_state['values']['foo'])
->set('baz', $form_state['values']['baz'])
->save();
}
}
