/** * Adds locations from $searchHit to $menu. * * @param ItemInterface $menu * @param SearchHit[] $searchHits */ private function addLocationsToMenu(ItemInterface $menu, array $searchHits) { foreach ($searchHits as $searchHit) { /** @var Location $location */ $location = $searchHit->valueObject; $menuItem = isset($menu[$location->parentLocationId]) ? $menu[$location->parentLocationId] : $menu; $menuItem->addChild($location->id, array('label' => $this->translationHelper->getTranslatedContentNameByContentInfo($location->contentInfo), 'uri' => $this->router->generate($location), 'attributes' => array('id' => 'nav-location-' . $location->id))); $menuItem->setChildrenAttribute('class', 'nav'); } }
/** * Returns tag keyword for provided tag ID or tag object. * * @param mixed|\Netgen\TagsBundle\API\Repository\Values\Tags\Tag $tag * * @return string */ public function getTagKeyword($tag) { if (!$tag instanceof Tag) { try { $tag = $this->tagsService->loadTag($tag); } catch (NotFoundException $e) { return ''; } } return $this->translationHelper->getTranslatedByMethod($tag, 'getKeyword'); }
public function testGeneratesCorrectRouteReference() { $content = $this->getCompleteContent(); $routeReference = new RouteReference(ContentDownloadRouteReferenceListener::ROUTE_NAME, [ContentDownloadRouteReferenceListener::OPT_CONTENT => $content, ContentDownloadRouteReferenceListener::OPT_FIELD_IDENTIFIER => 'file']); $event = new RouteReferenceGenerationEvent($routeReference, new Request()); $eventListener = $this->getListener(); $this->translationHelperMock->expects($this->once())->method('getTranslatedField')->will($this->returnValue($content->getField('file', 'eng-GB'))); $eventListener->onRouteReferenceGeneration($event); self::assertEquals('42', $routeReference->get(ContentDownloadRouteReferenceListener::OPT_CONTENT_ID)); self::assertEquals('file', $routeReference->get(ContentDownloadRouteReferenceListener::OPT_FIELD_IDENTIFIER)); self::assertEquals('Test-file.pdf', $routeReference->get(ContentDownloadRouteReferenceListener::OPT_DOWNLOAD_NAME)); }
/** * If "language" parameter is present, will try to get corresponding SiteAccess for translation. * If found, it will add "siteaccess" parameter to the RouteReference, to trigger SiteAccess switch when generating * the final link. * * @see \eZ\Publish\Core\MVC\Symfony\Routing\Generator::generate() * @see \eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator::doGenerate() * * @param \eZ\Publish\Core\MVC\Symfony\Event\RouteReferenceGenerationEvent $event */ public function onRouteReferenceGeneration(RouteReferenceGenerationEvent $event) { $routeReference = $event->getRouteReference(); if (!$routeReference->has('language')) { return; } $language = $routeReference->get('language'); $routeReference->remove('language'); $siteAccess = $this->translationHelper->getTranslationSiteAccess($language); if ($siteAccess !== null) { $routeReference->set('siteaccess', $siteAccess); } }
/** * @param mixed $contentId ID of a valid Content * @param string $fieldIdentifier Field Definition identifier of the Field the file must be downloaded from * @param string $filename * @param \Symfony\Component\HttpFoundation\Request $request * * @return \eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse */ public function downloadBinaryFileAction($contentId, $fieldIdentifier, $filename, Request $request) { if ($request->query->has('version')) { $content = $this->contentService->loadContent($contentId, null, $request->query->get('version')); } else { $content = $this->contentService->loadContent($contentId); } $field = $this->translationHelper->getTranslatedField($content, $fieldIdentifier, $request->query->has('inLanguage') ? $request->query->get('inLanguage') : null); if (!$field instanceof Field) { throw new InvalidArgumentException("'{$fieldIdentifier}' field not present on content #{$content->contentInfo->id} '{$content->contentInfo->name}'"); } $response = new BinaryStreamResponse($this->ioService->loadBinaryFile($field->value->id), $this->ioService); $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename); return $response; }
/** * @param $resolver */ protected function configureOptions(OptionsResolver $resolver) { $resolver->setRequired([self::OPT_CONTENT, self::OPT_FIELD_IDENTIFIER]); $resolver->setDefaults([self::OPT_VERSION => null, self::OPT_DOWNLOAD_LANGUAGE => null, self::OPT_SITEACCESS_LANGUAGE => null, self::OPT_SITEACCESS => null]); $resolver->setAllowedTypes(self::OPT_CONTENT, 'eZ\\Publish\\API\\Repository\\Values\\Content\\Content'); $resolver->setAllowedTypes(self::OPT_FIELD_IDENTIFIER, 'string'); $resolver->setDefault(self::OPT_CONTENT_ID, function (Options $options) { return $options[self::OPT_CONTENT]->id; }); $resolver->setDefault(self::OPT_DOWNLOAD_NAME, function (Options $options) { $field = $this->translationHelper->getTranslatedField($options[self::OPT_CONTENT], $options[self::OPT_FIELD_IDENTIFIER], $options[self::OPT_DOWNLOAD_LANGUAGE]); if (!$field instanceof Field) { throw new InvalidArgumentException(sprintf("The '%s' parameter did not match a known Field", self::OPT_FIELD_IDENTIFIER)); } return $field->value->fileName; }); }
public function testIsFieldNotEmpty() { $contentTypeId = 123; $contentInfo = new ContentInfo(array('contentTypeId' => $contentTypeId)); $content = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\Content\\Content'); $content->expects($this->any())->method('__get')->with('contentInfo')->will($this->returnValue($contentInfo)); $fieldDefIdentifier = 'my_field_definition'; $textLineFT = new TextLineType(); $nonEmptyValue = new Value('Vive le sucre !!!'); $emptyField = new Field(array('fieldDefIdentifier' => 'ezstring', 'value' => $nonEmptyValue)); $contentType = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\ContentType\\ContentType'); $fieldDefinition = $this->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\ContentType\\FieldDefinition')->setConstructorArgs(array(array('fieldTypeIdentifier' => 'ezstring')))->getMockForAbstractClass(); $contentType->expects($this->once())->method('getFieldDefinition')->with($fieldDefIdentifier)->will($this->returnValue($fieldDefinition)); $this->contentTypeServiceMock->expects($this->once())->method('loadContentType')->with($contentTypeId)->will($this->returnValue($contentType)); $this->translationHelper->expects($this->once())->method('getTranslatedField')->with($content, $fieldDefIdentifier)->will($this->returnValue($emptyField)); $this->fieldTypeServiceMock->expects($this->any())->method('getFieldType')->with('ezstring')->will($this->returnValue($textLineFT)); $this->assertFalse($this->fieldHelper->isFieldEmpty($content, $fieldDefIdentifier)); }
/** * Gets translated property generic helper. * * For generic use, expects property in singular form. For instance if 'name' is provided it will first look for * getName( $lang ) method, then property called ->names[$lang], in either case look for correct translation. * * Languages will consist of either forced language or current SiteAccess languages list, in addition for property * lookup helper will look for mainLanguage property and use it if either alwaysAvailable property is true or non- * existing. * * @param \eZ\Publish\API\Repository\Values\ValueObject $object Can be any kid of Value object which directly holds the translated data * @param string $property Property name, example 'name', 'description' * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale) * * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If $property does not exists as plural or as method * * @return string|null */ public function getTranslatedProperty(ValueObject $object, $property, $forcedLanguage = null) { $pluralProperty = $property . 's'; if (method_exists($object, 'get' . $property)) { return $this->translationHelper->getTranslatedByMethod($object, 'get' . $property, $forcedLanguage); } elseif (property_exists($object, $pluralProperty) && is_array($object->{$pluralProperty})) { return $this->translationHelper->getTranslatedByProperty($object, $pluralProperty, $forcedLanguage); } throw new InvalidArgumentValue('$property', $property, get_class($object)); }
/** * Renders the HTML for a given field. * * @param \eZ\Publish\API\Repository\Values\Content\Content $content * @param string $fieldIdentifier Identifier for the field we want to render * @param array $params An array of parameters to pass to the field view * * @return string The HTML markup * * @throws InvalidArgumentException */ public function renderField(Content $content, $fieldIdentifier, array $params = []) { $field = $this->translationHelper->getTranslatedField($content, $fieldIdentifier, isset($params['lang']) ? $params['lang'] : null); if (!$field instanceof Field) { throw new InvalidArgumentException('$fieldIdentifier', "'{$fieldIdentifier}' field not present on content #{$content->contentInfo->id} '{$content->contentInfo->name}'"); } $params = $this->getRenderFieldBlockParameters($content, $field, $params); $fieldTypeIdentifier = $this->getFieldTypeIdentifier($content, $field); return $this->fieldBlockRenderer->renderContentFieldView($field, $fieldTypeIdentifier, $params); }
/** * Checks if provided field can be considered empty. * * @param \eZ\Publish\API\Repository\Values\Content\Content $content * @param string $fieldDefIdentifier * @param null $forcedLanguage * * @return bool */ public function isFieldEmpty(Content $content, $fieldDefIdentifier, $forcedLanguage = null) { $field = $this->translationHelper->getTranslatedField($content, $fieldDefIdentifier, $forcedLanguage); $fieldDefinition = $this->getFieldDefinition($content->contentInfo, $fieldDefIdentifier); return $this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier)->isEmptyValue($field->value); }
/** * Returns the list of available languages. * * @return array */ public function getAvailableLanguages() { return $this->translationHelper->getAvailableLanguages(); }
public function testGetAvailableLanguagesWithoutTranslationSiteAccesses() { $this->configResolver->expects($this->any())->method('getParameter')->will($this->returnValueMap(array(array('translation_siteaccesses', null, null, array()), array('related_siteaccesses', null, null, array('fre', 'esl', 'heb')), array('languages', null, null, array('eng-GB')), array('languages', null, 'fre', array('fre-FR', 'eng-GB')), array('languages', null, 'esl', array('esl-ES', 'fre-FR', 'eng-GB')), array('languages', null, 'heb', array('heb-IL', 'eng-GB'))))); $expectedLanguages = array('eng-GB', 'esl-ES', 'fre-FR', 'heb-IL'); $this->assertSame($expectedLanguages, $this->translationHelper->getAvailableLanguages()); }
/** * Get the content name * * @param $contentId * @return string */ public function getContentName($contentId) { $contentInfo = $this->contentService->loadContentInfo($contentId); return $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo); }
/** * @param \eZ\Publish\API\Repository\Values\Content\Content $content * @param string $fieldDefIdentifier Identifier for the field we want to get the value from. * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale). * * @return mixed A primitive type or a field type Value object depending on the field type. */ public function getTranslatedFieldValue(Content $content, $fieldDefIdentifier, $forcedLanguage = null) { return $this->translationHelper->getTranslatedField($content, $fieldDefIdentifier, $forcedLanguage)->value; }