diff --git a/eZ/Bundle/EzPublishCoreBundle/Resources/config/helpers.yml b/eZ/Bundle/EzPublishCoreBundle/Resources/config/helpers.yml index 5059671df38..217376df65f 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Resources/config/helpers.yml +++ b/eZ/Bundle/EzPublishCoreBundle/Resources/config/helpers.yml @@ -1,9 +1,14 @@ parameters: # Helpers ezpublish.translation_helper.class: eZ\Publish\Core\Helper\TranslationHelper + ezpublish.field_helper.class: eZ\Publish\Core\Helper\FieldHelper services: # Helpers ezpublish.translation_helper: class: %ezpublish.translation_helper.class% arguments: [@ezpublish.config.resolver] + + ezpublish.field_helper: + class: %ezpublish.field_helper.class% + arguments: [@ezpublish.translation_helper, @ezpublish.api.service.content_type, @ezpublish.api.service.field_type] diff --git a/eZ/Bundle/EzPublishCoreBundle/Resources/config/templating.yml b/eZ/Bundle/EzPublishCoreBundle/Resources/config/templating.yml index 683f5df8520..1d1e75abf7c 100644 --- a/eZ/Bundle/EzPublishCoreBundle/Resources/config/templating.yml +++ b/eZ/Bundle/EzPublishCoreBundle/Resources/config/templating.yml @@ -30,7 +30,7 @@ services: ezpublish.twig.extension.content: class: %ezpublish.twig.extension.content.class% - arguments: [@service_container, @ezpublish.config.resolver, @ezpublish.translation_helper] + arguments: [@service_container, @ezpublish.config.resolver, @ezpublish.translation_helper, @ezpublish.field_helper] tags: - {name: twig.extension} diff --git a/eZ/Publish/Core/Helper/FieldHelper.php b/eZ/Publish/Core/Helper/FieldHelper.php new file mode 100644 index 00000000000..bc44ca1c11d --- /dev/null +++ b/eZ/Publish/Core/Helper/FieldHelper.php @@ -0,0 +1,76 @@ +fieldTypeService = $fieldTypeService; + $this->contentTypeService = $contentTypeService; + $this->translationHelper = $translationHelper; + } + + /** + * 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 FieldDefinition object based on $contentInfo and $fieldDefIdentifier. + * + * @param ContentInfo $contentInfo + * @param string $fieldDefIdentifier + * + * @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition + */ + public function getFieldDefinition( ContentInfo $contentInfo, $fieldDefIdentifier ) + { + return $this + ->contentTypeService + ->loadContentType( $contentInfo->contentTypeId ) + ->getFieldDefinition( $fieldDefIdentifier ); + } +} diff --git a/eZ/Publish/Core/Helper/Tests/FieldHelperTest.php b/eZ/Publish/Core/Helper/Tests/FieldHelperTest.php new file mode 100644 index 00000000000..c51c9d7a1ff --- /dev/null +++ b/eZ/Publish/Core/Helper/Tests/FieldHelperTest.php @@ -0,0 +1,104 @@ +fieldTypeServiceMock = $this->getMock( 'eZ\\Publish\\API\\Repository\\FieldTypeService' ); + $this->contentTypeServiceMock = $this->getMock( 'eZ\\Publish\\API\\Repository\\ContentTypeService' ); + $this->translationHelper = $this + ->getMockBuilder( 'eZ\\Publish\\Core\\Helper\\TranslationHelper' ) + ->disableOriginalConstructor() + ->getMock(); + $this->fieldHelper = new FieldHelper( $this->translationHelper, $this->contentTypeServiceMock, $this->fieldTypeServiceMock ); + } + + public function testIsFieldEmpty() + { + $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; + $emptyValue = $textLineFT->getEmptyValue(); + $emptyField = new Field( array( 'fieldDefIdentifier' => $fieldDefIdentifier, 'value' => $emptyValue ) ); + + $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->assertTrue( $this->fieldHelper->isFieldEmpty( $content, $fieldDefIdentifier ) ); + +// $nonEmptyValue = new Field( array( 'fieldDefIdentifier' => 'ezstring', 'value' => new Value( 'Vive le sucre !!!' ) ) ); +// $this->assertFalse( $this->fieldHelper->isFieldEmpty( $nonEmptyValue ) ); + } +} diff --git a/eZ/Publish/Core/MVC/Symfony/Templating/Tests/Twig/Extension/ContentExtensionTest.php b/eZ/Publish/Core/MVC/Symfony/Templating/Tests/Twig/Extension/ContentExtensionTest.php index d82b27bca54..9ffa170b985 100644 --- a/eZ/Publish/Core/MVC/Symfony/Templating/Tests/Twig/Extension/ContentExtensionTest.php +++ b/eZ/Publish/Core/MVC/Symfony/Templating/Tests/Twig/Extension/ContentExtensionTest.php @@ -33,7 +33,8 @@ public function getExtensions() new ContentExtension( $this->getContainerMock(), $configResolver, - new TranslationHelper( $configResolver ) + new TranslationHelper( $configResolver ), + $this->getMock( 'eZ\\Publish\\Core\\Helper\\FieldHelper' ) ) ); } diff --git a/eZ/Publish/Core/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php b/eZ/Publish/Core/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php index b5ad9127f31..d8f183dc63a 100644 --- a/eZ/Publish/Core/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php +++ b/eZ/Publish/Core/MVC/Symfony/Templating/Twig/Extension/ContentExtension.php @@ -9,6 +9,7 @@ namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension; +use eZ\Publish\Core\Helper\FieldHelper; use eZ\Publish\Core\Helper\TranslationHelper; use eZ\Publish\Core\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\Content\Field; @@ -101,9 +102,14 @@ class ContentExtension extends Twig_Extension /** * @var \eZ\Publish\Core\Helper\TranslationHelper */ - protected $contentHelper; + protected $translationHelper; - public function __construct( ContainerInterface $container, ConfigResolverInterface $resolver, TranslationHelper $contentHelper ) + /** + * @var \eZ\Publish\Core\Helper\FieldHelper + */ + protected $fieldHelper; + + public function __construct( ContainerInterface $container, ConfigResolverInterface $resolver, TranslationHelper $translationHelper, FieldHelper $fieldHelper ) { $comp = function ( $a, $b ) { @@ -119,7 +125,8 @@ public function __construct( ContainerInterface $container, ConfigResolverInterf $this->blocks = array(); $this->container = $container; $this->configResolver = $resolver; - $this->contentHelper = $contentHelper; + $this->translationHelper = $translationHelper; + $this->fieldHelper = $fieldHelper; } /** @@ -162,7 +169,11 @@ public function getFunctions() new Twig_SimpleFunction( 'ez_field_value', array( $this, 'getTranslatedFieldValue' ) - ) + ), + new Twig_SimpleFunction( + 'ez_is_field_empty', + array( $this, 'isFieldEmpty' ) + ), ); } @@ -287,7 +298,7 @@ public function renderFieldDefinitionSettings( FieldDefinition $definition ) */ public function renderField( Content $content, $fieldIdentifier, array $params = array() ) { - $field = $this->contentHelper->getTranslatedField( $content, $fieldIdentifier, isset( $params['lang'] ) ? $params['lang'] : null ); + $field = $this->translationHelper->getTranslatedField( $content, $fieldIdentifier, isset( $params['lang'] ) ? $params['lang'] : null ); if ( !$field instanceof Field ) { @@ -528,7 +539,7 @@ protected function getFieldTypeIdentifier( Content $content, Field $field ) */ public function getTranslatedContentName( Content $content, $forcedLanguage = null ) { - return $this->contentHelper->getTranslatedName( $content, $forcedLanguage ); + return $this->translationHelper->getTranslatedName( $content, $forcedLanguage ); } /** @@ -540,6 +551,20 @@ public function getTranslatedContentName( Content $content, $forcedLanguage = nu */ public function getTranslatedFieldValue( Content $content, $fieldDefIdentifier, $forcedLanguage = null ) { - return $this->contentHelper->getTranslatedField( $content, $fieldDefIdentifier, $forcedLanguage )->value; + return $this->translationHelper->getTranslatedField( $content, $fieldDefIdentifier, $forcedLanguage )->value; + } + + /** + * Checks if a given field is considered empty. + * + * @param \eZ\Publish\Core\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 bool + */ + public function isFieldEmpty( Content $content, $fieldDefIdentifier, $forcedLanguage = null ) + { + return $this->fieldHelper->isFieldEmpty( $content, $fieldDefIdentifier, $forcedLanguage ); } }