murph-doc/docs/crud/configuration.md
Simon Vieille 871e17d232
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
add boolean field
2023-07-28 09:18:01 +02:00

11 KiB

Configuration

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

setAction(string $page, string $action, bool|callable $enabled)

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();
})

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
Entity (context) View Description
show_entity @Core/admin/crud/_show.html.twig

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, [
    // options
])

All fields have these options:

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
action string null An action to perform on click (null, edit, view)
raw boolean false Render as HTML
sort `array callable` null
href `string callable` null
href_attr `array callable` null
inline_form `null callable` null
$configuration->setField('index', 'My field', TextField::class, [
    'property' => 'myProperty',
    // OR
    'property_builder' => function($entity, array $options) {
        return $entity->getMyProperty();
    },
])
$configuration->setField('index', 'My field', TextField::class, [
    'raw' => true,
    'property_builder' => function($entity, array $options) {
        return sprintf('<span class="foo">%s</span>', $entity->getBar());
    },
])
// https://127.0.0.7:8000/admin/my_entity?_sort=property&_sort_direction=asc
$configuration->setField('index', 'My field', TextField::class, [
    'property' => 'myProperty'
    'sort' => ['property', '.myProperty'],
    // OR
    'sort' => ['property', function(MyEntityRepositoryQuery $query, $direction) {
        $query->orderBy('.myProperty', $direction);
    }],
])
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()],
        ]);
    }
])

TextField

App\Core\Crud\Field\TextField

Option Type Default Description
view string @Core/admin/crud/field/text.html.twig The templated rendered

DateField

App\Core\Crud\Field\DateField

Option Type Default Description
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

Option Type Default Description
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

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

ImageField

App\Core\Crud\Field\ImageField

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

Option Type Default Description
view string @Core/admin/crud/field/boolean.html.twig The templated rendered

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.

setDefaultSort

setDefaultSort(string $context, string $label, string $direction = 'asc')

Set the default sort applied in the repository query.

$configuration
    ->setDefaultSort('index', 'title', 'asc')
    ->setField('index', 'Title', Field\TextField::class, [
        'property' => 'title',
        'sort' => ['title', '.title'],
    ]);

setIsSortableCollection

setIsSortableCollection(string $page, bool $isSortableCollection)

It enables the drag & drop to sort entities.

class MyEntity implements EntityInterface
{
    // ...

    /**
     * @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;
    }

    // ...
}

setSortableCollectionProperty

setSortableCollectionProperty(string $sortableCollectionProperty)

In order to sort entities, the default property used is sortOrder. You can set something else.

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
use App\Core\Entity\EntityInterface;
use App\Core\Manager\EntityManager;

$configuration->setBatchAction(
    'index',
    'delete',
    'Delete',
    function(EntityInterface $entity, EntityManager $manager) {
        $manager->delete($entity);
    }
);