annotate core/modules/text/text.module @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Defines simple text field types.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Component\Utility\Html;
Chris@0 9 use Drupal\Component\Utility\Unicode;
Chris@0 10 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 11 use Drupal\filter\Entity\FilterFormat;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Implements hook_help().
Chris@0 15 */
Chris@0 16 function text_help($route_name, RouteMatchInterface $route_match) {
Chris@0 17 switch ($route_name) {
Chris@0 18 case 'help.page.text':
Chris@0 19 $output = '';
Chris@0 20 $output .= '<h3>' . t('About') . '</h3>';
Chris@0 21 $output .= '<p>' . t('The Text module allows you to create short and long text fields with optional summaries. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":text_documentation">online documentation for the Text module</a>.', [':field' => \Drupal::url('help.page', ['name' => 'field']), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#', ':text_documentation' => 'https://www.drupal.org/documentation/modules/text']) . '</p>';
Chris@0 22 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 23 $output .= '<dl>';
Chris@0 24 $output .= '<dt>' . t('Managing and displaying text fields') . '</dt>';
Chris@0 25 $output .= '<dd>' . t('The <em>settings</em> and <em>display</em> of the text field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#']) . '</dd>';
Chris@0 26 $output .= '<dt>' . t('Creating short text fields') . '</dt>';
Chris@0 27 $output .= '<dd>' . t('If you choose <em>Text (plain)</em> or <em>Text (formatted)</em> as the field type on the <em>Manage fields</em> page, then a field with a single row is displayed. You can change the maximum text length in the <em>Field settings</em> when you set up the field.') . '</dd>';
Chris@0 28 $output .= '<dt>' . t('Creating long text fields') . '</dt>';
Chris@0 29 $output .= '<dd>' . t('If you choose <em>Text (plain, long)</em>, <em>Text (formatted, long)</em>, or <em>Text (formatted, long, with summary)</em> on the <em>Manage fields</em> page, then users can insert text of unlimited length. On the <em>Manage form display</em> page, you can set the number of rows that are displayed to users.') . '</dd>';
Chris@0 30 $output .= '<dt>' . t('Trimming the text length') . '</dt>';
Chris@0 31 $output .= '<dd>' . t('On the <em>Manage display</em> page you can choose to display a trimmed version of the text, and if so, where to cut off the text.') . '</dd>';
Chris@0 32 $output .= '<dt>' . t('Displaying summaries instead of trimmed text') . '</dt>';
Chris@0 33 $output .= '<dd>' . t('As an alternative to using a trimmed version of the text, you can enter a separate summary by choosing the <em>Text (formatted, long, with summary)</em> field type on the <em>Manage fields</em> page. Even when <em>Summary input</em> is enabled, and summaries are provided, you can display <em>trimmed</em> text nonetheless by choosing the appropriate format on the <em>Manage display</em> page.') . '</dd>';
Chris@0 34 $output .= '<dt>' . t('Using text formats and editors') . '</dt>';
Chris@0 35 $output .= '<dd>' . t('If you choose <em>Text (plain)</em> or <em>Text (plain, long)</em> you restrict the input to <em>Plain text</em> only. If you choose <em>Text (formatted)</em>, <em>Text (formatted, long)</em>, or <em>Text (formatted, long with summary)</em> you allow users to write formatted text. Which options are available to individual users depends on the settings on the <a href=":formats">Text formats and editors page</a>.', [':formats' => \Drupal::url('filter.admin_overview')]) . '</dd>';
Chris@0 36 $output .= '</dl>';
Chris@0 37 return $output;
Chris@0 38 }
Chris@0 39 }
Chris@0 40
Chris@0 41 /**
Chris@0 42 * Generates a trimmed, formatted version of a text field value.
Chris@0 43 *
Chris@0 44 * If the end of the summary is not indicated using the <!--break--> delimiter
Chris@0 45 * then we generate the summary automatically, trying to end it at a sensible
Chris@0 46 * place such as the end of a paragraph, a line break, or the end of a sentence
Chris@0 47 * (in that order of preference).
Chris@0 48 *
Chris@0 49 * @param $text
Chris@0 50 * The content for which a summary will be generated.
Chris@0 51 * @param $format
Chris@0 52 * The format of the content. If the line break filter is present then we
Chris@0 53 * treat newlines embedded in $text as line breaks. If the htmlcorrector
Chris@0 54 * filter is present, it will be run on the generated summary (if different
Chris@0 55 * from the incoming $text).
Chris@0 56 * @param $size
Chris@0 57 * The desired character length of the summary. If omitted, the default value
Chris@0 58 * will be used. Ignored if the special delimiter is present in $text.
Chris@0 59 *
Chris@0 60 * @return
Chris@0 61 * The generated summary.
Chris@0 62 */
Chris@0 63 function text_summary($text, $format = NULL, $size = NULL) {
Chris@0 64
Chris@0 65 if (!isset($size)) {
Chris@0 66 $size = \Drupal::config('text.settings')->get('default_summary_length');
Chris@0 67 }
Chris@0 68
Chris@0 69 // Find where the delimiter is in the body
Chris@0 70 $delimiter = strpos($text, '<!--break-->');
Chris@0 71
Chris@0 72 // If the size is zero, and there is no delimiter, the entire body is the summary.
Chris@0 73 if ($size == 0 && $delimiter === FALSE) {
Chris@0 74 return $text;
Chris@0 75 }
Chris@0 76
Chris@0 77 // If a valid delimiter has been specified, use it to chop off the summary.
Chris@0 78 if ($delimiter !== FALSE) {
Chris@0 79 return substr($text, 0, $delimiter);
Chris@0 80 }
Chris@0 81
Chris@0 82 // Retrieve the filters of the specified text format, if any.
Chris@0 83 if (isset($format)) {
Chris@0 84 $filters = FilterFormat::load($format)->filters();
Chris@0 85 // If the specified format does not exist, return nothing. $text is already
Chris@0 86 // filtered text, but the remainder of this function will not be able to
Chris@0 87 // ensure a sane and secure summary.
Chris@0 88 if (!$filters) {
Chris@0 89 return '';
Chris@0 90 }
Chris@0 91 }
Chris@0 92
Chris@0 93 // If we have a short body, the entire body is the summary.
Chris@0 94 if (Unicode::strlen($text) <= $size) {
Chris@0 95 return $text;
Chris@0 96 }
Chris@0 97
Chris@0 98 // If the delimiter has not been specified, try to split at paragraph or
Chris@0 99 // sentence boundaries.
Chris@0 100
Chris@0 101 // The summary may not be longer than maximum length specified. Initial slice.
Chris@0 102 $summary = Unicode::truncate($text, $size);
Chris@0 103
Chris@0 104 // Store the actual length of the UTF8 string -- which might not be the same
Chris@0 105 // as $size.
Chris@0 106 $max_rpos = strlen($summary);
Chris@0 107
Chris@0 108 // How much to cut off the end of the summary so that it doesn't end in the
Chris@0 109 // middle of a paragraph, sentence, or word.
Chris@0 110 // Initialize it to maximum in order to find the minimum.
Chris@0 111 $min_rpos = $max_rpos;
Chris@0 112
Chris@0 113 // Store the reverse of the summary. We use strpos on the reversed needle and
Chris@0 114 // haystack for speed and convenience.
Chris@0 115 $reversed = strrev($summary);
Chris@0 116
Chris@0 117 // Build an array of arrays of break points grouped by preference.
Chris@0 118 $break_points = [];
Chris@0 119
Chris@0 120 // A paragraph near the end of sliced summary is most preferable.
Chris@0 121 $break_points[] = ['</p>' => 0];
Chris@0 122
Chris@0 123 // If no complete paragraph then treat line breaks as paragraphs.
Chris@0 124 $line_breaks = ['<br />' => 6, '<br>' => 4];
Chris@0 125 // Newline only indicates a line break if line break converter
Chris@0 126 // filter is present.
Chris@0 127 if (isset($format) && $filters->has('filter_autop') && $filters->get('filter_autop')->status) {
Chris@0 128 $line_breaks["\n"] = 1;
Chris@0 129 }
Chris@0 130 $break_points[] = $line_breaks;
Chris@0 131
Chris@0 132 // If the first paragraph is too long, split at the end of a sentence.
Chris@0 133 $break_points[] = ['. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1];
Chris@0 134
Chris@0 135 // Iterate over the groups of break points until a break point is found.
Chris@0 136 foreach ($break_points as $points) {
Chris@0 137 // Look for each break point, starting at the end of the summary.
Chris@0 138 foreach ($points as $point => $offset) {
Chris@0 139 // The summary is already reversed, but the break point isn't.
Chris@0 140 $rpos = strpos($reversed, strrev($point));
Chris@0 141 if ($rpos !== FALSE) {
Chris@0 142 $min_rpos = min($rpos + $offset, $min_rpos);
Chris@0 143 }
Chris@0 144 }
Chris@0 145
Chris@0 146 // If a break point was found in this group, slice and stop searching.
Chris@0 147 if ($min_rpos !== $max_rpos) {
Chris@0 148 // Don't slice with length 0. Length must be <0 to slice from RHS.
Chris@0 149 $summary = ($min_rpos === 0) ? $summary : substr($summary, 0, 0 - $min_rpos);
Chris@0 150 break;
Chris@0 151 }
Chris@0 152 }
Chris@0 153
Chris@0 154 // If the htmlcorrector filter is present, apply it to the generated summary.
Chris@0 155 if (isset($format) && $filters->has('filter_htmlcorrector') && $filters->get('filter_htmlcorrector')->status) {
Chris@0 156 $summary = Html::normalize($summary);
Chris@0 157 }
Chris@0 158
Chris@0 159 return $summary;
Chris@0 160 }