\Field\EventFieldHook

Field rendering dispatcher.

Dispatches Render.Field\Model\Entity\Field rendering-request from View to their corresponding FieldHandlers.

Field Handlers should implement the <FieldHandler>.Entity.display hook. e.g.:

Field.TextField.Entity.display

Its callable method should expect two parameters, $field and $options, and it should return a HTML string representation of $field. i.e.:

public function display(Event $event, $field, $options) {
    return
        "<h2>{$field->label}</h2>" .
        "<p>{$field->value}</p>";
}

Usually you will rely on view elements for HTML rendering, to invoke View::element(...), you should use event's subject which is the view instance being used:

public function display(Event $event, $field, $options) {
    return $event
        ->subject()
        ->element('MyPlugin.text_field_display', compact('field', 'options'));
}

Remember that view elements can alway be overwritten by themes. So it's a good practice always use view elements as rendering method instead returning hard-coded HTML code.

Summary

Methods
Properties
Constants
implementedEvents()
renderField()
trigger()
eventDispatcher()
triggered()
viewMode()
addViewMode()
viewModes()
onViewMode()
asViewMode()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

implementedEvents()

implementedEvents() : void

Returns a list of hooks this Hook Listener is implementing. When the class is registered in an event manager, each individual method will be associated with the respective event.

renderField()

renderField(\Cake\Event\Event $event, \Field\Model\Entity\Field $field, array $options) : string

We catch all field rendering request (from QuickApps\View\View) here, then we dispatch to their corresponding FieldHandler.

If the field object being rendered has been set to "hidden" for the current view mode it will not be rendered.

Parameters

\Cake\Event\Event $event

The event that was triggered

\Field\Model\Entity\Field $field

Mock entity

array $options

Additional array of options

Returns

string —

The rendered field

trigger()

trigger(array|string $eventName) : \Cake\Event\Event

Triggers the given event name. This method provides a shortcut for:

$this->trigger('EventName', $arg1, $arg2, ..., $argn);

You can provide a subject to use by passing an array as first arguments where the first element is the event name and the second one is the subject, if no subject is given $this will be used by default:

$this->trigger(['GetTime', new MySubject()], $arg_0, $arg_1, ..., $arg_n);

You can also indicate an EventDispatcher instance to use by prefixing the event name with <InstanceName>::, for instance:

$this->trigger('Blog::EventName', $arg1, $arg2, ..., $argn);

This will use the EventDispacher instance named Blog and will trigger the event EventName within that instance.

Parameters

array|string $eventName

The event name to trigger

Returns

\Cake\Event\Event —

The event object that was fired

eventDispatcher()

eventDispatcher(string $name) : \QuickApps\Event\EventDispatcher

Gets an instance of the given Event Dispatcher name.

Usage:

$this->eventDispatcher('myDispatcher')
    ->trigger('MyEventName', $argument)
    ->result;

Parameters

string $name

Name of the dispatcher to get, defaults to 'default'

Returns

\QuickApps\Event\EventDispatcher

triggered()

triggered(string|null $eventName) : integer|array

Retrieves the number of times an event was triggered, or the complete list of events that were triggered.

Parameters

string|null $eventName

The name of the event, if null returns the entire list of event that were fired

Returns

integer|array

viewMode()

viewMode(string|null $slug) : void

Sets a view mode or get current view mode.

Parameters

string|null $slug

Slug name of the view mode

addViewMode()

addViewMode(string|array $slug, string|null $name, string|null $description) : void

Registers a new view mode. Or overwrite if already exists.

Parameters

string|array $slug

Slug name of your view mode. e.g.: my-view mode. Or an array of view modes to register indexed by slug name

string|null $name

Human readable name. e.g.: My View Mode

string|null $description

A brief description about for what is this view mode

viewModes()

viewModes(boolean|string $viewMode) : array

Gets the full list of all registered view modes, or for a single view mode if $viewMode is set to a string value.

Parameters

boolean|string $viewMode

Set to true to get full list. Or false (by default) to get only the slug of all registered view modes. Or set to a string value to get information for that view mode only.

Returns

array

onViewMode()

onViewMode(string|array $viewMode, callable $method) : mixed

Runs the given callable when the in-use view mode matches.

You can provide multiple view modes, in that case callable method will be executed if current view mode matches any in the given array.

Usage

// run this only on `teaser` view mode
echo $this->onViewMode('teaser', function () use ($someVar) {
    return $this->element('teaser_element', compact('someVar'));
});

// run this on "teaser" view mode, or "search-result" view mode
echo $this->onViewMode(['teaser', 'search-result'], function () use ($someVar) {
    return $this->element('teaser_or_search_result', compact('someVar'));
});

Parameters

string|array $viewMode

View Mode slug, or an array of slugs

callable $method

A callable function to run, it receives $this as only argument

Returns

mixed —

Callable return

asViewMode()

asViewMode(string|array $viewMode, callable $method) : mixed

Runs the given callable as it were under the given view mode.

Usage

$this->viewMode('full');
echo 'before: ' . $this->viewMode();

echo $this->asViewMode('teaser', function () {
     echo 'callable: ' . $this->viewMode();
});

echo 'after: ' . $this->viewMode();

// output:
// before: full
// callable: teaser
// after: full

Parameters

string|array $viewMode

View Mode slug, or an array of slugs

callable $method

A callable function to run, it receives $this as only argument

Returns

mixed —

Callable return