diff --git a/README.md b/README.md index e02328f..13834ad 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,25 @@ The URI your site's REST API can be accessed from. ### `recommender.included_content_types` This allows you to define content types on which tracking script will be shown. Go to the Tracking section to get more details. +If content's author or image are stored in different field, you can specify it in __default_settings.yml__ + +```yaml + ez_recommendation.field_identifiers: + {field fetched by controller (image or author)} + {content type}: {field with value} +``` + +For example: + +```yaml + ez_recommendation.field_identifiers: + author: + article: authors + image: + article: thumbnail + blog_post: main_image +``` + ### [advanced] ```yaml ez_recommendation: diff --git a/Rest/Controller/ContentController.php b/Rest/Controller/ContentController.php index bcce94c..34d05fe 100644 --- a/Rest/Controller/ContentController.php +++ b/Rest/Controller/ContentController.php @@ -9,6 +9,7 @@ namespace EzSystems\RecommendationBundle\Rest\Controller; use eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator as ImageVariationService; +use eZ\Publish\Core\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\Content\Field; use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\Core\MVC\ConfigResolverInterface; @@ -117,7 +118,7 @@ protected function prepareContent($contentIds) 'identifier' => $contentType->identifier, 'language' => $language, 'publishedDate' => $contentValue->contentInfo->publishedDate->format('c'), - 'author' => $contentValue->getFieldValue('author'), + 'author' => $this->getAuthor($contentValue, $contentType), 'uri' => $this->generator->generate($location, array(), false), 'mainLocation' => array( 'href' => '/api/ezp/v2/content/locations' . $location->pathString, @@ -245,6 +246,10 @@ private function getImageFieldIdentifier($contentId, $language) $content = $this->contentService->loadContent($contentId, array($language)); $contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId); + if ($identifier = $this->getFieldIdentifier('image', $contentType)) { + return $identifier; + } + foreach ($contentType->fieldDefinitions as $fieldDefinition) { if ($fieldDefinition->fieldTypeIdentifier == 'ezimage') { return $fieldDefinition->identifier; @@ -263,6 +268,8 @@ private function getImageFieldIdentifier($contentId, $language) * * @param \eZ\Publish\Core\Repository\Values\Content\Content $content * @param string $language + * + * @return int|null */ private function getRelation($content, $language) { @@ -278,4 +285,56 @@ private function getRelation($content, $language) return false; } + + /** + * Returns author of the content. + * + * @param Content $contentValue + * @param ContentType $contentType + * + * @return string + */ + private function getAuthor(Content $contentValue, ContentType $contentType) + { + $author = $contentValue->getFieldValue($this->getFieldIdentifier('author', $contentType)); + + if (null === $author) { + $user = $this->contentService->loadContentInfo($contentValue->contentInfo->ownerId); + $author = $user->name; + } + + return (string) $author; + } + + /** + * Returns field name. + * + * To define another field name for specific value (e. g. author) add it to default_settings.yml + * + * For example: + * + * ez_recommendation.field_identifiers: + * author: + * blog_post: authors + * image: + * blog_post: thumbnail + * + * @param string $fieldName + * @param ContentType $contentType + * + * @return string + */ + private function getFieldIdentifier($fieldName, ContentType $contentType) + { + $contentTypeName = $contentType->identifier; + if ($this->container->hasParameter('ez_recommendation.field_identifiers')) { + $fieldDefinitions = $this->container->getParameter('ez_recommendation.field_identifiers'); + + if (isset($fieldDefinitions[$fieldName]) && !empty($fieldDefinitions[$fieldName][$contentTypeName])) { + return $fieldDefinitions[$fieldName][$contentTypeName]; + } + } else { + return $fieldName; + } + } }