class AdvancedTextFormatter in Advanced Text Formatter 8
Same name and namespace in other branches
- 2.1.x src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php \Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter\AdvancedTextFormatter
- 2.0.x src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php \Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter\AdvancedTextFormatter
Plugin implementation of the 'advanced_text_formatter' formatter.
Plugin annotation
@FieldFormatter(
id = "advanced_text",
module = "advanced_text_formatter",
label = @Translation("Advanced Text"),
field_types = {
"string",
"string_long",
"text",
"text_long",
"text_with_summary",
},
quickedit = {
"editor" = "plain_text"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter\AdvancedTextFormatter
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of AdvancedTextFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ AdvancedTextFormatter.php, line 30
Namespace
Drupal\advanced_text_formatter\Plugin\Field\FieldFormatterView source
class AdvancedTextFormatter extends FormatterBase {
const FORMAT_DRUPAL = 'drupal';
const FORMAT_INPUT = 'input';
const FORMAT_NONE = 'none';
const FORMAT_PHP = 'php';
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'trim_length' => 600,
'ellipsis' => 1,
'word_boundary' => 1,
'token_replace' => 0,
'filter' => 'input',
'format' => 'plain_text',
'allowed_html' => '<a> <b> <br> <dd> <dl> <dt> <em> <i> <li> <ol> <p> <strong> <u> <ul>',
'autop' => 0,
'use_summary' => 0,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elTrimLengthId = Html::getUniqueId('advanced_text_formatter_trim');
$elFilterId = Html::getUniqueId('advanced_text_formatter_filter');
$element['trim_length'] = [
'#id' => $elTrimLengthId,
'#type' => 'number',
'#title' => $this
->t('Trim length'),
'#description' => $this
->t("Set this to 0 if you don't want to cut the text. Otherwise, input a positive integer."),
'#size' => 10,
'#default_value' => $this
->getSetting('trim_length'),
'#required' => TRUE,
];
$element['ellipsis'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Ellipsis'),
'#description' => $this
->t('If checked, a "..." will be added if a field was trimmed.'),
'#default_value' => $this
->getSetting('ellipsis'),
'#states' => [
'visible' => [
'#' . $elTrimLengthId => [
'!value' => '0',
],
],
],
];
$element['word_boundary'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Word Boundary'),
'#description' => $this
->t('If checked, this field be trimmed only on a word boundary.'),
'#default_value' => $this
->getSetting('word_boundary'),
'#states' => [
'visible' => [
'#' . $elTrimLengthId => [
'!value' => '0',
],
],
],
];
$element['use_summary'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use Summary'),
'#description' => $this
->t('If a summary exists, use it.'),
'#default_value' => $this
->getSetting('use_summary'),
];
$token_link = _advanced_text_formatter_browse_tokens($this->fieldDefinition
->getTargetEntityTypeId());
$element['token_replace'] = [
'#type' => 'checkbox',
'#description' => $this
->t('Replace text pattern. e.g %node-title-token or %node-author-name-token, by token values.', [
'%node-title-token' => '[node:title]',
'%node-author-name-token' => '[node:author:name]',
]) . ' ',
'#title' => $this
->t('Token Replace'),
'#default_value' => $this
->getSetting('token_replace'),
];
$element['filter'] = [
'#id' => $elFilterId,
'#title' => $this
->t('Filter'),
'#type' => 'select',
'#options' => [
static::FORMAT_NONE => $this
->t('None'),
static::FORMAT_INPUT => $this
->t('Selected Text Format'),
static::FORMAT_PHP => $this
->t('Limit allowed HTML tags'),
static::FORMAT_DRUPAL => $this
->t('Drupal'),
],
'#default_value' => $this
->getSetting('filter'),
];
$element['format'] = [
'#title' => $this
->t('Format'),
'#type' => 'select',
'#options' => [],
'#default_value' => $this
->getSetting('format'),
'#states' => [
'visible' => [
'#' . $elFilterId => [
'value' => 'drupal',
],
],
],
];
$formats = filter_formats();
foreach ($formats as $formatId => $format) {
$element['format']['#options'][$formatId] = $format
->get('name');
}
$allowedHtml = $this
->getSetting('allowed_html');
if (empty($allowedHtml)) {
$tags = '';
}
elseif (is_string($allowedHtml)) {
$tags = $allowedHtml;
}
else {
$tags = '<' . implode('> <', $allowedHtml) . '>';
}
$element['allowed_html'] = [
'#type' => 'textfield',
'#title' => $this
->t('Allowed HTML tags'),
'#description' => $this
->t('See <a href="@link" target="_blank">filter_xss()</a> for more information', [
'@link' => 'http://api.drupal.org/api/drupal/core%21includes%21common.inc/function/filter_xss/8',
]),
'#default_value' => $tags,
'#element_validate' => [
'_advanced_text_formatter_validate_allowed_html',
],
'#states' => [
'visible' => [
'#' . $elFilterId => [
'value' => 'php',
],
],
],
];
$element['autop'] = [
'#title' => $this
->t('Converts line breaks into HTML (i.e. <br> and <p>) tags.'),
'#type' => 'checkbox',
'#return_value' => 1,
'#default_value' => $this
->getSetting('autop'),
'#states' => [
'invisible' => [
'#' . $elFilterId => [
'!value' => 'php',
],
],
],
];
$element['br'] = [
'#markup' => '<br/>',
];
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$yes = $this
->t('Yes');
$no = $this
->t('No');
if ($this
->getSetting('trim_length') > 0) {
$summary[] = $this
->t('Trim length: @length', [
'@length' => $this
->getSetting('trim_length'),
]);
$summary[] = $this
->t('Ellipsis: @ellipsis', [
'@ellipsis' => $this
->getSetting('ellipsis') ? $yes : $no,
]);
$summary[] = $this
->t('Word Boundary: @word', [
'@word' => $this
->getSetting('word_boundary') ? $yes : $no,
]);
$summary[] = $this
->t('Use Summary: @summary', [
'@summary' => $this
->getSetting('use_summary') ? $yes : $no,
]);
}
$summary[] = $this
->t('Token Replace: @token', [
'@token' => $this
->getSetting('token_replace') ? $yes : $no,
]);
switch ($this
->getSetting('filter')) {
case static::FORMAT_DRUPAL:
$formats = filter_formats();
$format = $this
->getSetting('format');
$format = isset($formats[$format]) ? $formats[$format]
->get('name') : $this
->t('Unknown');
$summary[] = $this
->t('Filter: @filter', [
'@filter' => $this
->t('Drupal'),
]);
$summary[] = $this
->t('Format: @format', [
'@format' => $format,
]);
break;
case static::FORMAT_PHP:
$text = [];
$tags = $this
->getSetting('allowed_html');
$autop = $this
->getSetting('autop');
if (is_array($tags) && !empty($tags)) {
$tags = '<' . implode('> <', $tags) . '>';
}
if (empty($tags)) {
$text[] = $this
->t('Remove all HTML tags.');
}
else {
$text[] = $this
->t('Limit allowed HTML tags: @tags.', [
'@tags' => $tags,
]);
}
if (!empty($autop)) {
$text[] = $this
->t('Convert line breaks into HTML.');
}
$summary[] = $this
->t('Filter: @filter', [
'@filter' => implode(' ', $text),
]);
break;
case static::FORMAT_INPUT:
$summary[] = $this
->t('Filter: @filter', [
'@filter' => $this
->t('Selected Text Format'),
]);
break;
default:
$summary[] = $this
->t('Filter: @filter', [
'@filter' => $this
->t('None'),
]);
break;
}
$summary = array_filter($summary);
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$token_data = [
'user' => \Drupal::currentUser(),
$items
->getEntity()
->getEntityTypeId() => $items
->getEntity(),
];
foreach ($items as $delta => $item) {
if ($this
->getSetting('use_summary') && !empty($item->summary)) {
$output = $item->summary;
}
else {
$output = $item->value;
}
if ($this
->getSetting('token_replace')) {
$output = \Drupal::token()
->replace($output, $token_data);
}
switch ($this
->getSetting('filter')) {
case static::FORMAT_DRUPAL:
$output = check_markup($output, $this
->getSetting('format'), $item
->getLangcode());
break;
case static::FORMAT_PHP:
$output = Xss::filter($output, $this
->getSetting('allowed_html'));
if ($this
->getSetting('autop')) {
$output = _filter_autop($output);
}
break;
case static::FORMAT_INPUT:
$output = check_markup($output, $item->format, $item
->getLangcode());
break;
}
if ($this
->getSetting('trim_length') > 0) {
$options = [
'word_boundary' => $this
->getSetting('word_boundary'),
'max_length' => $this
->getSetting('trim_length'),
'ellipsis' => $this
->getSetting('ellipsis'),
];
$output = advanced_text_formatter_trim_text($output, $options);
}
$elements[$delta] = [
'#markup' => $output,
'#langcode' => $item
->getLangcode(),
];
}
return $elements;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AdvancedTextFormatter:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsBase:: |
|
AdvancedTextFormatter:: |
constant | |||
AdvancedTextFormatter:: |
constant | |||
AdvancedTextFormatter:: |
constant | |||
AdvancedTextFormatter:: |
constant | |||
AdvancedTextFormatter:: |
public | function |
Returns a form to configure settings for the formatter. Overrides FormatterBase:: |
|
AdvancedTextFormatter:: |
public | function |
Returns a short summary for the current formatter settings. Overrides FormatterBase:: |
|
AdvancedTextFormatter:: |
public | function |
Builds a renderable array for a field value. Overrides FormatterInterface:: |
|
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 | |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The label display setting. | |
FormatterBase:: |
protected | property |
The formatter settings. Overrides PluginSettingsBase:: |
|
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
11 |
FormatterBase:: |
protected | function | Returns the value of a field setting. | |
FormatterBase:: |
protected | function | Returns the array of field settings. | |
FormatterBase:: |
public static | function |
Returns if the formatter can be used for the provided field. Overrides FormatterInterface:: |
14 |
FormatterBase:: |
public | function |
Allows formatters to load information for field values being displayed. Overrides FormatterInterface:: |
2 |
FormatterBase:: |
public | function |
Builds a renderable array for a fully themed field. Overrides FormatterInterface:: |
1 |
FormatterBase:: |
public | function |
Constructs a FormatterBase object. Overrides PluginBase:: |
11 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
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. |