Commit 9beaadf7 authored by Deepanker Bhalla's avatar Deepanker Bhalla Committed by Mark Casias
Browse files

Issue #2997706 by deepanker_bhalla, dhirendra.mishra, msankhala, markie: Coding standard

parent d24b7655
Loading
Loading
Loading
Loading
+14 −5
Changes for README.txt: 14 added lines, 5 removed lines.
Original line number Diff line number Diff line
Smart Trim implements a new field formatter for textfields (text, text_long, and text_with_summary, if you want to get technical) that improved upon the "Summary or Trimmed" formatter built into Drupal 7.
Smart Trim implements a new field formatter for textfields
(text, text_long, and text_with_summary, if you want to get technical) that
improved upon the "Summary or Trimmed" formatter built into Drupal 7.

After installing and enabling Smart Trim, you should see a "Smart trimmed" option in the format dropdown for your text fields. With smart trim, you have control over:
After installing and enabling Smart Trim, you should see a "Smart trimmed"
option in the format dropdown for your text fields.
With smart trim, you have control over:

1. The trim length
2. Whether the trim length is measured in characters or word
@@ -8,12 +12,17 @@ After installing and enabling Smart Trim, you should see a "Smart trimmed" optio
4. Displaying an optional "More" link immediately after the trimmed text.
5. Stripping out HTML tags from the field

The "More" link functionality may not make sense in many contexts, and may be redundant in situations where "Read More" is included in set of links included with the node.
The "More" link functionality may not make sense in many contexts,
and may be redundant in situations where "Read More" is included in
set of links included with the node.

Initial release is strictly for Drupal 7. No backport to Drupal 6 is planned.

Note that HTML markup not seen by end-users will still be counted when calculating trim length. This may be addressed in future releases.
Note that HTML markup not seen by end-users will still be counted when
calculating trim length. This may be addressed in future releases.

Smart Trim was initially developed by Ben Byrne while at New Signature (bbyrne@newsignature.com) but Ben is now at Cornershop Creative (ben@cornershopcreative.com).
Smart Trim was initially developed by Ben Byrne while at
New Signature (bbyrne@newsignature.com) but Ben is now at
Cornershop Creative (ben@cornershopcreative.com).

8.x branch started by Paul Ferlito (https://drupal.org/user/511172);
+56 −57
Changes for src/Plugin/Field/FieldFormatter/SmartTrimFormatter.php: 56 added lines, 57 removed lines.
Original line number Diff line number Diff line
@@ -6,7 +6,6 @@ use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Component\Utility\Unicode;
use Drupal\smart_trim\Truncate\TruncateHTML;

/**
@@ -39,7 +38,7 @@ class SmartTrimFormatter extends FormatterBase {
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return array(
    return [
      'trim_length' => '600',
      'trim_type' => 'chars',
      'trim_suffix' => '',
@@ -49,8 +48,8 @@ class SmartTrimFormatter extends FormatterBase {
      'more_class' => 'more-link',
      'more_text' => 'More',
      'summary_handler' => 'full',
      'trim_options' => array(),
    ) + parent::defaultSettings();
      'trim_options' => [],
    ] + parent::defaultSettings();
  }

  /**
@@ -59,108 +58,108 @@ class SmartTrimFormatter extends FormatterBase {
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form, $form_state);

    $element['trim_length'] = array(
    $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'] = array(
    $element['trim_type'] = [
      '#title' => $this->t('Trim units'),
      '#type' => 'select',
      '#options' => array(
      '#options' => [
        'chars' => $this->t("Characters"),
        'words' => $this->t("Words"),
      ),
      ],
      '#default_value' => $this->getSetting('trim_type'),
    );
    ];

    $element['trim_suffix'] = array(
    $element['trim_suffix'] = [
      '#title' => $this->t('Suffix'),
      '#type' => 'textfield',
      '#size' => 10,
      '#default_value' => $this->getSetting('trim_suffix'),
    );
    ];

    $element['wrap_output'] = array(
    $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'] = array(
    $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' => array(
        'visible' => array(
          ':input[name="fields[body][settings_edit_form][settings][wrap_output]"]' => array('checked' => TRUE),
        ),
      ),
    );

    $element['more_link'] = array(
      '#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'] = array(
    $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' => array(
        'visible' => array(
          ':input[name="fields[body][settings_edit_form][settings][more_link]"]' => array('checked' => TRUE),
        ),
      ),
    );

    $element['more_class'] = array(
      '#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' => array(
        'visible' => array(
          ':input[name="fields[body][settings_edit_form][settings][more_link]"]' => array('checked' => TRUE),
        ),
      ),
    );
      '#states' => [
        'visible' => [
          ':input[name="fields[body][settings_edit_form][settings][more_link]"]' => ['checked' => TRUE],
        ],
      ],
    ];

    if ($this->fieldDefinition->getType() == 'text_with_summary') {
      $element['summary_handler'] = array(
      $element['summary_handler'] = [
        '#title' => $this->t('Summary'),
        '#type' => 'select',
        '#options' => array(
        '#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'] = array(
    $element['trim_options'] = [
      '#title' => $this->t('Additional options'),
      '#type' => 'checkboxes',
      '#options' => array(
      '#options' => [
        'text' => $this->t('Strip HTML'),
        'trim_zero' => t('Honor a zero trim length'),
      ),
      '#default_value' => empty($trim_options_value) ? array() : array_keys(array_filter($trim_options_value)),
    );
      ],
      '#default_value' => empty($trim_options_value) ? [] : array_keys(array_filter($trim_options_value)),
    ];

    return $element;
  }
@@ -169,7 +168,7 @@ class SmartTrimFormatter extends FormatterBase {
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = array();
    $summary = [];
    $type = $this->t('words');
    if ($this->getSetting('trim_type') == 'chars') {
      $type = $this->t('characters');
@@ -192,7 +191,7 @@ class SmartTrimFormatter extends FormatterBase {
   */
  public function viewElements(FieldItemListInterface $items, $langcode = NULL) {

    $element = array();
    $element = [];
    $setting_trim_options = $this->getSetting('trim_options');
    $settings_summary_handler = $this->getSetting('summary_handler');
    $entity = $items->getEntity();
@@ -210,7 +209,7 @@ class SmartTrimFormatter extends FormatterBase {

      // Process additional options (currently only HTML on/off).
      if (!empty($setting_trim_options)) {
        // Allow a zero length trim
        // Allow a zero length trim.
        if (!empty($setting_trim_options['trim_zero']) && $this->getSetting('trim_length') == 0) {
          // If the summary is empty, trim to zero length.
          if (empty($item->summary)) {
@@ -269,23 +268,23 @@ class SmartTrimFormatter extends FormatterBase {

        // But wait! Don't add a more link if the field ends in <!--break-->.
        if ($uri && $this->getSetting('more_link') && strpos(strrev($output), strrev('<!--break-->')) !== 0) {
          $more = $this->t($this->getSetting('more_text'));
          $more = $this->getSetting('more_text');
          $class = $this->getSetting('more_class');

          $project_link = Link::fromTextAndUrl($more, $uri);
          $project_link = $project_link->toRenderable();
          $project_link['#attributes'] = array(
            'class' => array(
          $project_link['#attributes'] = [
            'class' => [
              $class,
            ),
          );
            ],
          ];
          $project_link['#prefix'] = '<div class="' . $class . '">';
          $project_link['#suffix'] = '</div>';
          $link = render($project_link);
        }
      }
      $output .= $link;
      $element[$delta] = array('#markup' => $output);
      $element[$delta] = ['#markup' => $output];
    }
    return $element;
  }