Jump to content

Page Reference Beyond ID


fuzendesign
 Share

Recommended Posts

I created a select field that uses Page Reference. Works fine. But I don’t retrieve (and store) the ID of the referenced page, I want to use another field in that looked-up record as the value of my select/dropdown.

Page Name: Temp Order
Page Name URL: temp-order
Field I want to grab: email

PW makes it easy to assign multiple fields to comprise the ***label*** for that dropdown/select, but the ***value*** itself of the field is naturally hardwired to the page ID. That I understand.

But how can Page Reference be used beyond retrieving an ID? And can that be done within the Selector String field in the Input settings for that field?
 

Link to comment
Share on other sites

23 hours ago, wbmnfktr said:

In short... you could just grab that value saved within your reference.

<?php

$myreference = $page->referencefield->email;

 

Yes, but that’s after the fact. I want to grab and assign the field to the dropdown upon loading the form.

Link to comment
Share on other sites

Depending on what kind of page reference field you are using, you could achieve that with a hook, for example in site/ready.php.

In this example I am using a page reference field named 'page_reference' and as the Input Field Type I am using a Select field. Now I hook into the Inputfield::render method with a before hook:

// use before hook to alter options passed to the Inputfield
$wire->addHookBefore('InputfieldSelect::render', function ($event) {
    // return;
    if($event->object->name != 'page_reference') return;
    // build our new options array
    $options = array();
    // $event->object->options is an array of options originally passed to the InputField
    foreach($event->object->options as $id => $title) {
        $options[$this->pages->get($id)->name] = $title; // exchange name for email
    }
    // set our new options to the InputField
    $event->object->options = $options;
});

This sets the value of the <option> tag to the page name. you can use $this->pages->get($id)->email to set the emails as value.

alter-select-options.thumb.png.c25cf122057c3026541c48f067634679.png

Don't know where you render that form. If it is a custom form somewhere, this should do. But be careful if you plan to use this in the PW page editing form. You will need to take some extra steps when saving the page. Because PW needs the ID of the page to be saved. In that case you can have a look at InputfieldPage::processInput and hook into that to revert your email values back to IDs.

  • Like 2
Link to comment
Share on other sites

@gebeer That code works perfectly!…

Unfortunately, I’m using that field within a Form Builder form. So after the Form Builder module saves the form to a Processwire page, it sends an empty value. On the form page itself, though, the select menu properly sees the code you supplied and the values in the select menu do show up properly.

Link to comment
Share on other sites

The option value is not visible to the user of the form. What are you using this form for and why do you need the option values to be email instead of ID? Please describe what you want to do with that form data. Maybe I should have asked that question before posting my solution above ?

Link to comment
Share on other sites

2 hours ago, gebeer said:

The option value is not visible to the user of the form. What are you using this form for and why do you need the option values to be email instead of ID? Please describe what you want to do with that form data. Maybe I should have asked that question before posting my solution above ?

Yes, I know the value isn't visible to the user; I had my web dev inspector open and saw your code does exactly what I asked. The values _are_ being correctly assigned to the value attributes of the menu. Nothing wrong with your code and it’s much appreciated.

But it’s my fault that I assumed saving would work without a hitch within Form Builder. Especially since the select menus are populated just perfect.

The problem is that Form Builder’s seems to be stripping the values upon saving it to a PW page. Which tells me that I need to utilize a variation of your code _within_ Form Builder’s hook system. Otherwise it disregards certain things. Sort of like pre-populating fields needs to be done a “Form Builder way.”

Direct Message me and I’ll drop a little something your way for your time.

Link to comment
Share on other sites

4 hours ago, fuzendesign said:

Direct Message me and I’ll drop a little something your way for your time.

Thanks a lot but no need.

What are you doing with the form data once it is submitted?

Instead of using my hook to alter the value in the form it would be much easier to hook into the processing of the form and get the email there from the ID and save it (just like @wbmnfktr suggested).

Link to comment
Share on other sites

6 hours ago, gebeer said:

Thanks a lot but no need.

What are you doing with the form data once it is submitted?

Instead of using my hook to alter the value in the form it would be much easier to hook into the processing of the form and get the email there from the ID and save it (just like @wbmnfktr suggested).

I decided to take a different approach which kind of eludes to what you’re saying. I’ve split up my Form Builder form into two different pages. I'm passing the page reference ID to the second page. I’m not using a hook per se, but here’s where I’m stuck and once I have the answer then it’s smooth sailing. Perhaps you can answer:

 

Link to comment
Share on other sites

On 11/11/2021 at 9:56 PM, fuzendesign said:

Perhaps you can answer

You are pointing to a solved thread and asking me for an answer. You say you are stuck but don't tell where you're stuck. I'm afraid I can't help you if you don't give exact details of your problem.
EDIT: reading my reply again, it sounds a bit grumpy. That was not really intended. Just wanted to say that you have higher chances in getting a helpful answer if you try and explain your problem in more detail, possibly with some code that you already have.

Link to comment
Share on other sites

4 hours ago, gebeer said:

You are pointing to a solved thread and asking me for an answer. You say you are stuck but don't tell where you're stuck. I'm afraid I can't help you if you don't give exact details of your problem.
EDIT: reading my reply again, it sounds a bit grumpy. That was not really intended. Just wanted to say that you have higher chances in getting a helpful answer if you try and explain your problem in more detail, possibly with some code that you already have.

No worries @gebeer. You are correct, I should have just hooked into Form Builder and grabbed what I needed there. I’m learning as I go. I’ve never used hooks and this is my first true use of Form Builder.

Your efforts were not in vain. I learned much from it that I’m using what I learned for another part of my mini app.

The alternate direction I took was passing a Customer ID from form 1…onto form 2…and then querying the email there. It was a much better and smarter solution than I had initially planned since knowing the customer (ID) would allow me to auto populate a field with just **that customer”s** email, instead of sending all customer emails to that dropdown and expecting my client to choose the appropriate email tied to the customer. As you can imagine, not very smart but I was trying to bang something out that would hold me up for a week while I developed this app further.

Also, I pointed to that other thread because it contained a similar problem I was encountering. In retrospect, I should have started a **new** thread on that post instead of posting to Solved. Another learned moment.

And just yesterday, I understood why my code in that other thread wasn’t working.

Thanks again for your initiative. The PW community is great.

  • Like 1
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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