API Platform Admin 0.2: an admin in 1 minute for your API (React Progressive Web App)

The version 0.2 of the API Platform‘s admin component has just been released!

This nice tool allows to automatically and dynamically build a fully featured administration interface (CRUD, pagination, relations…) for any API supporting the Hydra hypermedia vocabulary (more formats supported soon, see at the end of this article). 0 line of code required!

API Platform Admin is built with React on top of the famous Admin On Rest library as a Progressive Web App.

Let’s discover the bunch of new features that this version brings.

Getting Started

Assuming that you have an API exposing a Hydra documentation, you just have to initialize the following React component to get your admin:

For instance, create a new app with Facebook’s create-react-app, replace the content of src/App.js with the previous snippet and run yarn add @api-platform/admin. You’re done!

If you get an error related to multiple versions of React being loaded, just remove the react and react-dom packages from your project’s package.json and run yarn install again.

If you don’t have a JSON-LD / Hydra API yet, here is the code of the one I’ll use in the following examples. This API has been created using the API Platform’s distribution:

Yes, you just need those two tiny PHP classes to get a hypermedia API. Learn more about the API component by reading the getting started guide. But, really, any API with a Hydra documentation will do the job regardless of the server-side programming language.

Native Support for to-Many Relations

API Platform Admin supports to-one relations since its very first release. However it was mandatory to customize the component used for to-many relations. This isn’t the case anymore. Our API documentation parser gained support for cardinalities and can now extract them if the API documentation includes OWL’s maxCardinality properties.

If no cardinality is provided, the admin will use a to-many widget by default.

Thanks to this new feature, here is how the edition screen of the Person resource looks like this:

The admin is able to guess that the Person resource is related to many Greeting ones and use the appropriate Admin On Rest component.

Detection of More Schema.org’s Types (name, url and email)

API Platform Admin is able to guess the widget to use depending of the type of a resource’s property. It supports:

  • Numbers (http://www.w3.org/2001/XMLSchema#float and http://www.w3.org/2001/XMLSchema#integer ranges)
  • Dates (http://www.w3.org/2001/XMLSchema#date and http://www.w3.org/2001/XMLSchema#dateTime ranges)
  • Booleans (http://www.w3.org/2001/XMLSchema#boolean range)
  • And of course text fields

In this new release, Admin also supports some types of the popular Schema.org vocabulary:

  • As shown in the previous screenshots (e.g. Greetings select box), if a property has the type http://schema.org/name, this property will be used instead of the ID when displaying this relation
  • If a property has the type http://schema.org/url, the URL will be clickable when displayed in the admin
  • If a property has the type http://schema.org/email, the HTML input will be of type email and a basic validation will occur (this was already working in v0.1)

Support for Read-only Resources

The version 0.1 wasn’t able to deal with read-only resource (no POST nor PUT operation). We have improved the API doc parser to support owl:equivalentClass properties. Now, if the API documentation provide those properties, the admin will be builded even if the resource is read-only (of course in this case you will only be able to browse resources, and not to edit them).

Easier and Deeper Customization

Morgan Auchedé did an excellent work to make the Admin fully and easily customizable. You can now override any generated React component by a custom one, or one from Admin On Rest, or from MUI React. You can just replace (or ad, or remove) a specific input or field. But you can also replace a whole list, a show view, a creation or edition form or a remove button.

Here is an example of full customization, courtesy of Morgan:

Ability to Support Other Formats Such as GraphQL

The parser has been designed to be able to be parse other formats such as a GraphQL schema or Swagger/Open API. The api-doc-parser library provides an intermediate representation that is populated by the specific format parser. It’s this representation that is used by the parser as well as by our React and Vue.js Progressive Web App generator.

It means that when converters from other formats than Hydra to this intermediate representation will be available (Pull Requests welcome), both tools we’ll support those formats. As you may know, the server part of API Platform now supports GraphQL. You can guess which format we’ll implement next in the api-doc-parser!

Making the Sonata Project better

As a freelancer and with my company, Les-Tilleuls.coop, I built many applications relying on Sonata bundles for Symfony. I especially recommend the Admin generator and the Media manager.

As these components help me to build apps with ease and speed, it’s a fair reward to contribute back to them when I can. In addition to being the author and maintainer of the Doctrine MongoDB ODM compatibility layer of the Admin Bundle ; I’ve recently added some useful features to the Sonata Project and it’s time to give them more exposure.

ACL editor for the Admin Bundle

Sonata ACL editor

The ACL component of the Symfony framework is as powerful as complicated. The Sonata Admin bundle provides a useful integration with it allowing to restrict actions to authorized users only. What was missing was an interface to set the permissions. I’ve fixed that and this ACL editor is now provided with the Admin bundle. This is now matter of a click for an admin to give rights to users on entities.

If you want to use this feature in your project, refer to the Sonata Admin documentation that have been updated accordingly.

ACL list filtering for the admin

This is a perfect complement to the ACL editor previously described.

I’ve built a bundle called CoopTilleulsAclSonataAdminExtensionBundle (distributed separately) providing a Sonata admin extension that filter lists of entities. Only entities that the logged in user as the right to interact with are displayed.

It also works with the ACL system of the Symfony security component. The documentation is provided with the bundle.

Confirm dialog to avoid losing unsaved changes

Sonata confirm on exit

A common pitfall with web apps is that when a link is clicked, unsaved data entered in a form is loose without warning. Thanks to the integration of my small confirmExit plugin for jQuery, the Sonata Admin no longer suffers of this flaw. This feature can be disabled through a configuration parameter.

My jQuery plugin internally use the useful Window.onbeforeunload Javascript event.

Integration of Sonata Media in CKEditor

Sonata integration in CKEditor

The Sonata Media manager is the most powerful way to manage images, videos and files in Symfony powered applications. It supports YouTube, Vimeo and Dailymotion videos out of the box, image thumbnail generation and Amazon S3, FTP and CDN storage systems. It is highly extendable and is provided with a customized Sonata Admin UI.

What I missed was the ability to select and upload Media directly from CKEditor, the popular WYSYWG visual text editor. Those features is now available as a separate bundle (when I have time, I’ll move it directly in the SonataMedia bundle). The CoopTilleulsCKEditorSonataMediaBundle bundle allows to:

  • Upload an image through Sonata Media directly form the CKeditor’s image dialog
  • Select an existing Sonata Media file
  • Create a link to a media handled with Sonata through the CKeditor’s link button
  • Upload a file from the CKEditor’s link dialog

As always, the doc is included with the bundle.

I’ve also make some bug fixes on SonataAdmin, SonataMedia, SonataPage and SonataDoctrineORMAdminBundle. Hoping these enhancements will help some Symfony developers!

Une doc (spartiate) pour le support MongoDB de l’admin Sonata pour Symfony 2

Il y’a plus d’un an déjà j’ai contribué le support de MongoDB au générateur d’admintration Sonata pour Symfony2.

Le projet n’était jusque le pas du tout documenté,  j’ai donc mis en ligne une petite documentation inspirée de celle du composant pour l’ORM. Attention, elle s’affiche mal sur GitHub à cause du format RST, il faut télécharger les fichiers.

J’en ai également profité pour corriger le composer.json qui ne fonctionnait plus et je publierais bientôt sous licence libre la démo d’un tout petit moteur de blog qui utilise MongoDB et l’admin Sonata, en espérant que ça puisse servir à quelques-uns.