Plugin Development: View Composers

Previous Section: Model Events


View composers allow modifying existing or passing additional data to views throughout the system before they are displayed to the user.

Contents

Why Use View Composers?

The idea behind your plugin might be to add some additional information to a view or replace part of a view. Example cases where this is useful:

Basic View Composer

A view composer can cover a single or multiple views, defined within an array. The composer has a View object passed into the closure, which is the current view data at that point in time. To pass additional or modify existing data in the view, the object can be modified as needed.

The view names work based on the folder structure of the frontend and operator template folders. If you have a template called 'integration', then the ticket view for that specific template would be 'operator.integration.ticket.ticket'. The * (star) character acts as a wildcard, so for example 'operator.*.ticket.ticket' would cover the ticket view in all templates.


    // Ticket view
    \View::composer('operator.default.ticket.ticket', function ($view) {
        // Get the ticket model
        $ticket = $view->ticket;
    });

    // Custom fields views
    \View::composer([ 'operator.*.forms.customfields', 'frontend.*.forms.customfields' ], function ($view) {
        // Remove all custom fields
        $view->customfields = [ ];
    });

Extending and Overriding Views in the Ticket View

The most common use of view composers will be to add or replace something in the ticket view, so we've provided the option to extend and override existing views in that view.

Extending

Extending will include the named view(s) in addition to the existing content. This is done by passing a variable called extendedViews to the view, with an array value, keyed by the the view to extend. The data for each key is an array of view names, making it possible to extend it with more than one view at a time.


    \View::composer('operator.default.ticket.ticket', function ($view) {
        // Extend the ticket view
        $view->extendedViews['ticket'][] = 'HelloWorld::ticket';
        $view->with('extendedViews', $view->extendedViews);
    });

Available extending options

Area Key
Ticket view sidebar sidebar-ticket
Ticket view main area (above or below messages) ticket
Ticket tab option (such as Messages, Log) ticket_tab
Ticket tab content ticket_tab_content
Reply options reply_options
Scripts within <head> scripts_header
Scripts just above </body> scripts_footer

Overriding

Overriding will replace the current view with the named view. This is done by passing a variable called overriddenViews to the view, with an array value, keyed by the the view to extend.


    \View::composer('operator.default.ticket.ticket', function ($view) {
        // Override the ticket sidebar user details box
        $view->with('overriddenViews', [ 'sidebar-ticket' => 'HelloWorld::sidebar-ticket' ]);
    });

Available overriding options

Area Key
Ticket view sidebar user details sidebar-ticket
Ticket view individual message view message

Next Section: Scheduled Tasks