chameleon-system/item-list-bundle

This package is abandoned and no longer maintained. The author suggests using the chameleon-system-private/item-list-bundle package instead.

Base bundle to handle lists with filter/paging/sorting

7.1.2 2022-09-26 13:54 UTC

README

Bundle provides a module to list, page, sort and filter any content.

Usage Example

To create your own list, follow these steps:

  1. Optional but recommended: Create a Data Model for your list items

  2. Create a List for your Data Models by implementing \ChameleonSystem\ItemList\Interfaces\ItemListInterface. This class is used to hold the sorted, paged and filtered result of a search.

  3. Create a list provider for your content by implementing \ChameleonSystem\ItemList\Interfaces\ItemListProviderInterface. This class will hold the search/filter/sort and paging logic for your list.

  4. Create a new Module for your list by creating a service for \ChameleonSystem\ItemListBundle\Bridge\Chameleon\Module\ListModule and registering it in the chameleon module list. Make sure to link the table chameleon_item_list_configuration to your Module for basic configuration.

Filter

Filter are created in groups. Usually a group of filters is used for one module. To create a group of filter, you need to define it in your config.yaml under the chameleon_system_item_list attribute. Example:

chameleon_system_item_list:
  filter:
    -
      id: experience
      title: my_title_translation_key
      group: consultant
      view: /filter/consultant-experience.html.twig
    -
      id: bank
      title: bank_title_translation_key
      group: consultant
      view: /filter/consultant-bank.html.twig

The id attribute is used as a parameter for the filter in the get/post request. You may also use the ID to map the filter data to some method/service in your item list provider to interpret the filter data.

The group name is passed to your list module so it can automatically pick the filters relevant for it.

The title can be used in each filter as a title for the filter - and should be passed through trans

The view should be used in your list view to render the data of each filter.

The Module will create a FilterDataModel for each filter in your group and pass these (along with the data provided by the user for each filter) to your item provider.

It is your item providers job then, to

  • use the data passed to filter the search
  • add the filter list back into your \ChameleonSystem\ItemList\Interfaces\ItemListInterface implementation - along with the available data for each filter.

Please note, that the module assumes, that the filter is rendered as part of the module itself.

The Module will overwrite any filters passed via GET with filters you set via request attributes. This makes it possible to create prefiltered landing pages with custom URLs.

Example:

class MyLandingPageController
{
    private ChameleonControllerInterface $mainController;

    public function __construct(ChameleonControllerInterface $mainController)
    {
        $this->mainController = $mainController;
    }

    public function viewBankSolutions(Request $request, string $experience, string $pagedef): Response
    {
        $request->attributes->set(ListModule::PARAM_FILTER, [
            'experience' => $experience
        ]);

        $request->attributes->set('pagedef', $pagedef);

        return $this->mainController->__invoke();
    }
}

Sorting

Sorting is defined and used in a similar way as filter.

chameleon_system_item_list:
  sort:
    -
      id: experience
      title: my_title_translation_key
      group: consultant
    -
      id: bank
      title: bank_title_translation_key
      group: consultant

The list of sorting options are selected via the group and made available in the module view as sortOptions. The active sort id is passed to the view as activeSortId.

The active sort option will be passed to your item provider, where you will need to interpret it.

Paging

Paging can be done via the twig file /list/paging.html.twig. The parameters required when including the paging block are described in the header of the paging twig.

Example:

{% include "/list/paging.html.twig" 
    with {
        'paging':paging, 
        'pageParam': constant('ChameleonSystem\\ItemListBundle\\Bridge\\Chameleon\\Module\\ListModule::PARAM_PAGE_NUMBER'),
        'filterParameterUrlEncoded': filterParameterUrlEncoded
    }
%}

Caching

Please note, that the list currently does not cache. This is intended behaviour, since caching lists is tricky.

If you are able to add caching (even if it is only for the first page of the list), you are encouraged to do so, by extending the \ChameleonSystem\ItemListBundle\Bridge\Chameleon\Module\ListModule class and implementing the appropriate methods.

Views

You will find example views for the list, filter, paging etc in Resources/views