EmailSpamspanFormatter.php in SpamSpan filter 8.2
File
src/Plugin/Field/FieldFormatter/EmailSpamspanFormatter.php
View source
<?php
namespace Drupal\spamspan\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\spamspan\SpamspanSettingsFormTrait;
class EmailSpamspanFormatter extends FormatterBase {
use SpamspanSettingsFormTrait;
public static function defaultSettings() {
$plugin_manager = \Drupal::service('plugin.manager.filter');
$configuration = $plugin_manager
->getDefinition('filter_spamspan');
return $configuration['settings'];
}
public function settingsSummary() {
$summary = parent::settingsSummary();
if ($this
->getSetting('spamspan_use_form')) {
$summary[] = $this
->t('Link to a contact form instead of an email address');
}
else {
$summary[] = $this
->t('Replacement for "@" is %1', [
'%1' => $this
->getSetting('spamspan_at'),
]);
if ($this
->getSetting('spamspan_use_graphic')) {
$summary[] = $this
->t('Use a graphical replacement for "@"');
}
if ($this
->getSetting('spamspan_dot_enable')) {
$summary[] = $this
->t('Replacement for "." is %1', [
'%1' => $this
->getSetting('spamspan_dot'),
]);
}
}
return $summary;
}
public function validateSettingsForm(array &$form, FormStateInterface $form_state) {
$field_name = $form_state
->get('plugin_settings_edit');
$settings = $form_state
->getValue([
'fields',
$field_name,
'settings_edit_form',
'settings',
]);
$use_form = $settings['use_form'];
unset($settings['use_form']);
$settings += $use_form;
$form_state
->setValue([
'fields',
$field_name,
'settings_edit_form',
'settings',
], $settings);
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#markup' => spamspan($item->value, $this
->getSettings()),
'#attached' => [
'library' => [
'spamspan/obfuscate',
],
],
];
}
return $elements;
}
}