Skip to content

Sharp Context ​

Sharp provide a way to grab some request context values in the application code.

Generalities ​

The class handling the context is Code16\Sharp\Http\Context\SharpContext; at any point in the request, you can get it via the global helper:

php
sharp()->context();

Current context ​

Let's start with a simple example of how to use the context in a Form to set a field as read-only when the form is in update mode:

php
class MyForm extends SharpForm
{
    // ...
    
    function buildFormFields()
    {
        $this
            ->addField(
                SharpFormTextField::make('key')
                    ->setReadOnly(sharp()->context()->isUpdate())
            )
            ->addField(/*...*/);
    }
}

The SharpContext class allows you to get the following information:

entityKey ​

Grab the current entity key.

isEntityList ​

isShow ​

isForm ​

Find out the current page type.

isUpdate ​

isCreation ​

In Form case, check the current status.

instanceId ​

In Form and Show Page cases, grab the instance id.

Interact with Sharp's Breadcrumb ​

To interact with Sharp's breadcrumb, you can call:

php
sharp()->context()->breadcrumb();

... and then use the following methods:

currentSegment ​

previousSegment ​

Get the current or previous breadcrumb item.

previousShowSegment ​

previousListSegment ​

Get (if existing) the closest Show or List in the breadcrumb.

TIP

As always, prefer the entity class name to the entity key. For instance: sharp()-⁠>context()-⁠>breadcrumb()-⁠>previousShowSegment(MyEntity::class).

The BreadcrumbItem class ​

A BreadcrumbItem instance has most of the methods seen above (note: no isUpdate()/isCreation() here, since a breadcrumb segment doesn't carry that information):

entityKey ​

isEntityList ​

isShow ​

isSingleShow ​

isForm ​

isSingleForm ​

instanceId ​

entityIs ​

Here's an example of how this information could be useful: imagine you have a Show for a Post instance, with an Embedded Entity List of Comment. When creating a new Comment, you'll need to set its post_id attribute on the Form update() method. You can for this make use of the breadcrumb context like this:

php
class CommentForm extends SharpForm
{
    // ...
    
    function update($id, array $data)
    {
        $comment = $id 
            ? Comment::find($id) 
            : new Comment([
                'post_id' => sharp()->context()
                    ->breadcrumb()
                    ->previousShowSegment(PostEntity::class)
                    ->instanceId()
            ]);

        $this->save($comment, $data);
        
        return $comment->id;
    }
}

Global and retained filters ​

globalFilterValue ​

Get the value of a Global Filter: see the Global Filter documentation to know more about this feature.

retainedFilterValue ​

Get the value of a retained Filter: see the Retained Filter documentation to know more about this feature.

Access instances cached by an Entity List ​

This feature is really useful to avoid multiple queries on the same instance in a single request. The specific documentation is available here.

Released under the MIT License.