Message-ID: <1580605120.3222.1485852432013.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3221_1998567152.1485852432013" ------=_Part_3221_1998567152.1485852432013 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html How to paginate API search results

How to paginate API search results

=20 =20

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 Platform recommends the use of = Pagerfanta library and proposes adapters for it.

Usage

=20
<?php
namespace Acme\TestBundle\Controller;
=20
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchAdapter;
use Pagerfanta\Pagerfanta;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ContentTypeIde=
ntifier;
use eZ\Publish\API\Repository\Values\Content\Query;
=20
class DefaultController extends Controller
{
    public function myContentListAction( $locationId, $viewType, $layout =
=3D false, array $params =3D array() )
    {
        // First build the search query.
        // Let's search for folders, sorted by publication date.
        $query =3D new Query();
        $query->criterion =3D new ContentTypeIdentifier( 'folder' );
        $query->sortClauses =3D array( new SortClause\DatePublished() );
 
        // Initialize the pager.
        // We pass the ContentSearchAdapter to it.
        // ContentSearchAdapter is built with your search query and the Sea=
rchService.
        $pager =3D new Pagerfanta(=20
            new ContentSearchAdapter( $query, $this->getRepository()->=
;getSearchService() )=20
        );
        // Let's list 2 folders per page, even if it doesn't really make se=
nse ;-)
        $pager->setMaxPerPage( 2 );
        // Defaults to page 1 or get "page" query parameter
        $pager->setCurrentPage( $this->getRequest()->get( 'page', =
1 ) );
=20
        return $this->render(
            'AcmeTestBundle::my_template.html.twig',
            array(
                'totalFolderCount' =3D> $pager->getNbResults(),
                'pagerFolder' =3D> $pager,
                'location' =3D> $this->getRepository()->getLocatio=
nService()->loadLocation( $locationId ),
            ) + $params
        );
    }
}
=20
my_template.html.twig
=20
{% block content %}
    <h1>Listing folder content objects: {{ totalFolderCount }} object=
s found.</h1>
=20
    <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=3D"pagerfanta">
        {{ pagerfanta( pagerFolder, 'twitter_bootstrap_translated', {'route=
Name': location} ) }}
    </div>
    {% endif %}
=20
{% endblock %}
=20

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

Adapters

Adapter class name Description
eZ\Publish\Core\Pagination\Pagerfanta\Conte=
ntSearchAdapter
Makes the search against passed Query and return= s Content objects.
eZ\Publish\Core\Pagination\Pa=
gerfanta\ContentSearchHitAdapter
Same as ContentSearchAdapter but r= eturns instead SearchHit objects.
eZ\Publish\Core\Pagination\Pa=
gerfanta\LocationSearchAdapter
Makes a Location search against pa= ssed Query and returns Location objects.
eZ\Publish\Core\Pagination\Pa=
gerfanta\LocationSearchHitAdapter
Same as LocationSearchAdapter but = returns instead SearchHit objects.

 

 

 

------=_Part_3221_1998567152.1485852432013--