Skip to content

Data list - Create a custom Datalist class#

namespace App\Controller;

use App\Datalist\Type\NewsDatalistType;
use App\Repository\NewsRepository;
use Leapt\CoreBundle\Datalist\DatalistFactory;
use Leapt\CoreBundle\Datalist\Datasource\DoctrineORMDatasource;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class NewsController extends AbstractController
{
    public function __construct(
        private DatalistFactory $datalistFactory,
        private NewsRepository $newsRepository,
    ) {
    }

    public function index(Request $request): Response
    {
        $queryBuilder = $this->newsRepository->createQueryBuilder('e')
            ->orderBy('e.publicationDate', 'DESC');

        $datalist = $this->datalistFactory
            ->createBuilder(NewsDatalistType::class)
            ->getDatalist();

        $datalist->setRoute($request->attributes->get('_route'))
            ->setRouteParams($request->query->all());
        $datasource = new DoctrineORMDatasource($queryBuilder);
        $datalist->setDatasource($datasource);
        $datalist->bind($request);

        return $this->render('news/index.html.twig', [
            'datalist' => $datalist,
        ]);
    }
}
namespace App\Datalist\Type;

use App\Entity\News;
use Leapt\CoreBundle\Datalist\Action\Type\SimpleActionType;
use Leapt\CoreBundle\Datalist\DatalistBuilder;
use Leapt\CoreBundle\Datalist\Field\Type\DateTimeFieldType;
use Leapt\CoreBundle\Datalist\Field\Type\TextFieldType;
use Leapt\CoreBundle\Datalist\Filter\Type\SearchFilterType;
use Leapt\CoreBundle\Datalist\Type\DatalistType;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class NewsDatalistType extends DatalistType
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        parent::configureOptions($resolver);

        $resolver
            ->setDefaults([
                'limit_per_page' => 10,
                'data_class'     => News::class,
            ])
        ;
    }

    public function buildDatalist(DatalistBuilder $builder, array $options): void
    {
        $builder
            ->addField('title', TextFieldType::class, [
                'label' => 'news.title',
            ])
            ->addField('publicationDate', DateTimeFieldType::class, [
                'label'  => 'news.publication_date',
                'format' => 'Y/m/d',
            ])
            ->addFilter('title', SearchFilterType::class, [
                'label'         => 'news.title',
                'search_fields' => ['e.title'],
            ])
            ->addAction('update', SimpleActionType::class, [
                'route'  => 'app_news_update',
                'label'  => 'content.index.update',
                'params' => ['id' => 'id'],
            ])
        ;
    }
}

Go back to Data lists documentation