View source
<?php
namespace Drupal\smart_trim\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\smart_trim\Truncate\TruncateHTML;
class SmartTrimFormatter extends FormatterBase {
public static function defaultSettings() {
return [
'trim_length' => '600',
'trim_type' => 'chars',
'trim_suffix' => '',
'wrap_output' => 0,
'wrap_class' => 'trimmed',
'more_link' => 0,
'more_class' => 'more-link',
'more_text' => 'More',
'more_aria_label' => 'Read more about [node:title]',
'summary_handler' => 'full',
'trim_options' => [],
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
$element['trim_length'] = [
'#title' => $this
->t('Trim length'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $this
->getSetting('trim_length'),
'#min' => 0,
'#required' => TRUE,
];
$element['trim_type'] = [
'#title' => $this
->t('Trim units'),
'#type' => 'select',
'#options' => [
'chars' => $this
->t("Characters"),
'words' => $this
->t("Words"),
],
'#default_value' => $this
->getSetting('trim_type'),
];
$element['trim_suffix'] = [
'#title' => $this
->t('Suffix'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $this
->getSetting('trim_suffix'),
];
$element['wrap_output'] = [
'#title' => $this
->t('Wrap trimmed content?'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('wrap_output'),
'#description' => $this
->t('Adds a wrapper div to trimmed content.'),
];
$element['wrap_class'] = [
'#title' => $this
->t('Wrapped content class.'),
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $this
->getSetting('wrap_class'),
'#description' => $this
->t('If wrapping, define the class name here.'),
'#states' => [
'visible' => [
':input[name="fields[body][settings_edit_form][settings][wrap_output]"]' => [
'checked' => TRUE,
],
],
],
];
$element['more_link'] = [
'#title' => $this
->t('Display more link?'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('more_link'),
'#description' => $this
->t('Displays a link to the entity (if one exists)'),
];
$element['more_text'] = [
'#title' => $this
->t('More link text'),
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $this
->getSetting('more_text'),
'#description' => $this
->t('If displaying more link, enter the text for the link.'),
'#states' => [
'visible' => [
':input[name="fields[body][settings_edit_form][settings][more_link]"]' => [
'checked' => TRUE,
],
],
],
];
$element['more_aria_label'] = [
'#title' => $this
->t('More link aria-label'),
'#type' => 'textfield',
'#size' => 30,
'#default_value' => $this
->getSetting('more_aria_label'),
'#description' => $this
->t('If displaying more link, provide additional context for screen-reader users. Tokens supported. In most cases, the aria-label value will be announced instead of the link text.'),
'#states' => [
'visible' => [
':input[name="fields[body][settings_edit_form][settings][more_link]"]' => [
'checked' => TRUE,
],
],
],
];
$element['token_browser'] = [
'#type' => 'item',
'#theme' => 'token_tree_link',
'#token_types' => [
$this->fieldDefinition
->getTargetEntityTypeId(),
],
'#states' => [
'visible' => [
':input[name="fields[body][settings_edit_form][settings][more_link]"]' => [
'checked' => TRUE,
],
],
],
];
$element['more_class'] = [
'#title' => $this
->t('More link class'),
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $this
->getSetting('more_class'),
'#description' => $this
->t('If displaying more link, add a custom class for formatting.'),
'#states' => [
'visible' => [
':input[name="fields[body][settings_edit_form][settings][more_link]"]' => [
'checked' => TRUE,
],
],
],
];
if ($this->fieldDefinition
->getType() == 'text_with_summary') {
$element['summary_handler'] = [
'#title' => $this
->t('Summary'),
'#type' => 'select',
'#options' => [
'full' => $this
->t("Use summary if present, and do not trim"),
'trim' => $this
->t("Use summary if present, honor trim settings"),
'ignore' => $this
->t("Do not use summary"),
],
'#default_value' => $this
->getSetting('summary_handler'),
];
}
$trim_options_value = $this
->getSetting('trim_options');
$element['trim_options'] = [
'#title' => $this
->t('Additional options'),
'#type' => 'checkboxes',
'#options' => [
'text' => $this
->t('Strip HTML'),
'trim_zero' => $this
->t('Honor a zero trim length'),
],
'#default_value' => empty($trim_options_value) ? [] : array_keys(array_filter($trim_options_value)),
];
return $element;
}
public function settingsSummary() {
$summary = [];
$type = $this
->t('words');
if ($this
->getSetting('trim_type') == 'chars') {
$type = $this
->t('characters');
}
$trim_string = $this
->getSetting('trim_length') . ' ' . $type;
if (mb_strlen(trim($this
->getSetting('trim_suffix')))) {
$trim_string .= " " . $this
->t("with suffix");
}
if ($this
->getSetting('more_link')) {
$trim_string .= ", " . $this
->t("with more link");
}
$summary[] = $trim_string;
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode = NULL) {
$element = [];
$setting_trim_options = $this
->getSetting('trim_options');
$settings_summary_handler = $this
->getSetting('summary_handler');
$entity = $items
->getEntity();
foreach ($items as $delta => $item) {
if ($settings_summary_handler != 'ignore' && !empty($item->summary)) {
$output = $item->summary;
}
else {
$output = $item->value;
}
if (!empty($setting_trim_options)) {
if (!empty($setting_trim_options['trim_zero']) && $this
->getSetting('trim_length') == 0) {
if (empty($item->summary)) {
$output = '';
}
elseif ($settings_summary_handler != 'full') {
$output = '';
}
}
if (!empty($setting_trim_options['text'])) {
$output = preg_replace('/<figcaption[^>]*>.*?<\\/figcaption>/is', ' ', $output);
$output = preg_replace('/<script[^>]*>.*?<\\/script>/is', ' ', $output);
$output = preg_replace('/<style[^>]*>.*?<\\/style>/is', ' ', $output);
$output = strip_tags($output);
$output = preg_replace('/\\n|\\r|\\t/m', ' ', $output);
$output = str_replace(' ', ' ', $output);
$output = str_replace(" ", ' ', $output);
$output = trim(preg_replace('/\\s\\s+/', ' ', $output));
}
}
if ($this
->getSetting('summary_handler') != 'full' || empty($item->summary)) {
$truncate = new TruncateHTML();
$length = $this
->getSetting('trim_length');
$ellipse = $this
->getSetting('trim_suffix');
if ($this
->getSetting('trim_type') == 'words') {
$output = $truncate
->truncateWords($output, $length, $ellipse);
}
else {
$output = $truncate
->truncateChars($output, $length, $ellipse);
}
}
$element[$delta] = [
'#type' => 'processed_text',
'#text' => $output,
'#format' => $item->format,
];
if ($this
->getSetting('wrap_output')) {
$element[$delta]['#prefix'] = '<div class="' . $this
->getSetting('wrap_class') . '">';
$element[$delta]['#suffix'] = '</div>';
}
if ($this
->getSetting('more_link') && $entity
->id() && $entity
->hasLinkTemplate('canonical')) {
if (strpos(strrev($output), strrev('<!--break-->')) !== 0) {
$more = $this
->t($this
->getSetting('more_text'));
$class = $this
->getSetting('more_class');
$project_link = $entity
->toLink($more)
->toRenderable();
$project_link['#attributes'] = [
'class' => [
$class,
],
];
$aria_label = $this
->t($this
->getSetting('more_aria_label'));
if ($aria_label) {
$project_link['#attributes']['aria-label'] = \Drupal::token()
->replace($aria_label, [
$entity
->getEntityTypeId() => $entity,
]);
}
$project_link['#prefix'] = '<div class="' . $class . '">';
$project_link['#suffix'] = '</div>';
$element[$delta]['more_link'] = $project_link;
}
}
}
return $element;
}
}