"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/text/src/Plugin/Field/FieldFormatter/TextTrimmedFormatter.php" (26 Nov 2020, 4208 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "TextTrimmedFormatter.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\text\Plugin\Field\FieldFormatter;
    4 
    5 use Drupal\Core\Field\FormatterBase;
    6 use Drupal\Core\Field\FieldItemListInterface;
    7 use Drupal\Core\Form\FormStateInterface;
    8 use Drupal\Core\Security\TrustedCallbackInterface;
    9 
   10 /**
   11  * Plugin implementation of the 'text_trimmed' formatter.
   12  *
   13  * Note: This class also contains the implementations used by the
   14  * 'text_summary_or_trimmed' formatter.
   15  *
   16  * @see \Drupal\text\Field\Formatter\TextSummaryOrTrimmedFormatter
   17  *
   18  * @FieldFormatter(
   19  *   id = "text_trimmed",
   20  *   label = @Translation("Trimmed"),
   21  *   field_types = {
   22  *     "text",
   23  *     "text_long",
   24  *     "text_with_summary"
   25  *   },
   26  *   quickedit = {
   27  *     "editor" = "form"
   28  *   }
   29  * )
   30  */
   31 class TextTrimmedFormatter extends FormatterBase implements TrustedCallbackInterface {
   32 
   33   /**
   34    * {@inheritdoc}
   35    */
   36   public static function defaultSettings() {
   37     return [
   38       'trim_length' => '600',
   39     ] + parent::defaultSettings();
   40   }
   41 
   42   /**
   43    * {@inheritdoc}
   44    */
   45   public function settingsForm(array $form, FormStateInterface $form_state) {
   46     $element['trim_length'] = [
   47       '#title' => t('Trimmed limit'),
   48       '#type' => 'number',
   49       '#field_suffix' => t('characters'),
   50       '#default_value' => $this->getSetting('trim_length'),
   51       '#description' => t('If the summary is not set, the trimmed %label field will end at the last full sentence before this character limit.', ['%label' => $this->fieldDefinition->getLabel()]),
   52       '#min' => 1,
   53       '#required' => TRUE,
   54     ];
   55     return $element;
   56   }
   57 
   58   /**
   59    * {@inheritdoc}
   60    */
   61   public function settingsSummary() {
   62     $summary = [];
   63     $summary[] = t('Trimmed limit: @trim_length characters', ['@trim_length' => $this->getSetting('trim_length')]);
   64     return $summary;
   65   }
   66 
   67   /**
   68    * {@inheritdoc}
   69    */
   70   public function viewElements(FieldItemListInterface $items, $langcode) {
   71     $elements = [];
   72 
   73     $render_as_summary = function (&$element) {
   74       // Make sure any default #pre_render callbacks are set on the element,
   75       // because text_pre_render_summary() must run last.
   76       $element += \Drupal::service('element_info')->getInfo($element['#type']);
   77       // Add the #pre_render callback that renders the text into a summary.
   78       $element['#pre_render'][] = [TextTrimmedFormatter::class, 'preRenderSummary'];
   79       // Pass on the trim length to the #pre_render callback via a property.
   80       $element['#text_summary_trim_length'] = $this->getSetting('trim_length');
   81     };
   82 
   83     // The ProcessedText element already handles cache context & tag bubbling.
   84     // @see \Drupal\filter\Element\ProcessedText::preRenderText()
   85     foreach ($items as $delta => $item) {
   86       $elements[$delta] = [
   87         '#type' => 'processed_text',
   88         '#text' => NULL,
   89         '#format' => $item->format,
   90         '#langcode' => $item->getLangcode(),
   91       ];
   92 
   93       if ($this->getPluginId() == 'text_summary_or_trimmed' && !empty($item->summary)) {
   94         $elements[$delta]['#text'] = $item->summary;
   95       }
   96       else {
   97         $elements[$delta]['#text'] = $item->value;
   98         $render_as_summary($elements[$delta]);
   99       }
  100     }
  101 
  102     return $elements;
  103   }
  104 
  105   /**
  106    * Pre-render callback: Renders a processed text element's #markup as a summary.
  107    *
  108    * @param array $element
  109    *   A structured array with the following key-value pairs:
  110    *   - #markup: the filtered text (as filtered by filter_pre_render_text())
  111    *   - #format: containing the machine name of the filter format to be used to
  112    *     filter the text. Defaults to the fallback format. See
  113    *     filter_fallback_format().
  114    *   - #text_summary_trim_length: the desired character length of the summary
  115    *     (used by text_summary())
  116    *
  117    * @return array
  118    *   The passed-in element with the filtered text in '#markup' trimmed.
  119    *
  120    * @see filter_pre_render_text()
  121    * @see text_summary()
  122    */
  123   public static function preRenderSummary(array $element) {
  124     $element['#markup'] = text_summary($element['#markup'], $element['#format'], $element['#text_summary_trim_length']);
  125     return $element;
  126   }
  127 
  128   /**
  129    * {@inheritdoc}
  130    */
  131   public static function trustedCallbacks() {
  132     return ['preRenderSummary'];
  133   }
  134 
  135 }