Completed
Push — ezp25875-richtext_image_embed_... ( 4bfcce )
by André
20:53
created

getFirstFilledImageFieldIdentifier()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 21
rs 9.0534
1
<?php
2
3
/**
4
 * File containing the ContentExtension class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension;
12
13
use eZ\Publish\API\Repository\Repository;
14
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
15
use eZ\Publish\API\Repository\Values\ValueObject;
16
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
17
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
18
use eZ\Publish\Core\Helper\FieldHelper;
19
use eZ\Publish\Core\Helper\TranslationHelper;
20
use eZ\Publish\API\Repository\Values\Content\Content;
21
use eZ\Publish\API\Repository\Values\Content\Field;
22
use Psr\Log\LoggerInterface;
23
use Twig_Extension;
24
use Twig_SimpleFunction;
25
26
/**
27
 * Twig content extension for eZ Publish specific usage.
28
 * Exposes helpers to play with public API objects.
29
 */
30
class ContentExtension extends Twig_Extension
31
{
32
    /**
33
     * @var \eZ\Publish\API\Repository\Repository
34
     */
35
    protected $repository;
36
37
    /**
38
     * @var \eZ\Publish\Core\Helper\TranslationHelper
39
     */
40
    protected $translationHelper;
41
42
    /**
43
     * @var \eZ\Publish\Core\Helper\FieldHelper
44
     */
45
    protected $fieldHelper;
46
47
    /**
48
     * @var LoggerInterface
49
     */
50
    protected $logger;
51
52
    public function __construct(
53
        Repository $repository,
54
        TranslationHelper $translationHelper,
55
        FieldHelper $fieldHelper,
56
        LoggerInterface $logger = null
57
    ) {
58
        $this->repository = $repository;
59
        $this->translationHelper = $translationHelper;
60
        $this->fieldHelper = $fieldHelper;
61
        $this->logger = $logger;
62
    }
63
64
    /**
65
     * Returns a list of functions to add to the existing list.
66
     *
67
     * @return array
68
     */
69
    public function getFunctions()
70
    {
71
        return array(
72
            new Twig_SimpleFunction(
73
                'ez_content_name',
74
                array($this, 'getTranslatedContentName')
75
            ),
76
            new Twig_SimpleFunction(
77
                'ez_field_value',
78
                array($this, 'getTranslatedFieldValue')
79
            ),
80
            new Twig_SimpleFunction(
81
                'ez_field',
82
                array($this, 'getTranslatedField')
83
            ),
84
            new Twig_SimpleFunction(
85
                'ez_is_field_empty',
86
                array($this, 'isFieldEmpty')
87
            ),
88
            new Twig_SimpleFunction(
89
                'ez_field_name',
90
                array($this, 'getTranslatedFieldDefinitionName')
91
            ),
92
            new Twig_SimpleFunction(
93
                'ez_field_description',
94
                array($this, 'getTranslatedFieldDefinitionDescription')
95
            ),
96
            new Twig_SimpleFunction(
97
                'ez_trans_prop',
98
                array($this, 'getTranslatedProperty')
99
            ),
100
            new Twig_SimpleFunction(
101
                'ez_first_filled_image_field_identifier',
102
                array($this, 'getFirstFilledImageFieldIdentifier')
103
            ),
104
        );
105
    }
106
107
    /**
108
     * Returns the name of the extension.
109
     *
110
     * @return string The extension name
111
     */
112
    public function getName()
113
    {
114
        return 'ezpublish.content';
115
    }
116
117
    /**
118
     * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be a valid Content or ContentInfo object.
119
     * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)
120
     *
121
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content or ContentInfo object.
122
     *
123
     * @return string
124
     */
125
    public function getTranslatedContentName(ValueObject $content, $forcedLanguage = null)
126
    {
127
        if ($content instanceof Content) {
128
            return $this->translationHelper->getTranslatedContentName($content, $forcedLanguage);
129
        } elseif ($content instanceof ContentInfo) {
130
            return $this->translationHelper->getTranslatedContentNameByContentInfo($content, $forcedLanguage);
131
        }
132
133
        throw new InvalidArgumentType('$content', 'eZ\Publish\API\Repository\Values\Content\Content or eZ\Publish\API\Repository\Values\Content\ContentInfo', $content);
134
    }
135
136
    /**
137
     * Returns the translated field, very similar to getTranslatedFieldValue but this returns the whole field.
138
     * To be used with ez_image_alias for example, which requires the whole field.
139
     *
140
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
141
     * @param string $fieldDefIdentifier Identifier for the field we want to get.
142
     * @param string $forcedLanguage Locale we want the field in (e.g. "cro-HR"). Null by default (takes current locale).
143
     *
144
     * @return \eZ\Publish\API\Repository\Values\Content\Field
145
     */
146
    public function getTranslatedField(Content $content, $fieldDefIdentifier, $forcedLanguage = null)
147
    {
148
        return $this->translationHelper->getTranslatedField($content, $fieldDefIdentifier, $forcedLanguage);
149
    }
150
151
    /**
152
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
153
     * @param string $fieldDefIdentifier Identifier for the field we want to get the value from.
154
     * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale).
155
     *
156
     * @return mixed A primitive type or a field type Value object depending on the field type.
157
     */
158
    public function getTranslatedFieldValue(Content $content, $fieldDefIdentifier, $forcedLanguage = null)
159
    {
160
        return $this->translationHelper->getTranslatedField($content, $fieldDefIdentifier, $forcedLanguage)->value;
161
    }
162
163
    /**
164
     * Gets name of a FieldDefinition name by loading ContentType based on Content/ContentInfo object.
165
     *
166
     * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be Content or ContentInfo object
167
     * @param string $fieldDefIdentifier Identifier for the field we want to get the name from
168
     * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)
169
     *
170
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content object.
171
     *
172
     * @return string|null
173
     */
174 View Code Duplication
    public function getTranslatedFieldDefinitionName(ValueObject $content, $fieldDefIdentifier, $forcedLanguage = null)
175
    {
176
        if ($contentType = $this->getContentType($content)) {
177
            return $this->translationHelper->getTranslatedFieldDefinitionProperty(
178
                $contentType,
179
                $fieldDefIdentifier,
180
                'name',
181
                $forcedLanguage
182
            );
183
        }
184
185
        throw new InvalidArgumentType('$content', 'Content|ContentInfo', $content);
186
    }
187
188
    /**
189
     * Gets name of a FieldDefinition description by loading ContentType based on Content/ContentInfo object.
190
     *
191
     * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be Content or ContentInfo object
192
     * @param string $fieldDefIdentifier Identifier for the field we want to get the name from
193
     * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)
194
     *
195
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content object.
196
     *
197
     * @return string|null
198
     */
199 View Code Duplication
    public function getTranslatedFieldDefinitionDescription(ValueObject $content, $fieldDefIdentifier, $forcedLanguage = null)
200
    {
201
        if ($contentType = $this->getContentType($content)) {
202
            return $this->translationHelper->getTranslatedFieldDefinitionProperty(
203
                $contentType,
204
                $fieldDefIdentifier,
205
                'description',
206
                $forcedLanguage
207
            );
208
        }
209
210
        throw new InvalidArgumentType('$content', 'Content|ContentInfo', $content);
211
    }
212
213
    /**
214
     * Gets translated property generic helper.
215
     *
216
     * For generic use, expects property in singular form. For instance if 'name' is provided it will first look for
217
     * getName( $lang ) method, then property called ->names[$lang], in either case look for correct translation.
218
     *
219
     * Languages will consist of either forced language or current SiteAccess languages list, in addition for property
220
     * lookup helper will look for mainLanguage property and use it if either alwaysAvailable property is true or non-
221
     * existing.
222
     *
223
     * @param \eZ\Publish\API\Repository\Values\ValueObject $object Can be any kid of Value object which directly holds the translated data
224
     * @param string $property Property name, example 'name', 'description'
225
     * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)
226
     *
227
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If $property does not exists as plural or as method
228
     *
229
     * @return string|null
230
     */
231
    public function getTranslatedProperty(ValueObject $object, $property, $forcedLanguage = null)
232
    {
233
        $pluralProperty = $property . 's';
234
        if (method_exists($object, 'get' . $property)) {
235
            return $this->translationHelper->getTranslatedByMethod(
236
                $object,
237
                'get' . $property,
238
                $forcedLanguage
239
            );
240
        } elseif (property_exists($object, $pluralProperty) && is_array($object->$pluralProperty)) {
241
            return $this->translationHelper->getTranslatedByProperty(
242
                $object,
243
                $pluralProperty,
244
                $forcedLanguage
245
            );
246
        }
247
248
        throw new InvalidArgumentValue('$property', $property, get_class($object));
249
    }
250
251
    /**
252
     * Checks if a given field is considered empty.
253
     * This method accepts field as Objects or by identifiers.
254
     *
255
     * @param \eZ\Publish\API\Repository\Values\Content\Content $content
256
     * @param \eZ\Publish\API\Repository\Values\Content\Field|string $fieldDefIdentifier Field or Field Identifier to
257
     *                                                                                   get the value from.
258
     * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR").
259
     *                               Null by default (takes current locale).
260
     *
261
     * @return bool
262
     */
263
    public function isFieldEmpty(Content $content, $fieldDefIdentifier, $forcedLanguage = null)
264
    {
265
        if ($fieldDefIdentifier instanceof Field) {
266
            $fieldDefIdentifier = $fieldDefIdentifier->fieldDefIdentifier;
267
        }
268
269
        return $this->fieldHelper->isFieldEmpty($content, $fieldDefIdentifier, $forcedLanguage);
0 ignored issues
show
Bug introduced by
It seems like $forcedLanguage defined by parameter $forcedLanguage on line 263 can also be of type string; however, eZ\Publish\Core\Helper\FieldHelper::isFieldEmpty() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
270
    }
271
272
    /**
273
     * Get ContentType by Content/ContentInfo.
274
     *
275
     * @param \eZ\Publish\API\Repository\Values\Content\Content|\eZ\Publish\API\Repository\Values\Content\ContentInfo $content
276
     *
277
     * @return \eZ\Publish\API\Repository\Values\ContentType\ContentType|null
278
     */
279
    private function getContentType(ValueObject $content)
280
    {
281
        if ($content instanceof Content) {
282
            return $this->repository->getContentTypeService()->loadContentType(
283
                $content->getVersionInfo()->getContentInfo()->contentTypeId
284
            );
285
        } elseif ($content instanceof ContentInfo) {
286
            return $this->repository->getContentTypeService()->loadContentType($content->contentTypeId);
287
        }
288
    }
289
290
    public function getFirstFilledImageFieldIdentifier(Content $content)
291
    {
292
        foreach ($content->getFieldsByLanguage() as $field) {
293
            $fieldTypeIdentifier = $this->fieldHelper->getFieldDefinition(
294
                $content->contentInfo,
295
                $field->fieldDefIdentifier
296
            )->fieldTypeIdentifier;
297
298
            if ($fieldTypeIdentifier !== 'ezimage') {
299
                continue;
300
            }
301
302
            if ($this->fieldHelper->isFieldEmpty($content, $field->fieldDefIdentifier)) {
303
                continue;
304
            }
305
306
            return $field->fieldDefIdentifier;
307
        }
308
309
        return null;
310
    }
311
}
312