murph-doc/docs/crud/configuration.md

341 lines
14 KiB
Markdown
Raw Permalink Normal View History

2021-05-30 23:00:08 +02:00
# Configuration
2021-05-31 16:30:46 +02:00
A generated crud controller contains a method named `getConfiguration`. This methods returns a instance of `App\Core\Crud\CrudConfiguration`.
## setPageTitle
`setPageTitle(string $page, string $title)`
Set the title of the given page.
Example of usage in a CRUD template: `<title>{{ configuration.pageTitle('index') }}</title>`.
## setPageRoute
`setPageRoute(string $page, string $route)`
Set the route of the given page. By default, pages are: `index`, `edit`, `new`, `show`. You can create a custom page for a custom controller.
Example of usage in a CRUD template: `<a href="{{ path(configuration.pageRoute('new')) }}">...</a>`.
## setForm
`setForm(string $context, string $form, `array` $options = [])`
Set the form used in the given context.
## setFormOptions
`setFormOptions(string $context, `array` $options = [])`
Defines options given to a form.
## setAction
2022-05-10 19:47:39 +02:00
`setAction(string $page, string $action, bool|callable $enabled)`
2021-05-31 16:30:46 +02:00
2022-05-10 19:47:39 +02:00
Set if an action is enabled or not in the given page. Take a look at `core/Resources/views/admin/crud/*.html.twig` for more information. Depending the context, the callable could receive the entity in parameter. Example:
```
->setAction('index', 'edit', function(EntityInterface $entity) {
return $entity->getUser()->getId() === $this->getUser()->getId();
})
```
2021-05-31 16:30:46 +02:00
Usage in a CRUD template: `{% if configuration.action('index', 'new')%}...{% endif %}`.
## setActionTitle
`setActionTitle(string $page, string $action, string $title)`
Set the title of an action in the given page.
Example of usage in a CRUD template: `{{ configuration.actionTitle(context, 'new', 'New')|trans }}`
## setView
`setView(string $context, string $view)`
Override a view.
| Controller (context) | View | Description |
| ---------- | ---- | ----------- |
| `index` | `@Core/admin/crud/index.html.twig` | Template of the page `index` |
| `edit` | `@Core/admin/crud/edit.html.twig` | Template of the page `edit` |
| `new` | `@Core/admin/crud/new.html.twig` | Template of the page `new` |
| `show` | `@Core/admin/crud/show.html.twig` | Template of the page `show` |
| `filter` | `@Core/admin/crud/filter.html.twig` | Template of the page `filter` |
| Form (context) | View | Description |
| ---- | ---- | ----------- |
| `form` | `@Core/admin/crud/_form.html.twig` | Template to render a form |
| `form_widget` | `@Core/admin/crud/_form_widget.html.twig` | Template to render a form widget |
| `form_translations` | `@Core/admin/crud/_form_translations.html.twig` | Template to render a the translation field |
2023-07-29 13:37:08 +02:00
| Entity (context) | View | Description |
| ------ | ---- | ----------- |
| `show_entity` | `@Core/admin/crud/_show.html.twig` | Template to render the entity |
2021-05-31 16:30:46 +02:00
## setViewDatas
`setViewDatas(string $context, `array` $datas)` and `addViewData(string $context, string $name, $value)`
Add datas given to a view. Useful in a custom controller.
## setField
`setField(string $context, string $label, string $field, `array` $options)`
Add a field displayed in the given context. Used in the index.
```
use App\Core\Crud\Field;
$configuration->setField('index', 'Title', Field\TextField::class, [
2022-05-10 19:47:39 +02:00
// options
2021-05-31 16:30:46 +02:00
])
```
All fields have these options:
2023-08-11 17:24:55 +02:00
| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| `property` | `string` | `null` | Entity's property to display |
| `property__builder` | `callable` | `null` | A callable data and used to generate the content displayed |
| `view` | `string` | `@Core/admin/crud/field/text.html.twig` | The templated rendered |
2023-08-11 17:39:23 +02:00
| `default_value` | `string` | `null` | Default value to display when the property is `null` |
2023-08-11 17:24:55 +02:00
| `action` | `string` | `null` | An action to perform on click (`null`, `edit`, `view`) |
| `raw` | `boolean` | `false` | Render as HTML |
| `sort` | `array | callable` | `null` | Defines how to sort |
| `href` | `string | callable` | `null` | Data to generate a link |
| `href_attr` | `array | callable` | `null` | Attributes of the link |
| `inline_form` | `null | callable` | `null` | A method to define a form to edit datas |
| `inline_form_validation` | `null | callable` | `null` | A method to define a custom form validation callback |
2021-05-31 16:30:46 +02:00
2023-04-14 06:56:50 +02:00
```php-inline title="Example #0"
2021-05-31 16:30:46 +02:00
$configuration->setField('index', 'My field', TextField::class, [
2022-05-10 19:47:39 +02:00
'property' => 'myProperty',
// OR
'property_builder' => function($entity, array $options) {
return $entity->getMyProperty();
},
2021-05-31 16:30:46 +02:00
])
2023-04-14 06:55:20 +02:00
```
2021-05-31 16:30:46 +02:00
2023-04-14 06:56:50 +02:00
```php-inline title="Example #1"
2021-05-31 16:30:46 +02:00
$configuration->setField('index', 'My field', TextField::class, [
2022-05-10 19:47:39 +02:00
'raw' => true,
'property_builder' => function($entity, array $options) {
return sprintf('<span class="foo">%s</span>', $entity->getBar());
},
2021-05-31 16:30:46 +02:00
])
2023-04-14 06:55:20 +02:00
```
2021-05-31 16:30:46 +02:00
2023-04-14 06:56:50 +02:00
```php-inline title="Example #2"
2021-05-31 16:30:46 +02:00
// https://127.0.0.7:8000/admin/my_entity?_sort=property&_sort_direction=asc
$configuration->setField('index', 'My field', TextField::class, [
2022-05-10 19:47:39 +02:00
'property' => 'myProperty'
'sort' => ['property', '.myProperty'],
// OR
'sort' => ['property', function(MyEntityRepositoryQuery $query, $direction) {
$query->orderBy('.myProperty', $direction);
}],
2021-05-31 16:30:46 +02:00
])
2023-04-14 06:55:20 +02:00
```
2023-04-14 06:52:26 +02:00
2023-04-14 06:56:50 +02:00
```php-inline title="Example #3"
2023-04-14 06:52:26 +02:00
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\NotBlank;
$configuration->setField('index', 'My field', TextField::class, [
'property' => 'myProperty',
'inline_form' => function(FormBuilderInterface $builder, EntityInterface $entity) {
$builder->add('myProperty', TextType::class, [
'required' => true,
'constraints' => [new NotBlank()],
]);
}
])
2021-05-31 16:30:46 +02:00
```
### TextField
`App\Core\Crud\Field\TextField`
2023-07-28 09:18:01 +02:00
| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| `view` | `string` | `@Core/admin/crud/field/text.html.twig` | The templated rendered |
2021-05-31 16:30:46 +02:00
### DateField
`App\Core\Crud\Field\DateField`
2023-07-28 09:18:01 +02:00
| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
2021-05-31 16:30:46 +02:00
| `view` | `string` | `@Core/admin/crud/field/date.html.twig` | The templated rendered |
| `format` | `string` | `Y-m-d` | The date format |
### DatetimeField
`App\Core\Crud\Field\DatetimeField`
2023-07-28 09:18:01 +02:00
| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
2021-05-31 16:30:46 +02:00
| `view` | `string` | `@Core/admin/crud/field/date.html.twig` | The templated rendered |
| `format` | `string` | `Y-m-d H:i:s` | The date format |
### ButtonField
`App\Core\Crud\Field\ButtonField`
2023-07-28 09:18:01 +02:00
| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| `view` | `string` | `@Core/admin/crud/field/button.html.twig` | The templated rendered |
| `button_attr` | `array` | `[]` | Button HTML attributes |
| `button_attr_builder` | `callabled` | `null` | A callable data and used to generate `button__attr` |
| `button_tag` | `string` | `button` | HTML tag of the button |
2021-05-31 16:30:46 +02:00
### ImageField
`App\Core\Crud\Field\ImageField`
2023-07-28 09:18:01 +02:00
| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| `view` | `string` | `@Core/admin/crud/field/image.html.twig` | The templated rendered |
| `image_attr` | `array` | `[]` | Image HTML attributes |
### BooleanField
`App\Core\Crud\Field\BooleanField`
2023-08-31 18:00:54 +02:00
| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| `view` | `string` | `@Core/admin/crud/field/boolean.html.twig` | The templated rendered |
| `display` | `string` | `toggle` | Type of render (`toggle` or `checkbox`) |
| `checkbox_class_when_true` | `string` | `fa-check-square` | HTML class added when the value is `true` and display is `checkbox` |
| `checkbox_class_when_false` | `string` | `fa-square` | HTML class added when the value is `false` and display is `checkbox` |
| `toggle_class_when_true` | `string` | `bg-success` | HTML class added when the value is `true` and display is `toggle` |
| `toggle_class_when_false` | `string` | `bg-secondary` | HTML class added when the value is `false` and display is `toggle` |
2021-05-31 16:30:46 +02:00
## setMaxPerPage
`setMaxPerPage(string $page, int $max)`
Set how many elements are displayed in a single page.
## setI18n
`setI18n(array $locales, string $defaultLocale)`
Set an array of locales for a translatable entity. The default locale is used in the index page.
Compatible with [https://github.com/KnpLabs/DoctrineBehaviors/blob/master/docs/translatable.md](https://github.com/KnpLabs/DoctrineBehaviors/blob/master/docs/translatable.md).
## setDefaultSort
`setDefaultSort(string $context, string $label, string $direction = 'asc')`
Set the default sort applied in the repository query.
2023-04-14 06:58:16 +02:00
```php-inline
2021-05-31 16:30:46 +02:00
$configuration
2022-05-10 19:47:39 +02:00
->setDefaultSort('index', 'title', 'asc')
->setField('index', 'Title', Field\TextField::class, [
'property' => 'title',
'sort' => ['title', '.title'],
]);
2021-05-31 16:30:46 +02:00
```
## setIsSortableCollection
`setIsSortableCollection(string $page, bool $isSortableCollection)`
It enables the drag & drop to sort entities.
2023-04-14 06:58:16 +02:00
```php-inline
2021-05-31 16:30:46 +02:00
class MyEntity implements EntityInterface
{
2022-05-10 19:47:39 +02:00
// ...
2021-05-31 16:30:46 +02:00
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $sortOrder;
public function getSortOrder(): ?int
{
return $this->sortOrder;
}
public function setSortOrder(?int $sortOrder): self
{
$this->sortOrder = $sortOrder;
return $this;
}
2022-05-10 19:47:39 +02:00
// ...
2021-05-31 16:30:46 +02:00
}
```
## setSortableCollectionProperty
`setSortableCollectionProperty(string $sortableCollectionProperty)`
In order to sort entities, the default property used is `sortOrder`. You can set something else.
2021-06-04 10:48:41 +02:00
## setBatchAction
`setBatchAction(string $context, string $action, string $label, callable $callack)`
Add a batch action. The callback has 2 arguments:
* An instance of `App\Core\Entity\EntityInterface`
* An instance of `App\Core\Manager\EntityManager`
2023-04-14 06:59:01 +02:00
```php-inline
2021-06-04 10:48:41 +02:00
use App\Core\Entity\EntityInterface;
use App\Core\Manager\EntityManager;
$configuration->setBatchAction(
'index',
'delete',
'Delete',
function(EntityInterface $entity, EntityManager $manager) {
$manager->delete($entity);
}
);
```
## setGlobalBatchAction
`setGlobalBatchAction(string $context, string $action, string $label, callable $callack)`
Add a global batch action. The callback has 3 arguments:
* An instance of `App\Core\Repository\RepositoryQuery`
* An instance of `App\Core\Manager\EntityManager`
* An array of selected entities or a `null` value
Do not use the same action in global and classic batch action.
The callback can return a response. If not, the user will be redirect automatically. See the example below:
```php-inline
use App\Core\Entity\RepositoryQuery;
use App\Core\Manager\EntityManager;
use Symfony\Component\HttpFoundation\JsonResponse;
$configuration->setGlobalBatchAction(
'index',
'export_json',
'Export to JSON',
function(RepositoryQuery $query, EntityManager $manager, ?array $selection): JsonResponse {
$items = $selection ?? $query->find();
return $this->json($items);
}
);
```