Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made template for showing image_field in admin-ui more robust #1387

Open
wants to merge 1 commit into
base: 4.6
Choose a base branch
from

Conversation

vidarl
Copy link
Contributor

@vidarl vidarl commented Oct 31, 2024

Description:

I was not able to reproduce the problem on a fresh 4.6, but on a installation just upgraded to 4.6 (was at some point using 2.5 or maybe even older releases) the database had a lot of images with the following "empty" data in ezcontentobject_attribute.data_text:

<?xml version="1.0" encoding="UTF-8"?>
<ezimage serial_number=""
         is_valid=""
         filename=""
         suffix=""
         basename=""
         dirpath=""
         url=""
         original_filename=""
         mime_type=""
         width=""
         height=""
         alternative_text=""
         alias_key="1293033771"
         timestamp="1086942459" />

In 4.6, the value will simply be null if no image is uploaded. However, in earlier versions it was obviously stored like shown above. And one problem here is that height has a value set to empty string. The admin-ui template requires height to be numeric or a Unsupported operand types: string / string exception will be thrown.

It would be nice if twig had a is numeric test but that is not going to happend. For this use-case it is sufficient to check if value is empty string too

For QA:

Documentation:

Copy link

sonarcloud bot commented Oct 31, 2024

@@ -447,7 +447,7 @@
<td>{{ 'ezimage.master_dimensions'|trans|desc('Master dimensions') }}:</td>
<td>{{ 'ezimage.width_and_height'|trans({'%width%': field.value.width, '%height%': field.value.height})|desc('Width: %width%px height: %height%px') }}</td>
</tr>
{% if field.value.height != '0' %}
{% if field.value.height != '0' and field.value.height != '' %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should achieve the same result:

Suggested change
{% if field.value.height != '0' and field.value.height != '' %}
{% if field.value.height %}

However, the important bit is that what is in the block below relies on field.value.height being "not zero", as we're performing a division. Non-empty string that does not contain a numerical value will cause PHP to error out.

As Fabien pointed out in the comments to the PR you linked, adding is_numeric as a function/test to Twig is trivial and we can do that relatively easily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need to keep the check for value 0 in order to avoid division by zero.
But we could change it to this indeed:

Suggested change
{% if field.value.height != '0' and field.value.height != '' %}
{% if field.value.height != '0' and field.value.height %}

Or do you prefer I make a is numeric test to Twig so it reads something like :

Suggested change
{% if field.value.height != '0' and field.value.height != '' %}
{% if field.value.height is numeric and field.value.height != '0' %}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Steveb-p You want me to make a is numeric test, or this is not needed ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd stay away from adding a numeric test, since we would be introducing it to every installation. And we can't guarantee someone else did not have it implemented already.

I'd fine with the first set you shown, although I'm pretty sure "0" is a falsy value and that means

{% if field.value.height %}

is logically equal to

{% if field.value.height != '0' and field.value.height %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants