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

Fix [warning] Invalid argument supplied for foreach() EntityViewsDataTaxonomyFilterTrait.php:26 #575

Merged

Conversation

mstenta
Copy link
Member

@mstenta mstenta commented Sep 26, 2022

Fixes #574

mstenta added a commit to mstenta/farmOS that referenced this pull request Sep 26, 2022
@mstenta mstenta force-pushed the 2.x-fix-entityviewsdatataxonomyfiltertrait branch from 02ce5d5 to a85b677 Compare September 26, 2022 21:24
@mstenta mstenta marked this pull request as ready for review September 26, 2022 21:24
$field_name = $field->getName();
foreach ($data as $table_name => $table_data) {
foreach ($table_data as $table_field_name => $field_data) {
if (isset($field_data['filter']) && strpos($table_field_name, $field_name) === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

strpos($table_field_name, $field_name) === 0

I'm curious why we can't check that $table_field_name == $field_name ? I think it could be possible that two different fields have the same prefix and this logic could inadvertently change the filter ID for the wrong field.

Consider two field names: Taxonomy reference custom_field and String custom_field_raw. I think this logic would change custom_field_raw to use the taxonomy_index_tid filter.

Copy link
Member

Choose a reason for hiding this comment

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

but I think this approach should work! 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Consider two field names: Taxonomy reference custom_field and String custom_field_raw. I think this logic would change custom_field_raw to use the taxonomy_index_tid filter.

That's a good point.

I'm not sure why we used strpos($table_field_name, $field_name) === 0 originally. Here is the commit that added it, if it rings any bells: 7358e4d

This PR doesn't change that bit, just restructures the foreach so that we don't need to figure out $table_name.

I wonder if there was a good reason for using strpos... 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

This PR doesn't change that bit, just restructures the foreach so that we don't need to figure out $table_name.

Ah but I see how that would introduce this issue, as we are no longer limiting which tables this logic can potentially run on...

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah ok... just ran this through the debugger and quickly realized why we used strpos()...

Example:

  • $table_field_name is animal_type_target_id
  • $field_name is animal_type

Copy link
Member Author

Choose a reason for hiding this comment

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

But not for all of them...

Example:

  • $table_field_name is units
  • $field_name is units

Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps we could do:

if (isset($field_data['filter']) && in_array($table_field_name, [$field_name, $field_name . '_target_id'])) {

... basically checking if it's either $field_name or $field_name . '_target_id'.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it could be possible that two different fields have the same prefix and this logic could inadvertently change the filter ID for the wrong field.

Ah ha! Sure enough I found a case where this happens in the custom codebase I'm working in, which has both field_input and field_input_date fields.

@mstenta mstenta force-pushed the 2.x-fix-entityviewsdatataxonomyfiltertrait branch from a85b677 to bef0400 Compare September 27, 2022 17:11
@mstenta
Copy link
Member Author

mstenta commented Sep 27, 2022

OK. Force-pushed the commit to implement this change. I also restructured the code to make the logic more readable with extra comments etc.

Copy link
Member

@paul121 paul121 left a comment

Choose a reason for hiding this comment

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

Thanks for adding the comments :-)

@mstenta mstenta merged commit bef0400 into farmOS:2.x Sep 27, 2022
@mstenta mstenta deleted the 2.x-fix-entityviewsdatataxonomyfiltertrait branch September 27, 2022 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[warning] Invalid argument supplied for foreach() EntityViewsDataTaxonomyFilterTrait.php:26
2 participants