Sharp Documentation

website·Indexed 33 days ago

https://sharp7.code16.fr/docs/guide/

A framework for building administration interfaces in Laravel, featuring Entity Lists, Forms, Show pages, and Dashboards. The documentation covers form and show fields, Artisan generators, data transformation, entity states, and upgrading guides from version 4.0 through 7.x.

Tokens
39.6K
Snippets
232
Records
303
Agent score
59%

What's inside Sharp

  1. Overview of the Geolocation form field

    The Geolocation field (Code16\Sharp\Form\Fields\SharpFormGeolocationField) provides a map-based interface for users to select a precise location. It returns the coordinates as latitude and longitude.
    // Class used for Geolocation fields
    Code16\Sharp\Form\Fields\SharpFormGeolocationField
  2. Overview of SharpFormEditorField

    The SharpFormEditorField is a rich text editor form field that supports formatting and an optional toolbar. It is used for capturing structured text content that may include embedded media or custom data embeds.
    use Code16\Sharp\Form\Fields\SharpFormEditorField;
  3. Display dynamic HTML content with SharpFormHtmlField

    The Code16\Sharp\Form\Fields\SharpFormHtmlField is a read-only field designed to display dynamic information within a form. It does not accept input from the user (fromFront returns null) and sends data to the front-end as provided by the transformer.
  4. Define a Sharp entity class

    An entity in Sharp is a data structure (typically a Model) that represents a business concept. To create an entity, you must create a class that extends Code16\Sharp\Entities\SharpEntity. This class serves as the central configuration point for the entity's List, Form, Show page, and authorization policies.
    use Code16\Sharp\Entities\SharpEntity;
    
    class SpaceshipEntity extends SharpEntity
    {
        protected ?string $list = SpaceshipSharpList::class;
        protected ?string $show = SpaceshipSharpShow::class;
        protected ?string $form = SpaceshipSharpForm::class;
        protected string $label = "Spaceship";
    }
  5. Define a list of complex items using SharpShowListField

    Use the Code16\Sharp\Show\Fields\SharpShowListField class to display a list where each item consists of multiple other Show fields. You can define the internal structure of each list item by chaining addItemField() calls.
    function buildShowFields()
    {
        $this->addField(
            SharpShowListField::make("pictures")
                ->setLabel("additional pictures")
                ->addItemField(
                    SharpShowFileField::make("file")
                        ->setStorageDisk("local")
                        ->setStorageBasePath("data/Spaceship/{id}/Pictures")
                )
                ->addItemField(
                    SharpShowTextField::make("legend")
                        ->setLabel("Legend")
                )
        );
    }
  6. Update FormUploadModelTransformer class for Sharp 5.x

    In Sharp 5.x, the Code16\Sharp\Form\Eloquent\Transformers\FormUploadModelTransformer class has been refactored and renamed. Update your imports and references to use the new class name: Code16\Sharp\Form\Eloquent\Uploads\Transformers\SharpUploadModelAttributeTransformer.
  7. Transform attributes in hasMany relationships

    To transform an attribute of a related model within a 'has many' relationship, use array notation in the attribute key. For example, if a model has a sons relationship, use sons[attribute_name] to target the attribute of each related object in the collection.
    return $this
        ->setCustomTransformer(
            "sons[name]",
            function($son) {
                return strtoupper($son->name);
            }
        )
        ->transform($father);
  8. Transform localized data for Sharp Forms

    Sharp expects localized fields to be provided as a key/value array where the keys are the locale codes.

    When retrieving data in find(), use setCustomTransformer to map database columns to the locale array. When saving in update(), extract the locale values from the array and use ignore() to prevent Sharp from attempting to save the raw localization array directly to the model.

    function find($id): array
    {
        return $this->setCustomTransformer("title", function($title, $book) {
                return [
                   "fr" => $book->title_french,
                   "en" => $book->title_english
                ];
            })
            ->transform(
                Book::findOrFail($id)
            );
    }
    
    function update($id, array $data)
    {
        $instance = $id ? Book::findOrFail($id) : new Book;
    
        $data["title_french"] = $data["title"]["fr"];
        $data["title_english"] = $data["title"]["en"];
    
        $this
            ->ignore("title")
            ->save($instance, $data);
    
        return $instance->id;
    }
  9. Replace SharpUploadModelAttributeTransformer

    The SharpUploadModelAttributeTransformer class has been removed. When using a transformer for SharpUploadModel in an EntityList, use SharpUploadModelThumbnailUrlTransformer instead.
    ->setCustomTransformer("picture", (new SharpUploadModelThumbnailUrlTransformer(100))->renderAsImageTag())
  10. Embed an Entity List in a Show Page

    You can display a related "one-to-many" list of entities within a Show Page using SharpShowEntityListField. Sharp will look for the corresponding sharp.entities.{entity}.list configuration to render the list with full functionality (filters, commands, etc.).

    To include this in the layout, use addEntityListSection() in the buildShowLayout method.

    // In buildShowFields
    function buildShowFields(FieldsContainer $showFields): void
    {
        $showFields->addField(SharpShowEntityListField::make("pilots", "pilot"));
    }
    
    // In buildShowLayout
    function buildShowLayout(ShowLayout $showLayout): void
    {
        $showLayout->addEntityListSection('members');
    }
  11. Refactor Dashboard widgets and layout to use proxy objects

    Sharp 7.x uses proxy objects for Dashboard configuration.

    1. buildWidgets() is renamed to buildWidgets(WidgetsContainer $widgetsContainer). Use $widgetsContainer->addWidget().
    2. buildWidgetsLayout() is renamed to buildDashboardLayout(DashboardLayout $dashboardLayout). Use $dashboardLayout->addRow() or $dashboardLayout->addFullWidthWidget().
    function buildWidgets(WidgetsContainer $widgetsContainer): void
    {
        $widgetsContainer->addWidget(
            SharpBarGraphWidget::make("travels")
        );
    }
    
    function buildDashboardLayout(DashboardLayout $dashboardLayout): void
    {
        $dashboardLayout->addRow(function(DashboardLayoutRow $row) {
            $row->addWidget(6, "types_pie")
                ->addWidget(6, "features_bars");
        });
    }