Transformers

The admin CRUD controller provides an XHR JSON endpoint out of the box. Transformers are a way to you to control what is provided in these routes.

There are four transformers applied by default. Each of these is a method on Yadda\Enso\Crud\Controller. However, any suitable callable can be used as a transformer. You probably don't want to override or remove these.

  • applyFormatters
  • setRowClasses
  • addIds
  • addIndexActions

applyFormatters

This will apply any of the formatters that may be applied to the columns that are on your CRUD's index page.

setRowClasses

If a Row Class Callback has been set on a column, this transformer will be used to control the classes for an item when displayed on an back-end index page.

addIds

This will add an ID so that the JavaScript that displays the CRUD index will know which item is which.

addIndexActions

This applies index actions that have been defined in the crud. I'm not sure how this works. Andrew?

Adding your own Transformers

You can add your own Transformers by overriding the getTransformers method on your Controller. For example:

<?php

namespace App\Http\Controllers\Admin;

use Yadda\Enso\Crud\Controller;

class PageController extends Controller
{
  public function getTransformers()
  {
    return [
      ...parent::getTransformers(),
      [$this, 'prependTitle']
    ];
  }

  public function prependTitle($parsed, $item)
  {
    $parsed['title'] = 'This is the title: ' . $parsed['title'];

    return $parsed;
  }
}

Transformers will always receive two arguments:

  • $parsed - This is an array representation of the model being transformed. The first transformer in getTransformers will receive the original Model. This will convert it into an array and then pass it to each transformer to manipulate it as needed.
  • $item - The original model. This will be unchanged from tranform to transform and can be useful for accessing original, unaltered data.