By default, this method will return the field in current language if translation is present. If not, main language will be used.
If $forcedLanguage is provided, will return the field in this language, if translation is present.
public getTranslatedField ( eZ\Publish\API\Repository\Values\Content\Content $content, string $fieldDefIdentifier, string $forcedLanguage = null ) : eZ\Publish\API\Repository\Values\Content\Field | null | ||
$content | eZ\Publish\API\Repository\Values\Content\Content | |
$fieldDefIdentifier | string | Field definition identifier. |
$forcedLanguage | string | Locale we want the field translation in (e.g. "fre-FR"). Null by default (takes current locale) |
Résultat | eZ\Publish\API\Repository\Values\Content\Field | null |
/** * @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; }); }
/** * 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); }
/** * @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; }
/** * @dataProvider getTranslatedFieldProvider * * @param array $prioritizedLanguages * @param string $expectedLocale */ public function getTranslatedField(array $prioritizedLanguages, $expectedLocale) { $content = $this->generateContent(); $this->configResolver->expects($this->once())->method('getParameter')->with('languages')->will($this->returnValue($prioritizedLanguages)); $this->assertSame($this->translatedFields[$expectedLocale], $this->translationHelper->getTranslatedField($content, 'test')); }