Skip to content

Commit

Permalink
Merge pull request #26 from kamilmusial/author_value
Browse files Browse the repository at this point in the history
proper value for author field
  • Loading branch information
lserwatka committed Aug 24, 2015
2 parents 5774d5d + bb4cfb5 commit 7472459
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
61 changes: 60 additions & 1 deletion Rest/Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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;
}
}
}

0 comments on commit 7472459

Please sign in to comment.