Skip to content

Commit db1cb96

Browse files
committed
docs: revamp schema.org doc
1 parent c873a42 commit db1cb96

File tree

4 files changed

+141
-37
lines changed

4 files changed

+141
-37
lines changed

admin/images/admin-filter.png

47.4 KB
Loading

admin/images/admin-sort.png

99.3 KB
Loading

admin/schema-org.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Customizing the Schema
2+
3+
Both [`HydraAdmin`](./components.md#hydraadmin) and [`OpenApiAdmin`](./components.md#openapiadmin) leverage introspection of the API's schema to discover its capabilities, like **filtering** and **sorting**.
4+
5+
They also detect wether the API has real-time capabilities using [Mercure](./real-time-mercure.md), and automatically enable it if it does.
6+
7+
Lastly, API Platform Admin has native support for the popular [Schema.org](#about-schemaorg) vocabulary, which enables it to automatically use the field type matching your data, or display a related resource's name instead of its IRI.
8+
9+
## Adding Filtering Capabilities
10+
11+
You can add the [`ApiFilter` attribute](../core/filters.md#apifilter-attribute) to an Entity to configure a filter on a property.
12+
13+
For instance, here is how configure filtering on the `id`, `title` and `author` properties of a `Book` entity:
14+
15+
```php
16+
<?php
17+
// api/src/Entity/Book.php
18+
namespace App\Entity;
19+
20+
use ApiPlatform\Metadata\ApiFilter;
21+
use ApiPlatform\Metadata\ApiResource;
22+
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
23+
use Doctrine\ORM\Mapping as ORM;
24+
25+
#[ApiResource]
26+
#[ORM\Entity]
27+
#[ApiFilter(SearchFilter::class, properties: [
28+
'id' => 'exact',
29+
'title' => 'ipartial',
30+
'author' => 'ipartial'
31+
])]
32+
class Book
33+
{
34+
// ...
35+
}
36+
```
37+
38+
If you are using the guessers, the Admin will automatically update the Book list view to include a filter on the selected properties.
39+
40+
![Filtering on the title property](./images/admin-filter.png)
41+
42+
**Tip:** Learn more about the [`ApiFilter` attribute](../core/filters.md#apifilter-attribute) in the core documentation.
43+
44+
## Adding Sorting Capabilities
45+
46+
You can also use the [`ApiFilter` attribute](../core/filters.md#apifilter-attribute) on an Entity to configure sorting.
47+
48+
For instance, here is how to configure sorting on the `id`, `isbn`, `title`, `author` and `publicationDate` properties of a `Book` entity:
49+
50+
```php
51+
<?php
52+
// api/src/Entity/Book.php
53+
namespace App\Entity;
54+
55+
use ApiPlatform\Metadata\ApiFilter;
56+
use ApiPlatform\Metadata\ApiResource;
57+
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
58+
use Doctrine\ORM\Mapping as ORM;
59+
60+
#[ApiResource]
61+
#[ORM\Entity]
62+
#[ApiFilter(OrderFilter::class, properties: [
63+
'id' => 'ASC',
64+
'isbn' => 'ASC',
65+
'title' => 'ASC',
66+
'author' => 'ASC',
67+
'publicationDate' => 'DESC'
68+
])]
69+
class Book
70+
{
71+
// ...
72+
}
73+
```
74+
75+
If you are using the guessers, the Admin will automatically update the Book list view to make the selected columns sortable.
76+
77+
![Sorting by the title property](./images/admin-sort.png)
78+
79+
**Tip:** Learn more about the [`ApiFilter` attribute](../core/filters.md#apifilter-attribute) in the core documentation.
80+
81+
## Enabling Real-Time Updates
82+
83+
You can use the `mercure` attribute to hint API Platform that it must dispatch the updates regarding the given resources to the Mercure hub:
84+
85+
```php
86+
<?php
87+
// api/src/ApiResource/Book.php with Symfony or app/ApiResource/Book.php with Laravel
88+
namespace App\ApiResource;
89+
90+
use ApiPlatform\Metadata\ApiResource;
91+
92+
#[ApiResource(mercure: true)]
93+
class Book
94+
{
95+
// ...
96+
}
97+
```
98+
99+
Make sure you have [configured your dataProvider to support Mercure](./real-time-mercure.md).
100+
101+
**Tip:** Learn more about how to use the Mercure Protocol in the [API Platform Core documentation](../core/mercure.md).
102+
103+
## About Schema.org
104+
105+
API Platform Admin has native support for the popular [Schema.org](https://schema.org) vocabulary.
106+
107+
> Schema.org is a collaborative, community activity with a mission to create, maintain, and promote schemas for structured data on the Internet, on web pages, in email messages, and beyond.
108+
109+
To leverage this capability, your API must use the JSON-LD format and the appropriate Schema.org types.
110+
The following examples will use [API Platform Core](../core/) to create such API, but keep in mind that this feature will work with any JSON-LD API using the Schema.org vocabulary, regardless of the used web framework or programming language.
111+
112+
## Displaying Related Resource's Name Instead of its IRI
113+
114+
By default, IRIs of related objects are displayed in lists and forms.
115+
However, it is often more user-friendly to display a string representation of the resource (such as its name) instead of its ID.
116+
117+
To configure which property should be shown to represent your entity, map the property containing the name of the object with the `https://schema.org/name` type:
118+
119+
```php
120+
// api/src/Entity/Person.php
121+
...
122+
123+
#[ApiProperty(iris: ["https://schema.org/name"])]
124+
private $name;
125+
126+
...
127+
```
128+
129+
## Emails, URLs and Identifiers
130+
131+
Besides, it is also possible to use the documentation to customize some fields automatically while configuring the semantics of your data.
132+
133+
The following Schema.org types are currently supported by API Platform Admin:
134+
135+
- `https://schema.org/email`: the field will be rendered using the [`<EmailField>`](https://marmelab.com/react-admin/EmailField.html) React Admin component
136+
- `https://schema.org/url`: the field will be rendered using the [`<UrlField>`](https://marmelab.com/react-admin/UrlField.html) React Admin component
137+
- `https://schema.org/identifier`: the field will be formatted properly in inputs
138+
139+
Note: if you already use validation on your properties, the semantics are already configured correctly (see [the correspondence table](../core/validation.md#open-vocabulary-generated-from-validation-metadata))!
140+
141+
**Next step:** Learn how to tweak the generated Admin by [Customizing the Guessers](./customizing.md).

admin/schema.org.md

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)