View source
<?php
namespace Drupal\spamspan;
use Drupal\Core\Form\FormStateInterface;
trait SpamspanSettingsFormTrait {
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['spamspan_at'] = [
'#type' => 'textfield',
'#title' => $this
->t('Replacement for "@"'),
'#default_value' => $this
->getSetting('spamspan_at'),
'#required' => TRUE,
'#description' => $this
->t('Replace "@" with this text when javascript is disabled.'),
];
$element['spamspan_use_graphic'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use a graphical replacement for "@"'),
'#default_value' => $this
->getSetting('spamspan_use_graphic'),
'#description' => $this
->t('Replace "@" with a graphical representation when javascript is disabled (and ignore the setting "Replacement for @" above).'),
];
$element['spamspan_dot_enable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Replace dots in email with text'),
'#default_value' => $this
->getSetting('spamspan_dot_enable'),
'#description' => $this
->t('Switch on dot replacement.'),
];
$element['spamspan_dot'] = [
'#type' => 'textfield',
'#title' => $this
->t('Replacement for "."'),
'#default_value' => $this
->getSetting('spamspan_dot'),
'#required' => TRUE,
'#description' => $this
->t('Replace "." with this text.'),
];
$element['use_form'] = [
'#type' => 'details',
'#title' => $this
->t('Use a form instead of a link'),
'#open' => TRUE,
];
$element['use_form']['spamspan_use_form'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use a form instead of a link'),
'#default_value' => $this
->getSetting('spamspan_use_form'),
'#description' => $this
->t('Link to a contact form instead of an email address. The following settings are used only if you select this option.'),
];
$element['use_form']['spamspan_form_pattern'] = [
'#type' => 'textfield',
'#title' => $this
->t('Replacement string for the email address'),
'#default_value' => $this
->getSetting('spamspan_form_pattern'),
'#required' => TRUE,
'#description' => $this
->t('Replace the email link with this string and substitute the following: <br />%url = the url where the form resides,<br />%email = the email address (base64 and urlencoded),<br />%displaytext = text to display instead of the email address.'),
];
$element['use_form']['spamspan_form_default_url'] = [
'#type' => 'textfield',
'#title' => $this
->t('Default url'),
'#default_value' => $this
->getSetting('spamspan_form_default_url'),
'#required' => TRUE,
'#description' => $this
->t('Default url to form to use if none specified (e.g. me@example.com[custom_url_to_form])'),
];
$element['use_form']['spamspan_form_default_displaytext'] = [
'#type' => 'textfield',
'#title' => $this
->t('Default displaytext'),
'#default_value' => $this
->getSetting('spamspan_form_default_displaytext'),
'#required' => TRUE,
'#description' => $this
->t('Default displaytext to use if none specified (e.g. me@example.com[custom_url_to_form|custom_displaytext])'),
];
$element['#process'] = [
[
$this,
'processSettingsForm',
],
];
return $element;
}
public function getSetting($key) {
if (method_exists($this, 'mergeDefaults') && !$this->defaultSettingsMerged && !array_key_exists($key, $this->settings)) {
$this
->mergeDefaults();
}
return isset($this->settings[$key]) ? $this->settings[$key] : NULL;
}
public function processSettingsForm(&$element, FormStateInterface $form_state, &$complete_form) {
$complete_form['#validate'][] = [
$this,
'validateSettingsForm',
];
return $element;
}
public function validateSettingsForm(array &$form, FormStateInterface $form_state) {
$settings = $form_state
->getValue([
'filters',
'filter_spamspan',
'settings',
]);
$use_form = $settings['use_form'];
unset($settings['use_form']);
$settings += $use_form;
$form_state
->setValue([
'filters',
'filter_spamspan',
'settings',
], $settings);
}
}