ComputedStringFormatter.php in Computed Field 8.2
File
src/Plugin/Field/FieldFormatter/ComputedStringFormatter.php
View source
<?php
namespace Drupal\computed_field\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
class ComputedStringFormatter extends ComputedFormatterBase {
public static function defaultSettings() {
return [
'sanitized' => TRUE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
return [
'sanitized' => [
'#type' => 'checkbox',
'#title' => t('Sanitized'),
'#default_value' => $this
->getSetting('sanitized'),
],
] + parent::settingsForm($form, $form_state);
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = $this
->getSetting('sanitized') ? t('Sanitized') : t('Unsanitized');
return $summary;
}
protected function prepareValue($value) {
if ($this
->getSetting('sanitized')) {
return nl2br(Html::escape($value));
}
else {
return nl2br($value);
}
}
}