PHP code example of heimrichhannot / contao-list-widget-bundle

1. Go to this page and download the library: Download heimrichhannot/contao-list-widget-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

heimrichhannot / contao-list-widget-bundle example snippets


# /contao/dca/tl_custom.php
$GLOBALS['TL_DCA']['tl_custom']['fields']['people'] = [
  'inputType' => 'listWidget',
  'eval' => [
      'tl_class' => 'long clr',
      'listWidget' => [
          'ajax' => true,
          'ajaxConfig' => [
              'route' => 'app_list_widget_people_list',
          ],
          'table' => 'tl_custom',
      ],
  ],
];

# /src/Controller/PeopleController.php

use Contao\CoreBundle\Framework\ContaoFramework;
use Doctrine\Common\Collections\Criteria;
use HeimrichHannot\ListWidgetBundle\Controller\ListWidgetContext;
use HeimrichHannot\ListWidgetBundle\Controller\ListWidgetResponse;
use App\People\PeopleFinder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/app/list_widget/people_list', name: 'app_list_widget_people_list', defaults: ['_scope' => 'backend'])]
class PeopleController
{
    public function __construct(
        private readonly ContaoFramework $framework,
        private readonly PeopleFinder $people,
    ) {}

    public function __invoke(Request $request): ListWidgetResponse
    {
        $this->framework->initialize();
        try {
            $context = ListWidgetContext::createFromRequest($request);
        } catch (\Exception $e) {
            return new ListWidgetResponse(1, 0, 0, [], $e->getMessage());
        }

        $countTotal = $this->people->countByList($context->id);
        $fields = ['id', 'email', 'firstname', 'lastname'];

        $criteria = new Criteria();
        $context->applySearchToCriteria($criteria,$fields);
        $countFiltered = $this->people->countByList($context->id, $criteria);
        $context->applyListConfigToCriteria($criteria, $fields);
        $people = $this->people->findByList($context->id, $fields, $criteria);
        return $context->createResponse($countTotal, $countFiltered, $people);
    }
}

'someField' => [
    'label'     => &$GLOBALS['TL_LANG']['tl_my_dca']['someField'],
    'exclude'   => true,
    'inputType' => 'listWidget',
    'eval'      => [
        'listWidget' => [
            'ajax'                   => true,
            'ajaxConfig'             => [
                'load_items_callback' => ['SomeClass', 'loadItems']
            ],
            'header_fields_callback' => function ()
            {
                $arrHeaderFields = [];

                foreach (['academicTitle', 'additionalTitle', 'gender', 'lastname', 'email'] as $strField)
                {
                    $arrHeaderFields[$strField] = \HeimrichHannot\Haste\Dca\General::getLocalizedFieldname($strField, 'tl_dca');
                }

                return $arrHeaderFields;
            },
            'table'                  => 'tl_dca'
        ]
    ]
]

static::$arrListConfig = [
    'identifier' => 'module_' . $this->id,
    'table' => 'tl_dca',
    'ajax' => true,
    'ajaxConfig' => [
        'load_items_callback' => function($arrConfig, $arrOptions = [], $objContext = null, $objDc = null) {
            return $this->loadItems($arrConfig, $arrOptions, $objContext, $objDc);
        },
        'prepare_items_callback' => function($objItems) {
            return $this->parseNewsletters($objItems);
        },
    ],
    'columns' => static::getColumns(),
    'language' => static::getLanguage()
];

static::$arrListConfig = ListWidget::prepareConfig(static::$arrListConfig, $this);

ListWidget::initAjaxLoading(static::$arrListConfig);

ListWidget::addToTemplate($this->Template, static::$arrListOptions);

public static function loadItemsNew($arrConfig, $arrOptions = [], $objContext = null, $objDc = null)
{
    // set an initial filter using the contao options array
    $arrOptions = [
        'table'   => $arrConfig['table'],
        'columns' => $arrConfig['columns'],
        // filtering
        'column'  => 'pid',
        'value'   => $objDc->id
    ];

    // the rest of the function should also be called
    return ListWidget::loadItems($arrConfig, $arrOptions, $objContext, $objDc);
}