\Field\UtilityTextToolbox

Text utility class.

Utility methods used by TextField Handler.

Summary

Methods
Properties
Constants
getInstance()
formatter()
process()
plainProcessor()
fullProcessor()
filteredProcessor()
markdownProcessor()
closeOpenTags()
stripHtmlTags()
urlToLink()
emailToLink()
emailObfuscator()
filterText()
trimmer()
getMarkdownParser()
__debugInfo()
shortcodes()
stripShortcodes()
escapeShortcodes()
enableShortcodes()
disableShortcodes()
No public properties found
No constants found
No protected methods found
$_instance
$_MarkdownParser
N/A
No private methods found
No private properties found
N/A

Properties

$_MarkdownParser

$_MarkdownParser : \Field\Lib\Parsedown

Instance of markdown parser class.

Type

\Field\Lib\Parsedown

Methods

getInstance()

getInstance() : \Field\Utility\TextToolbox

Returns an instance of this class.

Useful when we need to use some of the trait methods.

Returns

\Field\Utility\TextToolbox

formatter()

formatter(\Field\Model\Entity\Field $field) : string

Formats the given field.

Parameters

\Field\Model\Entity\Field $field

The field being rendered

Returns

string

process()

process(string $content, string $processor) : string

Process the given text to its corresponding format.

Parameters

string $content

Content to process

string $processor

"plain", "filtered", "markdown" or "full"

Returns

string

plainProcessor()

plainProcessor(string $text) : string

Process text in plain mode.

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Parameters

string $text

The text to process

Returns

string

fullProcessor()

fullProcessor(string $text) : string

Process text in full HTML mode.

  • Web page addresses turn into links automatically.
  • E-mail addresses turn into links automatically.

Parameters

string $text

The text to process

Returns

string

filteredProcessor()

filteredProcessor(string $text) : string

Process text in filtered HTML mode.

  • Web page addresses turn into links automatically.
  • E-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Parameters

string $text

The text to process

Returns

string

markdownProcessor()

markdownProcessor(string $text) : string

Process text in markdown mode.

Parameters

string $text

The text to process

Returns

string

closeOpenTags()

closeOpenTags(string $html) : string

Attempts to close any unclosed HTML tag.

Parameters

string $html

HTML content to fix

Returns

string

stripHtmlTags()

stripHtmlTags(string $html) : string

Safely strip HTML tags.

Parameters

string $html

HTML content

Returns

string

urlToLink()

urlToLink(string $text) : string

Convert any URL to a "<a>" HTML tag.

It will ignores URLs in existing <a> tags.

Parameters

string $text

The text where to look for links

Returns

string

emailToLink()

emailToLink(string $text) : string

Convert any email to a "mailto" link.

Escape character is \. For example, "\demo@email.com" won't be converted to link.

Parameters

string $text

The text where to look for emails addresses

Returns

string

emailObfuscator()

emailObfuscator(string $email) : string

Protects email address so bots can not read it.

Replaces emails address with an encoded JS script, so there is no way bots can read an email address from the generated HTML source code.

Parameters

string $email

The email to obfuscate

Returns

string

filterText()

filterText(string $text) : string

Strips HTML tags and any shortcode.

Parameters

string $text

The text to process

Returns

string

trimmer()

trimmer(string $text, string|integer|false $len, string $ellipsis) : string

Safely trim a text.

This method is HTML aware, it will not "destroy" any HTML tag. You can trim the text to a given number of characters, or you can give a string as second argument which will be used to cut the given text and return the first part.

Examples:

$text = '
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Fusce augue nulla, iaculis adipiscing risus sed, pharetra tempor risus.
<!-- readmore -->
Ut volutpat nisl enim, quic sit amet quam ut lacus condimentum volutpat in eu magna.
Phasellus a dolor cursus, aliquam felis sit amet, feugiat orci. Donec vel consec.';

echo $this->trimmer($text, '<!-- readmore -->');

// outputs:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Fusce augue nulla, iaculis adipiscing risus sed, pharetra tempor risus.

echo $this->trimmer('Lorem ipsum dolor sit amet, consectetur adipiscing elit', 10);
// out: "Lorem ipsu ..."

Parameters

string $text

The text to trim

string|integer|false $len

Either a string indicating where to cut the text, or a integer to trim text to that number of characters. If not given (false by default) text will be trimmed to 600 characters length.

string $ellipsis

Will be used as ending and appended to the trimmed string. Defaults to ...

Returns

string

getMarkdownParser()

getMarkdownParser() : \Field\Lib\Parsedown

Gets a markdown parser instance.

Returns

\Field\Lib\Parsedown

__debugInfo()

__debugInfo() : array

Debug friendly object properties.

Returns

array

shortcodes()

shortcodes(string $content, object|null $context) : string

Look for shortcodes in the given text.

If any is found an event is triggered asking for its Event Lister method. For example:

{nice_button color=green}Click Me!{/nice_button}

You must define an Event Lister nice_button:

class YourListener implements EventListenerInterface {
    public function implementedEvents() {
        return ['nice_button' => 'shortcodeNiceButton'];
    }

    public function shortcodeNiceButton(Event $event, $atts, $content, $tag) {
        // return some text
    }
}

As you can see shortcodes methods will receive three arguments:

$atts

Array which may include any arbitrary attributes that are specified by the user. Attribute names are always converted to lowercase before they are passed into the handler function. Values remains untouched.

{some_shortcode Foo="bAr" /}

Produces:

$atts = ['foo' => 'bAr'];

TIP: Don't use camelCase or UPPER-CASE for your $atts attribute names

$content

Holds the enclosed content (if the shortcode is used in its enclosing form). For self-closing shortcodes $content will be null:

{self_close some=thing /}

$tag

The shortcode name. i.e.: some_shortcode

Parameters

string $content

The the text to parse

object|null $context

Context to use when triggering events

Returns

string —

Orginal string modified with no shortcodes [..]

stripShortcodes()

stripShortcodes(string $content) : string

Removes all shortcodes from the given content.

Parameters

string $content

Text from which to remove shortcodes

Returns

string —

Content without shortcodes

escapeShortcodes()

escapeShortcodes(string $content) : string

Escapes all shortcodes from the given content.

Parameters

string $content

Text from which to escape shortcodes

Returns

string —

Content with all shortcodes escaped

enableShortcodes()

enableShortcodes() : void

Enables shortcode parser.

disableShortcodes()

disableShortcodes() : void

Globally disables shortcode parser.

The shortcodes() method will not work when disabled.