text.module 8.1 KB

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