Sharp for Laravel 4.1 is now available
A quick overview of what’s new in this version
More than a year after 4.0, we at Code 16 are proud to present the 4.1 version of Sharp for Laravel, the open source content management framework we use a lot in our client projects. This post quickly presents the new features of this version.
Note: if you are not familiar with Sharp, you can find a 6-part presentation on Medium, starting here.
Dashboards. Dashboards everywhere.
First, the dashboard generalization feature; since I’ve already wrote a post about this one a few days ago on Medium, it feels unnecessary to say more about it here.
Data localization
This is the main part of the 4.1 version: we added a way to handle localized data in forms.
Let’s first say that in some simple cases, this specific feature is not needed: in a blog, for instance, we could manage English an Italian posts with a simple language filter in the Entity List. Here we address a more complex issue, which is sharing common data between versions and localize only text data. In our blog example, this would mean that only the post title and the body text are localized, but the post date, the visual, the author are shared and stored once.
In the screenshot above, we can see that “name” and “legend” fields (one is even in a list item) are localized; other fields are shared between languages. More precisely, FR
and EN
versions of the “name” attribute are set, but IT
is missing.
Here’s how localization would be configured in the Form code:
class SpaceshipSharpForm extends SharpForm
{
function buildFormFields()
{
$this->addField(
SharpFormTextField::make("name")
->setLocalized()
->setLabel("Name")
)->addField(
SharpFormUploadField::make("picture")
->setLabel("Picture")
->setFileFilterImages()
->setCropRatio("1:1", ["jpg","jpeg","png"])
->setStorageDisk("local")
->setStorageBasePath("data/Spaceship/{id}")
)->addField(
SharpFormTextField::make("picture:legend")
->setLocalized()
->setLabel("Legend")
);
[...] }
function getDataLocalizations()
{
return ["fr", "en", "it"];
} [...]
}
Last thing is, of course, to provide and handle localized data. The value of an attribute linked to a localized field must be formatted like this:
name: {
fr: 'Voyageur deux',
en: 'Voyager two'
}
As always, Sharp will not require you to store your data in a certain way, so to obtain this you can use a custom transformer—but to be honest, the real idea behind this is to really store the localized data in this fashion, in a JSON
or a TEXT
DB field, which is the case with this popular Spatie package for instance. In this case, there is nothing to change here: it will work out of the box.
A new UI for filters and commands
OK, this isn’t a new feature. The idea is to slowly work on the general Sharp UI, to smooth things out; we started with the most problematic part, in my opinion: filters and commands, in the Entity List. Here’s a sample of the new user interface:
Misplaced orange color and cryptic [+] icons are gone. Entity commands action button found a much place above the list, and each row can clearly display an entity state selector and an instance commands list.
And small other stuff
First, as it was requested a long time ago, we can now replace the default sharp URL segment with something else:
// In config/sharp.phpreturn [ "custom_url_segment" => "admin",
[...]];
A description can be added to a Command:
class SomeCommand extends EntityCommand
{
public function label(): string
{
return "Synchronize the gamma-spectrum";
}
public function description(): string
{
return "Let's be honest: this command is a fraud. It's just an empty command for test purpose.";
} [...]
}
Last small feature: we can now add a separator between Commands, to group things:
class SpaceshipSharpList extends SharpEntityList
{
[...]
function buildListConfig()
{
$this->addInstanceCommand("message", SpaceshipSendMessage::class)
->addInstanceCommand("preview", SpaceshipPreview::class)
->addInstanceCommandSeparator()
->addInstanceCommand("external", SpaceshipExternalLink::class);
} [...]
}
That’s all for this overview; I’ll write a dedicated post on data localization with a full example, since this feature is quite important. In the meantime, you can find Sharp 4.1 on Github, with an upgrade guide from 4.0 (it should be very quick).