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

Collection custom filter messing with localized content - in() function side effect? #1539

Closed
joaojoyce opened this Issue Aug 4, 2017 · 7 comments

Comments

Projects
None yet
2 participants
@joaojoyce

joaojoyce commented Aug 4, 2017

I'm using a custom filter to prevent some content from being shown dependent on some custom logic.

The filter seems to work fine but it messes with the localization of content.

For instance this basic filter works and shows my content according to the current locale.

    public function filter()
    {
        return $this->collection->filter(function ($entry) {
              $temporal = $entry->get('temporal');
              return true;
       });
    }

But with this one:

    public function filter()
    {
        return $this->collection->filter(function ($entry) {
              $temporal = $entry->in('pt')->get('temporal');
              return true;
       });
    }

the filter still works but the content shows in Portuguese, even though I have only read a variable. It seems that the in() function has some kind of side effect.

This doesn't seem to be the expected behaviour. Is there any other function like in() but without the side effect?

@joaojoyce

This comment has been minimized.

joaojoyce commented Aug 4, 2017

By the way, I can work around this by copying the object.

    public function filter()
    {
        return $this->collection->filter(function ($entry) {

              $content = clone $entry;
              $temporal = $content->in('pt')->get('temporal');

              (......)

       });
    }

but it still seems that the in() function shouldn't have this kind of side effect.

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Aug 10, 2017

I'm guessing there's more to your code than the snippet you posted.

        return $this->collection->filter(function ($entry) {
              $temporal = $entry->in('pt')->get('temporal');
              return true;
       });

You're just setting the $temporal variable and not doing anything with it. Not sure why this would affect your collection at all.

Can you provide your actual code? If you're using map and returning the localized entry, then I'd expect it to happen in that case.

@joaojoyce

This comment has been minimized.

joaojoyce commented Aug 14, 2017

Hi. Thank you for your help.

Of course that's not all the code.
But just that simple code that only sets a variable and does nothing with it breaks the application by setting the wrong locale for all the app.

That's why I'm assuming that the in() function has the unintended side effect of changing the locale for all the app.

Here's the code:

public function filter()
    {
        return $this->collection->filter(function ($entry) {

            $temporal = $entry->in('pt')->get('temporal');
            $initial_date = $entry->in('pt')->get('initial_date');
            $final_date = $entry->in('pt')->get('final_date');

            if($temporal=='false') {
                return true;
            }

            $date = new DateTime($initial_date, new DateTimeZone('Europe/Lisbon'));
            $initial_timestamp = $date->format('U');

            $date = new DateTime($final_date, new DateTimeZone('Europe/Lisbon'));
            $final_timestamp = $date->format('U');


            if($initial_timestamp<strtotime('now') && $final_timestamp>strtotime('now')) {
                return true;
            }
            return false;
        });

    }

This is the version I currently have in production to fix that issue. It works perfectly fine by just copying the entry variable avoiding running the in() function on the original $entry variable.

    public function filter()
    {
        return $this->collection->filter(function ($entry) {

            $content = clone $entry;

            $temporal = $content->in('pt')->get('temporal');
            $initial_date = $content->in('pt')->get('initial_date');
            $final_date = $content->in('pt')->get('final_date');

            if($temporal=='false') {
                return true;
            }

            $date = new DateTime($initial_date, new DateTimeZone('Europe/Lisbon'));
            $initial_timestamp = $date->format('U');

            $date = new DateTime($final_date, new DateTimeZone('Europe/Lisbon'));
            $final_timestamp = $date->format('U');


            if($initial_timestamp<strtotime('now') && $final_timestamp>strtotime('now')) {
                return true;
            }
            return false;
        });

    }
@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Aug 14, 2017

Excuse the silly question, but if you avoid using this filter, does the tag continue to work normally?

@joaojoyce

This comment has been minimized.

joaojoyce commented Aug 14, 2017

Hi!

Questions are OK :)
Avoiding the filter completely the app works great. The content is shown in the correct language.

As soon as I run:

            $entry->in('pt')->get('temporal');

the content changes language.

Actually I believe that it is the combination between in() and get() that messes things up.

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Aug 15, 2017

I was able to reproduce this and will get a fix in soon. Thanks for your patience here!

@jasonvarga jasonvarga added the bug label Aug 15, 2017

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Aug 18, 2017

I believe this fixes it. Can you update the file and confirm for me?

image

@jasonvarga jasonvarga closed this Dec 14, 2017

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