This module adds an extension to DBHTMLVarchar and DBHTMLText which enables limit html text without loosing the html tags. For example if you want a teaser text written with a WYSIWYG.
- Requirements
- Installation
- License
- Configuration
- Documentation
- Usage examples
- Maintainers
- Bugtracker
- Development and contribution
- Silverstripe ^4 | ^5
Simply install the extension with composer
composer require csoellinger/silverstripe-limit-characters-with-html
The extension will be auto added to SilverStripe\ORM\FieldType\DBHTMLVarchar and SilverStripe\ORM\FieldType\DBHTMLText by the included config file. Just run:
sake dev/build "flush=all"
See License
Enable/Disable html minify.
Set HTML minify options as associative array:
collapse_whitespace
=> true|false (Default: true)disable_comments
=> true|false (Default: false)
After installing the module you have three new methods for your db html fields.
/**
* Limit this field's content by a number of characters. It can consider
* html and limit exact or at word ending.
*
* @param int $limit Number of characters to limit by.
* @param string|false $add Ellipsis to add to the end of truncated string.
* @param bool $exact Truncate exactly or at word ending.
*
* @return string HTML text with limited characters.
*/
/**
* Limit this field's content by a number of characters and truncate the field
* to the closest complete word.
*
* @param int $limit Number of characters to limit by.
* @param string|false $add Ellipsis to add to the end of truncated string.
*
* @return string HTML text value with limited characters truncated to the closest word.
*/
/**
* Check if a string is longer than a number of characters. It excludes html
* by default.
*
* @param mixed $excludeHtml Default is true
*
* @return bool
*/
<?php
$htmlText = '<b>Hello World</b>';
// example 1
DBHTMLText::create('Test')
->setValue($htmlText)
->LimitCharactersWithHtml(9)
->Value() // Output: <b>Hello Wo…</b>
// example 2
DBHTMLText::create('Test')
->setValue($htmlText)
->LimitCharactersWithHtmlToClosestWord(9)
->Value() // Output: <b>Hello…</b>
<p>{$TestField.LimitCharactersWithHtml(9)}</p>
<p>{$TestField.LimitCharactersWithHtmlToClosestWord(9)}</p>
- Christopher Söllinger christopher.soellinger@gmail.com
Bugs are tracked in the issues section of this repository. Before submitting an issue please read over existing issues to ensure yours is unique.
If the issue does look like a new bug:
- Create a new issue
- Describe the steps required to reproduce your issue, and the expected outcome. Unit tests, screenshots and screencasts can help here.
- Describe your environment as detailed as possible: Silverstripe version, Browser, PHP version, Operating System, any installed Silverstripe modules.
Please report security issues to the module maintainers directly. Please don't file security issues in the bug tracker.
If you would like to make contributions to the module please ensure you raise a pull request and discuss with the module maintainers.