Some common examples showcasing Site API usage.
/** @var $repository \eZ\Publish\API\Repository\Repository */
$contentService = $repository->getContentService();
/** @var $configResolver \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
$languages = $configResolver->getParameter('languages');
$content = $contentService->loadContent(42, $languages);
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$loadService = $site->getLoadService();
$content = $loadService->loadContent(42);
- Repository API requires a list of languages
- Repository API will return Content in all given languages from the given list (if they exist), but only one will be rendered
- Site API returns the Content only in the language that will be rendered on the siteaccess
/** @var $repository \eZ\Publish\API\Repository\Repository */
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
/** @var $configResolver \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
$languages = $configResolver->getParameter('languages');
$location = $locationService->loadLocation(42);
// Can we show this Location on the siteaccess?
$versionInfo = $contentService->loadVersionInfoById($location->contentId);
$languageCodesSet = array_flip($versionInfo->languageCodes);
$canShowLocation = false;
foreach ($languages as $languageCode) {
if (isset($languageCodesSet[$languageCode])) {
$canShowLocation = true;
break;
}
}
if (!$canShowLocation) {
throw new NotFoundException('404 not found');
}
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$loadService = $site->getLoadService();
$location = $loadService->loadLocation(42);
- Repository API does't take languages into account, it takes a lot of work to take care of it manually
- Site API takes care of that automatically, it won't find the Location if the language should not be rendered on the siteaccess
/** @var $repository \eZ\Publish\API\Repository\Repository */
$locationService = $repository->getLocationService();
$contentService = $repository->getContentService();
/** @var $configResolver \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
$languages = $configResolver->getParameter('languages');
$location = $locationService->loadLocation(42);
$content = $contentService->loadContent($location->contentId, $languages);
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$loadService = $site->getLoadService();
$location = $loadService->loadLocation(42);
$content = $location->content;
- no additional call to get the Content
- Content is lazy loaded only when accessed!
For example you might want to do this for performance, loading ContentInfo is faster because doesn't contain fields.
/** @var $repository \eZ\Publish\API\Repository\Repository */
$contentService = $repository->getContentService();
/** @var $configResolver \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
$languages = $configResolver->getParameter('languages');
// We can't load only ContentInfo, because if does not contain language information
$versionInfo = $contentService->loadVersionInfoById(42);
$languageCodesSet = array_flip($versionInfo->languageCodes);
$canShowContent = false;
foreach ($languages as $languageCode) {
if (isset($languageCodesSet[$languageCode])) {
$canShowContent = true;
break;
}
}
if ($canShowContent && $versionInfo->contentInfo->contentTypeId === 239458721341) {
$content = $contentService->loadContent(42, $languages);
$field = $content->getField('name');
// ...
}
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$loadService = $site->getLoadService();
$content = $loadService->loadContent(42);
if ($content->contentInfo->contentTypeIdentifier === 'article') {
$field = $content->getField('name');
// ...
}
- Content fields are lazy loaded only when accessed!
- With Repository API doing something as simple as this can be very involving
- With Site API it just comes natural
/** @var $repository \eZ\Publish\API\Repository\Repository */
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
/** @var $configResolver \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
$languages = $configResolver->getParameter('languages');
$content = $contentService->loadContent(42, $languages);
$mainLocation = $locationService->loadLocation($content->contentInfo->mainLocationId);
$parentLocation = $locationService->loadLocation($mainLocation->parentLocationId);
$parentContent = $contentService->loadContent($parentLocation->contentId, $languages);
$name = $parentContent->getVersionInfo()->getName();
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$loadService = $site->getLoadService();
$content = $loadService->loadContent(42);
$name = $content->mainLocation->parent->content->name;
- properties are lazy loaded only when accessed!
- with Site API simple content traversal is simple
/** @var $repository \eZ\Publish\API\Repository\Repository */
$contentService = $repository->getContentService();
/** @var $configResolver \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
$languages = $configResolver->getParameter('languages');
$content = $contentService->loadContent(42, $languages);
/** @var \eZ\Publish\Core\Helper\FieldHelper */
if ($fieldHelper->isFieldEmpty($content, 'image')) {
// ...
}
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$loadService = $site->getLoadService();
$content = $loadService->loadContent(42);
if (!$content->getField('image')->isEmpty()) {
// ...
}
- no special helper needed to check if the field is empty
/** @var $repository \eZ\Publish\API\Repository\Repository */
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$searchService = $repository->getSearchService();
/** @var $configResolver \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
$languages = $configResolver->getParameter('languages');
$content = $contentService->loadContent(42, $languages);
$mainLocation = $locationService->loadLocation($content->contentInfo->mainLocationId);
$query = new Query([
'filter' => new LogcialAnd([
new ParentLocationId($mainLocation->id),
new ContentTypeIdentifier('image'),
]),
]);
$searchResult = $searchService->findContent($query, ['languages' => $languages]);
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$loadService = $site->getLoadService();
$content = $loadService->loadContent(42);
$images = $content->mainLocation->filterChildren(['images']);
- again - properties are lazy loaded only when accessed
$images
is an instance ofPagerfanta
- you can iterate over it directly or use it to build a pager
/** @var $repository \eZ\Publish\API\Repository\Repository */
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$searchService = $repository->getSearchService();
/** @var $configResolver \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
$languages = $configResolver->getParameter('languages');
$location = $locationService->loadLocation(42);
$query = new LocationQuery([
'filter' => new LogcialAnd([
new ParentLocationId($location->parentLocationId),
new ContentTypeId($location->contentInfo->contentTypeId),
new LogicalNot(
new LocationId($location->id)
),
]),
]);
$searchResult = $searchService->findLocations($query, ['languages' => $languages]);
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$loadService = $site->getLoadService();
$location = $loadService->loadContent(42);
$siblings = $location->filterSiblings([$location->contentInfo->contentTypeIdentifier]);
- kind of a syntax sugar
- this would be similar to
$location->parent->filterChildren([$location->contentInfo->contentTypeIdentifier])
, but it excludes current Location automatically
/** @var $repository \eZ\Publish\API\Repository\Repository */
$contentService = $repository->getContentService();
$searchService = $repository->getSearchService();
/** @var $configResolver \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
$languages = $configResolver->getParameter('languages');
$content = $locationService->loadContent(42, $languages);
$relationField = $content->getField('related_content');
$relatedContentIds = $relationField->value->destinationContentIds;
// We have to use search to account for permissions and status
$query = new Query([
'filter' => new LogcialAnd([
new ContentId($relatedContentIds),
new ContentTypeId(1234234512),
]),
]);
$searchResult = $searchService->findLocations($query, ['languages' => $languages]);
// Now we have the result, but the relation order is not ensured by search
$relatedContentItems = [];
foreach ($searchResult->searchHit as $searchHit) {
$relatedContentItems[] = $searchHit->valueObject;
}
$sortedIdList = array_flip($relatedContentIds);
$sorter = function (Content $content1, Content $content2) use ($sortedIdList) {
if ($content1->id === $content2->id) {
return 0;
}
return ($sortedIdList[$content1->id] < $sortedIdList[$content2->id]) ? -1 : 1;
};
usort($relatedContentItems, $sorter);
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$loadService = $site->getLoadService();
$content = $loadService->loadContent(42);
$relatedContentItems = $content->filterFieldRelations('related_content', ['image']);
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$findService = $site->getFindService();
// This is unchanged Repository Search API
$query = new Query();
// Search result contains Site API Content items
$searchResult = $findService->findContent($query);
// This is unchanged Repository Search API
$locationQuery = new LocationQuery();
// Search result contains Site API Locations
$searchResult = $findService->findLocations($locationQuery);
Sometimes you don't want to depend on the asynchronous nature of indexing data in Solr. FilterService provides Search API on top of Legacy Search Engine, in parallel to using Solr as the Repository's search engine. There is no similar functionality in Repository API. You could access Solr search handler to force commit, and then wait for a certain amount of time to ensure data is available for search, but that's poor solution.
For various reasons Repository API will need to implement something like this in the future, it was already proposed in:
ezsystems/ezpublish-kernel#1636
/** @var $site \Netgen\EzPlatformSiteApi\API\Site */
$filterService = $site->getFilterService();
$query = new Query();
$searchResult = $findService->filterContent($query);
- it mostly behaves the same as FindService, only uses different search engine (Legacy)
- fulltext search (Fulltext criterion) should not be used with FilterService, as the fulltext data will be indexed only for the configured search engine (assuming Solr)
Access Content fields directly through dot or array notation:
{ ez_render_field( content, 'title' ) }
{ ng_render_field( content.fields.title ) }
{ ng_render_field( content.fields['title'] ) }
Check if the field is empty without helper function:
{% if ez_is_field_empty( content, 'image' ) %}
...
{% endif %}
{% if content.fields.image.empty %}
...
{% endif %}
Simplified image alias rendering:
{ ez_image_alias( content.field( 'image' ), content.versionInfo, 'large' ) }
{ ng_image_alias( content.fields.image, 'large' ) }
{% set children = location.filterChildren(['blog_post'], 10, 2) %}
<p>Parent name: {{ location.parent.content.name }}<p>
<!-- 'children' variable is full Pagerfanta instance -->
<p>Total blog posts: {{ children.nbResults }}</p>
<p>Blog posts per page: {{ children.maxPerPage }}</p>
<p>Page: {{ children.currentPage }}</p>
<ul>
{% for child in children %}
<li>{{ child.content.name }}</li>
{% endfor %}
</ul>
{{ pagerfanta( children, 'twitter_bootstrap' ) }}