Jump to content

Recommended Posts

Posted

Hello,

what I'd like to do with a Hook is this.
the hook should work if I change a certain field on a page with a certain template.
Afterwards a value should be written in the template "user".

Somehow I am stuck at the moment. If I enter the value $page->entryPages fix in the if statement ( eg. 1234 ), it works.

Where do I have my error:

$wire->addHookAfter('Page::changed', function(HookEvent $event) {
  $page = $event->object;
  $change = $event->arguments(0);
        
  if($page->template == 'entry' && $change == 'entryPages') {
    // execute some code when "entryPages" changes on "entry" pages

    // show info about the value "entryPages
    wire('session')->message("Hello World 2 ! You saved {$page->entryPages}");

        // get all users
        $allUser = wire('pages')->find("template=user");

        foreach($allUser as $user){

      // where the user->id == $page->entryPages, write the value in user-template

          if( $user->id == $page->entryPages ) {
			// check out if success
             wire('session')->message("This is it {$user->id}");
             $user->setAndSave('userEntryUrl', $page->name);
          }
         
        }

  }
});

Thank you!!

Posted

What kind of field is "entryPages"? I suppose an integer field due to the fact you're comparing with the user->id.
And in which situation a user would write inside that field an exact id?

...or, entryPages is a page reference filed, but since it's plural you are dealing with an array of pages, so comparing a single id with an array would never match?

  • Like 1
Posted
2 minutes ago, 3fingers said:

What kind of field is "entryPages"? I suppose an integer field due to the fact you're comparing with the user->id.
And in which situation a user would write inside that field an exact id?

If it's a Page reference field, then maybe you need: $page->entryPages->id

Actually, given that it's Page "s", plural, then you might need: $page->entryPages->first()->id

or maybe you need to check if the $user->id is in an array of entryPages IDs?

Posted
Just now, adrian said:

If it's a Page reference field, then maybe you need: $page->entryPages->id

Yes, edited right before your answer ?

  • Like 1
Posted

it´s a Page reference - field and this writes the needed id into the session.

But i cannot compare it with $user->id.

When i write $user->id == 1234 - this works

wire('session')->message("Hello World 2 ! You saved {$page->entryPages}")
Posted

I have to get off the computer now but for others to help, it would be helpful if you provide the error and also let us know what the setup is for that page reference field - single vs multple etc. Otherwise we're just guessing ?

Posted

This comparison, written like this, shoud never return true:

if( $user->id == $page->entryPages )

How could a user id match another page id?
Are you trying to check if a particular user is the one who created the page you are checking against?
In this case you could use $pageToCheck->createdUser where $pageToCheck is your $page->entryPages (still think this is a WireArray and need to be traversed before).

Posted
1 hour ago, adrian said:

If it's a Page reference field, then maybe you need: $page->entryPages->id

Actually, given that it's Page "s", plural, then you might need: $page->entryPages->first()->id

or maybe you need to check if the $user->id is in an array of entryPages IDs?

$page->entryPages->id did it, when adjusting the fieldtype-settings.

Thanks for pointing to this....

Posted

Glad you sorted out! But I still don't get the logic where $user->id could match $page->entryPages->id, now I'm the one asking for help ?
Maybe @adrian could enlighten my tired brain? ?

Posted
7 hours ago, 3fingers said:

But I still don't get the logic where $user->id could match $page->entryPages->id

My assumption was the entryPages allowed selection of users. I just assumed it was multiple users which is why I thought the ->has() approach would work, but if it's a single page, then id === id works, as of course would $user === $page->entryPages

But again, I am not certain ?

Posted
9 hours ago, 3fingers said:

Glad you sorted out! But I still don't get the logic where $user->id could match $page->entryPages->id, now I'm the one asking for help ?
Maybe @adrian could enlighten my tired brain? ?

hi 3fingers,

i think it's great that you are thinking about my usecase. maybe it's not the "right" way to solve this problem.
i will describe it briefly:
users can log in and create a profile. this profile should then be accessible via its own url on the site. ( entry/url-user-1/
to achieve this, i create (at the moment manually) a page name per user ( url-user-1 ), which i combine with the entry of the user. in the template of the url page i have the possibility - per page - to select the appropriate user ( $user->id ) via a page-reference field.
since i may want to use this url as a segment in the further course of the portal, i want to store it automatically in the respective user entry during the assignment.
This way I can achieve that every user gets his own page, which can then also appear in a sitemap.
the much more elegant and less cumbersome way would probably be to enter the url in the user-entries themselves and automatically create a page with the template mentioned above, which is linked to the user->id. For this I simply did not yet deal enough with the hooks.
i hope i could now contribute even more to the confusion ?
Many greetings

Posted

Thanks @adrian and @itsberni, now it's clear what you are doing, thanks for the explanation ?
Nice gem from an old blog post too @Jan Romero, I remember I read it once it was published but I forgot it completely. There is so much knowledge "hidden" in the blog which is missing from the docs unfortunately.

Posted
1 hour ago, Jan Romero said:

Have you considered using the custom user template and parent feature? That way you wouldn’t need to link the profile page to a user. You could just put user pages in the appropriate place in the page tree ans use those as profiles.

no, i did not. i didn't find a way how user-profiles could be integrated into the "normal" page tree. i will dig in this first.

Anyway, thanks a lot to all of you!

Posted
14 minutes ago, itsberni said:

no, i did not. i didn't find a way how user-profiles could be integrated into the "normal" page tree. i will dig in this first.

Anyway, thanks a lot to all of you!

ok - read it.

i think that's exactly what i need. thanks for sharing!
the question now is only if this is compatible with the login/register ( Pro - Version ).
in the module settings of login/register the fields refer exclusively to the template "user".
i guess i could get problems here...
What do you think?

Posted
6 hours ago, itsberni said:

the question now is only if this is compatible with the login/register ( Pro - Version )

Currently it doesn't support it.

It's probably a pretty easy hack to make it support your scenario, but also ask Ryan about it in the support board for that module so he knows that users want that functionality.

 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...