Navigation
eZ Documentation Center
 

This is outdated documentation made for eZ Publish Platform 5.2. It is being moved into the eZ Publish 5.x documentation, so please go there for most up-to-date documentation.

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Note
titleVersion compatibility

This recipe is compatible with eZ Publish 5.2 / 2013.11

Table of Contents

Description

When listing content (e.g. blog posts), pagination is a very common use case and is usually painful to implement by hand.

For this purpose eZ Publish 5 recommends the use of Pagerfanta library and proposes adapters for it.

Usage

Code Block
languagephp
<?php
namespace Acme\TestBundle\Controller;
 
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchAdapter;
use Pagerfanta\Pagerfanta;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ContentTypeIdentifier;
use eZ\Publish\API\Repository\Values\Content\Query;
 
class DefaultController extends Controller
{
    public function myContentListAction()
    {
        // First build the search query.
        // Let's search for folders, sorted by publication date.
        $query = new Query();
        $query->criterion = new ContentTypeIdentifier( 'folder' );
        $query->sortClauses = array( new SortClause\DatePublished() );
 
        // Initialize the pager.
        // We pass the ContentSearchAdapter to it.
        // ContentSearchAdapter is built with your search query and the SearchService.
        $pager = new Pagerfanta( 
            new ContentSearchAdapter( $query, $this->getRepository()->getSearchService() ) 
        );
        // Let's list 2 folders per page, even if it doesn't really make sense ;-)
        $pager->setMaxPerPage( 2 );
        // Defaults to page 1 or get "page" query parameter
        $pager->setCurrentPage( $this->getRequest()->get( 'page', 1 ) );
 
        return $this->render(
            'AcmeTestBundle::my_template.html.twig',
            array(
                'totalFolderCount' => $pager->getNbResults(),
                'pagerFolder' => $pager
            )
        );
    }
}
Code Block
languagehtml/xml
titlemy_template.html.twig
{% extends noLayout ? viewbaseLayout : "AcmeTestBundle::pagelayout.html.twig" %}
 
{% block content %}
    <h1>Listing folder content objects: {{ totalFolderCount }} objects found.</h1>
 
    <div>
        <ul>
        {# Loop over the page results #}
        {% for folder in pagerFolder %}
            <li>{{ ez_content_name( folder ) }}</li>
        {% endfor %}
        </ul>
    </div>
 
    {# Only display pagerfanta navigator if needed. #}
    {% if pagerFolder.haveToPaginate() %}
    <div class="pagerfanta">
        {{ pagerfanta( pagerFolder, 'twitter_bootstrap_translated', {'routeName': location} ) }}
    </div>
    {% endif %}
 
{% endblock %}
Tip

For more information and examples, have a look at PagerFanta documentation.

Adapters

Adapter class nameDescription
eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchAdapter
Makes the search against passed Query and returns Content objects.
eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchHitAdapter
Same as ContentSearchAdapter but returns instead SearchHit objects.