Have you seen our new video tutorials? Check it out!

WYSIWYG Field Type

A powerful WYSIWYG editor field type built with Redactor.

Introduction

anomaly.field_type.wysiwyg

The WYSIWYG field type provides a rich text editor input powered by Redactor.

Configuration

Below is the full configuration available with defaults values:

"example" => [
    "type"   => "anomaly.field_type.wysiwyg",
    "config" => [
        "default_value" => null,
        "configuration" => "default",
        "line_breaks"   => false,
        "height"        => 500,
        "buttons"       => [],
        "plugins"       => [],
    ]
]
Configuration
Key Example Description

default_value

<h1>Hello World</h1>

The default value.

configuration

basic

The configuration preset.

line_breaks

true

If enabled, line breaks will be used instead of new paragraphs when pressing enter in the editor.

height

750

The height of the editor.

buttons

["bold", "italic"]

Specify the available buttons for the editor. By default, available options are format, bold, italic, deleted, lists, link, horizontalrule, and underline.

plugins

["filemanager", "imagemanager"]

Specify the available plugins for the editor. By default, available options are alignment, inlinestyle, table, video, filemanager, imagemanager, source, and fullscreen.

Addon Configuration

The WYSIWYG field type configures Redactor options using it's redactor.php config file.

You can override these options by publishing the addon and modifying the resulting configuration file:

php artisan addon:publish anomaly.field_type.wysiwyg

The field type will be published to /resources/{application}/addons/anomaly/wysiwyg-field_type.

Success: If you feel something is missing - add it to the config and send a pull request to https://github.com/anomalylabs/wysiwyg-field_type

Storage Files

The WYSIWYG field type dumps content to a corresponding storage file. The path to this file will be visible underneath the editor when app.debug is enabled.

You can edit this file directly and it will automatically sync back and forth with the database value. This is for convenience only, never commit the storage directory.

Usage

This section will show you how to use the field type via API and in the view layer.

Setting Values

You can set the field type value with a string.

$entry->example = $html;

Basic Output

The field type always returns the storage file content by default.

$entry->example; // Raw contents of storage::example/input/file.html

Presenter Output

This section will show you how to use the decorated value provided by the \Anomaly\WysiwygFieldType\WysiwygFieldTypePresenter class.

WysiwygFieldTypePresenter::path()

The path method returns the prefix hinted path to the storage file.

Returns: string
Example
$decorated->example->path(); // storage::the/path/example.html
Twig
{{ decorated.example.path() }} // storage::the/path/example.html

WysiwygFieldTypePresenter::render()

The render method returns the rendered view using the storage file.

Returns: string
Arguments
Key Required Type Default Description

$payload

false

array

null

Additional payload data for the view.

Example
$decorated->example->render(['name' => 'Ryan']);
Twig
{{ decorated.example.render({'name': 'Ryan'})|raw }}

WysiwygFieldTypePresenter::content()

The content method returns the raw value.

Returns: string
Example
$decorated->example->content();
Twig
{{ decorated.example.content() }}

WysiwygFieldTypePresenter::excerpt()

The excerpt method returns a limited snippet from the text method while preserving whole words.

Returns: string
Arguments
Key Required Type Default Description

$length

false

integer

100

The length to limit the value by.

$end

false

string

...

The ending for the string only if truncated.

Example
$decorated->example->excerpt();
Twig
{{ decorated.example.excerpt() }}

WysiwygFieldTypePresenter::text()

The text method returns the text-only content from the storage file.

Returns: string
Arguments
Key Required Type Default Description

$allowed

false

string

none

The string of tags to allow.

Example
$decorated->example->text();
Twig
{{ decorated.example.text() }}

WysiwygFieldTypePresenter::__toString()

The __toString method is mapped to the render method.

Returns: string
Example
$decorated->example;
Twig
{{ decorated.example|raw }}
Heads Up: The __toString will not properly display exceptions spurring from value content.