csoellinger / silverstripe-limit-characters-with-html
A simple Silverstripe extension to limit DBHtmlText and DBHtmlVarChar without loosing html tags.
Installs: 115
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 1
Type:silverstripe-vendormodule
Requires
- php: ^7.4 || ^8.0 || ^8.1 || ^8.2
- pfaciana/tiny-html-minifier: ^3.0
- silverstripe/admin: ^1
- silverstripe/framework: ^4
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3
This package is auto-updated.
Last update: 2024-12-07 14:27:19 UTC
README
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
Requirements
- Silverstripe ^4 | ^5
Installation
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"
License
See License
Configuration
private static bool html_min
Enable/Disable html minify.
private static array html_min_options
Set HTML minify options as associative array:
collapse_whitespace
=> true|false (Default: true)disable_comments
=> true|false (Default: false)
Documentation
After installing the module you have three new methods for your db html fields.
public function LimitCharactersWithHtml( $limit = 20, $add = false, $exact = true ): string
/** * 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. */
public function LimitCharactersWithHtmlToClosestWord( int $limit = 20, $add = false ): string
/** * 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. */
public function LongerThan( int $limit, $excludeHtml = true ): bool
/** * Check if a string is longer than a number of characters. It excludes html * by default. * * @param mixed $excludeHtml Default is true * * @return bool */
Usage examples
<?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>
Maintainers
- Christopher Söllinger christopher.soellinger@gmail.com
Bugtracker
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.
Development and contribution
If you would like to make contributions to the module please ensure you raise a pull request and discuss with the module maintainers.