Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/documentation of preselect definition #69

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Cookbook
** [Overview](cookbook/overview.md)
** - [Login](cookbook/login.md)
** - [View Definition](cookbook/view_definition.md)
** - [Filters](cookbook/filters.md)
** - [Search](cookbook/search.md)
** - [Custom Styles](cookbook/custom_styles.md)
Expand Down
3 changes: 3 additions & 0 deletions docs/cookbook/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
- [How to remove an unwanted filter](cookbook/filters.md#how-to-remove-an-unwanted-filter)
- [How to predefine often used filters](cookbook/filters.md#how-to-predefine-often-used-filters)

- [View Definition](cookbook/view_definition.md)
- [How to prefill a relation field in forms](cookbook/view_definition.md#prefill-field-in-form)

- [Search](cookbook/search.md)
- [Prerequisites](cookbook/search.md#prerequisites)
- [Search on Definition](cookbook/search.md#search-on-definition)
Expand Down
75 changes: 75 additions & 0 deletions docs/cookbook/view_definition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# View Definition

## Prefill a relation field in forms
You can define which data row the definition uses as the default value of an input field.
This way you can either give the user advice on what you think might be right in this situation or, if you hide the input field, you can automatically fill in the correct relation for the user without him having to do anything.

As an example we use an imaginary Book/Author management software: We would like to add a button to an authors page that allows us to create a new book with the author already prefilled.
In order to achieve this, we need to add the `Content::OPT_PRESELECT_DEFINITION` and the `class` form option to the content of the `BookDefinition` like so:

```php
// BookDefinition
public function configureView(DefinitionBuilder $builder, $data): void
{
parent::configureView($builder, $data);

$builder
->addBlock('base')
->addContent('author', null, [
Content::OPT_DEFAULT_VALUE => $entity->getAuthor(),
Content::OPT_PRESELECT_DEFINITION => AuthorDefinition::class,
Content::OPT_FORM_OPTIONS => [
'class' => Author::class,
],
])
;
}
```

### Linking to the prefilled form
On the other side, in the AuthorDefinition, we can now create the link to this page like so:

```php
// AuthorDefinition
public function configureActions(mixed $data): void
{
parent::configureActions($data);

if ($data && $this->getPage() === Page::SHOW)
{
$this->addAction('add_book', [
'route' => BookDefinition::getRoute(Page::CREATE),
'route_parameters' => [
self::getQueryAlias() => $data->getId(),
],
]);
}
}
```

This adds a button on the detail page of an author that links to the create page of a book with the author preselected.

### Hiding the input field
If you want to hide the input field in the form, you can do so by using the `EntityHiddenType` as the form type like so:

```php
// BookDefinition
public function configureView(DefinitionBuilder $builder, $data): void
{
parent::configureView($builder, $data);

$builder
->addBlock('base')
->addContent('author', null, [
Content::OPT_DEFAULT_VALUE => $entity->getAuthor(),
Content::OPT_PRESELECT_DEFINITION => AuthorDefinition::class,
Content::OPT_FORM_OPTIONS => [
'class' => Author::class,
],
Content::OPT_FORM_TYPE => EntityHiddenType::class,
])
;
}
```

This way the user will not see the input field but the value will still be prefilled.
82 changes: 41 additions & 41 deletions docs/exporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,59 @@ The CrudBundle allows you to easily export your data from a Table View to CSV fi

1: Enable the export route in your definition:
```php
/**
* {@inheritdoc}
*/
public static function getCapabilities()
{
return [
Page::INDEX,
Page::SHOW,
Page::DELETE,
Page::EDIT,
Page::CREATE,
Page::EXPORT <----- Export Route
];
}
/**
* {@inheritdoc}
*/
public static function getCapabilities()
{
return [
Page::INDEX,
Page::SHOW,
Page::DELETE,
Page::EDIT,
Page::CREATE,
Page::EXPORT <----- Export Route
];
}
```

You now see an export button at the bottom of your table.

By default the table configuration will be exported.
By default, the table configuration will be exported.

## Customization

To define your custom export, just override the `configureExport` method. Create columns as you need them.

```php
public function configureExport(Table $table)
{
$this->configureTable($table);
public function configureExport(Table $table)
{
$this->configureTable($table);

$table->addColumn('id', null, [
Column::OPTION_PRIORITY => 200
])
->addColumn('jobTitle');
}
$table->addColumn('id', null, [
Column::OPTION_PRIORITY => 200
])
->addColumn('jobTitle');
}
```



## Export Column options

```php
public function configureTable(Table $table): void
{
parent::configureTable($table);
$table
->addColumn('name', null, [
Column::OPTION_EXPORT => [
Column::OPTION_EXPORT_EXPORTABLE => false
Column::OPTION_EXPORT_TEXTWRAP => true
]
])
;
}
public function configureTable(Table $table): void
{
parent::configureTable($table);
$table
->addColumn('name', null, [
Column::OPTION_EXPORT => [
Column::OPTION_EXPORT_EXPORTABLE => false
Column::OPTION_EXPORT_TEXTWRAP => true
]
])
;
}
```

## Multiple Exporter
Expand Down Expand Up @@ -96,10 +96,10 @@ class ExampleExporter implements ExporterInterface
In your definition you can add your new exporter to the table. For this override the `configureTableExporter` method and pass the acronym and the exporter:

```php
public function configureTableExporter(Table $table): void
{
parent::configureTableExporter($table);
# translation key in this example equals to -> wwd.app_entity_example.exporter.acronym_for_translation
$table->addExporter('acronym_for_translation', $this->exampleExporter);
}
public function configureTableExporter(Table $table): void
{
parent::configureTableExporter($table);
# translation key in this example equals to -> wwd.app_entity_example.exporter.acronym_for_translation
$table->addExporter('acronym_for_translation', $this->exampleExporter);
}
```
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link rel="icon" href="media/favicon-32x32.png" sizes="32x32">
<meta name="description" content="This bundle is part of the araise.dev framework.">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify-themeable@0/dist/css/theme-simple-dark.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsify-themeable@0/dist/css/theme-simple-dark.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@500;700&display=swap" rel="stylesheet">
Expand Down
39 changes: 20 additions & 19 deletions docs/view-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,39 @@ All Relation are handled with the [RelationContent](/contents/relation-content.m
For example to add the books relation the an author entity it would look like this:

````php
$builder
->addBlock( 'base')
->addContent('books')
;
$builder
->addBlock( 'base')
->addContent('books')
;
````
Under the hood we recognize the relation and create a [RelationContent](/contents/relation-content.md) for you.
````php
$builder
->addBlock( 'base')
->addContent('books', RelationContent::class, [
'label' => 'Books',
])
;
$builder
->addBlock( 'base')
->addContent('books', RelationContent::class, [
'label' => 'Books',
])
;
````

### Ajax in Create/Edit
You can use Ajax on the edit and create pages.
To do so active the capability on this definition:
```php
public static function getCapabilities(): array
{
return array_merge(parent::getCapabilities(), [Page::AJAXFORM]);
}
public static function getCapabilities(): array
{
return array_merge(parent::getCapabilities(), [Page::AJAXFORM]);
}
```
You also need to choose which fields will trigger an ajax request.
For example while creating a Book entity we want the authors name prefixed as book name.
```php
$builder
->addBlock('base')
->addContent('author', null, [
Content::OPT_AJAX_FORM_TRIGGER => true,
])
$builder
->addBlock('base')
->addContent('author', null, [
Content::OPT_AJAX_FORM_TRIGGER => true,
])
;
```
Then override the `ajaxForm` function. For the described example above this coud look something like this:
```php
Expand Down
Loading