class Settings in Avatar Kit 8
Configure avatar kit settings.
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\avatars\Form\Settings
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of Settings
2 string references to 'Settings'
- avatars.routing.yml in ./
avatars.routing.yml - avatars.routing.yml
- Settings::buildForm in src/
Form/ Settings.php - Form constructor.
File
- src/
Form/ Settings.php, line 17
Namespace
Drupal\avatars\FormView source
class Settings extends ConfigFormBase {
/**
* The avatar generator plugin manager.
*
* @var \Drupal\avatars\AvatarGeneratorPluginManagerInterface
*/
protected $avatarGenerator;
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\avatars\AvatarGeneratorPluginManagerInterface $avatar_generator
* The avatar generator plugin manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, AvatarGeneratorPluginManagerInterface $avatar_generator) {
parent::__construct($config_factory);
$this->avatarGenerator = $avatar_generator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.avatar_generator'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'avatars_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'avatars.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this
->config('avatars.settings');
// Define table.
$headers = [
'label' => $this
->t('Avatar Generator'),
'type' => $this
->t('Type'),
'plugin' => $this
->t('Plugin'),
'settings' => $this
->t('Settings'),
'enabled' => [
'data' => $this
->t('Enabled'),
'class' => [
'checkbox',
],
],
'weight' => $this
->t('Weight'),
'operations' => $this
->t('Operations'),
];
$form['avatar_generators_help'] = [
'#prefix' => '<p>',
'#markup' => $this
->t('A list of avatar generators to try for each user in order of preference.'),
'#suffix' => '</p>',
];
$form['avatar_generators'] = [
'#type' => 'table',
'#header' => $headers,
'#empty' => $this
->t('No avatar generators found.'),
'#attributes' => [
'id' => 'avatar-generators',
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'generator-weight',
],
],
];
/** @var \Drupal\avatars\AvatarGeneratorInterface[] $instances */
$instances = AvatarGenerator::loadMultiple();
uasort($instances, '\\Drupal\\avatars\\Entity\\AvatarGenerator::sort');
foreach ($instances as $instance) {
$form['avatar_generators'][$instance
->id()] = [];
$row =& $form['avatar_generators'][$instance
->id()];
$row['#attributes']['class'][] = 'draggable';
$definition = $instance
->getPlugin()
->getPluginDefinition();
$row['label']['#markup'] = $instance
->label();
$row['type']['#markup'] = $definition['dynamic'] ? $this
->t('Dynamic') : $this
->t('Static');
$row['plugin']['#markup'] = $definition['label'];
$row['settings'] = $instance
->getPlugin()
->settingsSummary();
$row['enabled'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enabled'),
'#title_display' => 'invisible',
'#default_value' => $instance
->status(),
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
],
];
$row['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight'),
'#title_display' => 'invisible',
'#default_value' => $instance
->getWeight(),
'#attributes' => [
'class' => [
'generator-weight',
],
],
];
$operations = [];
if ($instance
->access('update')) {
$operations['edit'] = [
'title' => $this
->t('Edit'),
'weight' => 10,
'url' => $instance
->toUrl('edit-form'),
];
}
if ($instance
->access('delete')) {
$operations['delete'] = [
'title' => $this
->t('Delete'),
'weight' => 100,
'url' => $instance
->toUrl('delete-form'),
];
}
$row['operations'] = [
'#type' => 'operations',
'#links' => $operations,
];
}
$form['refresh_interval']['#tree'] = TRUE;
$intervals = $config
->get('refresh_interval');
$form['refresh_interval']['dynamic'] = [
'#type' => 'number',
'#title' => $this
->t('Dynamic lifetime'),
'#description' => $this
->t('How long dynamic avatars are cached before allowing refresh.'),
'#default_value' => $intervals['dynamic'],
'#step' => 60,
'#min' => 0,
'#field_suffix' => $this
->t('seconds'),
];
/*
* Keep unused avatars on file system (will use up more disk space)
* Avatars must be purged manually if you change this settings.
* Expire unused static avatars (will use up more network bandwidth)
* */
$form['refresh_interval']['static'] = [
'#type' => 'unlimited_number',
'#title' => $this
->t('Static lifetime'),
'#description' => $this
->t('How long static avatars are cached. Only applies to avatars which are not the users preference.'),
'#default_value' => $intervals['static'] < 1 ? UnlimitedNumber::UNLIMITED : $intervals['static'],
'#step' => 60,
'#min' => 60,
'#field_suffix' => $this
->t('seconds'),
'#options' => [
'unlimited' => $this
->t('Never delete'),
'limited' => $this
->t('Delete after'),
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('avatars.settings');
// Generators are already sorted correctly.
foreach ($form_state
->getValue('avatar_generators') as $id => $row) {
/** @var \Drupal\avatars\AvatarGeneratorInterface $avatar_generator */
$avatar_generator = AvatarGenerator::load($id);
$avatar_generator
->setStatus($row['enabled'])
->setWeight($row['weight'])
->save();
}
Cache::invalidateTags([
'avatar_preview',
]);
$intervals = $form_state
->getValue('refresh_interval');
if ($intervals['static'] == UnlimitedNumber::UNLIMITED) {
$intervals['static'] = 0;
}
$config
->set('refresh_interval', [
'dynamic' => $intervals['dynamic'],
'static' => $intervals['static'],
]);
$config
->save();
$this
->messenger()
->addStatus(t('Settings saved.'));
}
}
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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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. | |
Settings:: |
protected | property | The avatar generator plugin manager. | |
Settings:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
Settings:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
Settings:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
Settings:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
Settings:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
Settings:: |
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. |