Skip to content

Sharp show view: specification draft

Philippe Lonchampt edited this page Jan 16, 2019 · 1 revision

The goal is to allow Sharp to display a "show" view (optional) for an entity.

Configuration

"projects" => [
    "list" => ProjectSharpList::class,
    "show" => ProjectSharpShow::class,
    "form" => ProjectSharpForm::class,
    "validator" => ProjectSharpValidator::class
]

Create a show view: fields & layout

We build fields, just like for a Form, but with specific fields (Text, Image, List with items, maybe Vue template based fields... TBD). This code is included in a new SharpShow implementation class.

function buildShowFields()
{
    $this->addField(
        SharpShowTextField::make("last_name")
            ->setLabel("Last name")
    )->addField(
        SharpShowTextField::make("first_name")
            ->setLabel("First name")
    )->addField(
        SharpShowTextField::make("email")
            ->setLabel("Email")
    );
}

And we define a layout, with sections which can contain columns, plus the ability to include EntityLists (with custom configuration):

function buildShowLayout()
{
    $this->addSection(function(ShowLayoutSection $section) {
        $section->setTitle("Coordinates");
        $section->addColumn(7, function(ShowLayoutColumn $column) {
            $column->withFields("first_name|4", "last_name|5")
                ->withSingleField("email");
        });

    // We use an anonymous class here, but of course we can refer to
    // some explicit class giving its full name.
    })->addEntityList(new class() extends SharpShowEntityList {
        function getEntityListClassName()
        {
            return OrderSharpList::class;
        }

        function isFilterHidden(string $filterName)
        {
            return $filterName == "customer";
        }

        function getFilterValueFor(string $filterName)
        {
            return $filterName == "customer" 
                ? $this->getInstanceId() // instanceId was set by Sharp
                : null;
        }
    });
}

The SharpShowEntityList class allows to set default values for filters, search, specificIds, sort (see EntityListQueryParams). The getEntityListClassName() function (abstract) must return a real SharpEntityList (which can be used elsewhere and present in the menu, or not).

Populate a show view

In the same SharpShow implementation class, we include a method to populate data:

function find($id): array
{
    return $this->setCustomTransformer("email", function($email) {
            return "<a href='mailto:{$email}'>{$email}</a>";
        })
        ->transform(
            Customer::findOrFail($id)
        );
}

Embedded EntityLists are populated on their side, each with their regular getListData() method "configured" with the SharpShowEntityList details.

Display a show view

If configured, the show view is displayed on the EntityList row click (replacing the form here).

From the view, with authorization, we can:

  • go to the Form via a dedicated button in the action bar
  • return to the EntityList (back button)
  • maybe execute Commands that are explicitly configured to be present?
  • display an EntityState?