annotate core/modules/text/text.module @ 19:fa3358dc1485 tip

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