Jump to content

Getting the selected value of a inputtype Page field


jtborger
 Share

Recommended Posts

Hi,

I have a global field assigned to all templates, called 'Channel'. It is a Page field, which links to a 'Tag' page (template) which only has a title field. This way I created a sort of 'channel tag' for content.

Now, I try to get the channel value of a certain page. And somehow, this doesn't work. 

I have a recursive function:

public static function getPageChannel($page)
	{
		
		$level = count($page->parents);
		var_dump($level,$page->name,$page->channel->title);
		if($level > 1)
		{
			self::getPageChannel($page->parent);
		}
		else if($level == 1)
		{
			return $page->channel;
		}
	}

which prints out the stuff. But channel always returns a pageArray - which is OK. But then, How do I get the selected channel page? doing first() or whatever doesnt work cause I get the first option, not the chosen page (value).

Must be thinking the wrong way. Feels a bit stupid because it must be something easy...

(function is not finished yet, I know for the above example I can use $page->rootParent (equals $level==1). But when it does work returning channel value, I have it modified to return the first found channel value... up on the path).

Link to comment
Share on other sites

Am surprised there's been no response yet to your query. I'm wondering if maybe the question is not clear? I'm trying to understand exactly what you want to do but I'm not getting it. Then again, I'm slow that way  :D.

Are you trying to create a tagging system? Why a module instead of  directly using the API and page system (using page reference fields as you are)? Sorry if am off topic but just trying to understand your question :)

Link to comment
Share on other sites

Hey jtborger,

I'm also not quite clear, what you are doing here. Can you please describe how your field "Channel" is set up and what your code does output at the moment?

If you always have only a single channel you want to choose, set your "Channel" to "Dereference in API as:  Single page (Page) or boolean false when none selected" in the "Details" tab of the field configuration in the API. Then you can get the value of your field just as usual in your template:

// get the page object of your "channel" in your template
$channel = $page->channel;

// get the title of your "channel" in your template
$channelTitle = $page->channel->title;

The code you posted above should work without problems, as far as I can judge it without seeing more.

Link to comment
Share on other sites

Since this is a recursive function, you might want to pass your $level as a second argument so that you aren't relying upon external factors for the recursion. That way you control the value of it in your own function. Also note that it is possible for $page->parent to be a NullPage, which would have no parents. You might be better off checking the $parent->id to make sure that it is 1 or higher. ID 1 is always the homepage. ID 0 is typically a NullPage (or a new page that's not yet been saved, but that wouldn't be applicable here I don't think). 

Link to comment
Share on other sites

Hi all,

Thanks for the replies. Sorry if my question wasn't clear.

Are you trying to create a tagging system? Why a module instead of  directly using the API and page system (using page reference fields as you are)?

Well, it is a tagging system but with limited tags. And I use no module but just a page reference field.

That field is set up as follows:

Type=Page

Dereference as:Multi Page (I'll change that)

Selectable pages: from a certain parent which hold pages with a template (type=Tag)

Input field type: Select

If you always have only a single channel you want to choose, set your "Channel" to "Dereference in API as:  Single page (Page) or boolean false when none selected" in the "Details" tab of the field configuration in the API. Then you can get the value of your field just as usual in your template:

I'll try this when finished this post. A question which comes up with me: OK if this works, great! But, how can I get the selected page(s) of a Page reference field when returning Multi Page array?

Is there a sort of 'isSelected()' property of each page?

Thanks Ryan for feedback on my recursive function. I can pass $level as an argument, of course. But I dont see the direct benefit of it, because if parent level is not level-1, then.... something is in error I guess? So I would assume it should be safe to rely on. I'd rather pass the least arguments in a recursive function. Don't know exactly why that is.. Perhaps just a preference.

I'll check for NullPage or $parent->id. Thanks for the tip !

By the way I found out that with 'Dereference => Multi Page', the selected value = $field->last() . Is this a coincident and just a side effect? It seems to be right in the places where I implemented it so far. But I'll change to Single page anyway. It's easier for now. But I'm just curious.

Link to comment
Share on other sites

A question which comes up with me: OK if this works, great! But, how can I get the selected page(s) of a Page reference field when returning Multi Page array?

Is there a sort of 'isSelected()' property of each page?

Not sure I follow correctly but the below will return all selected pages in a multiple pages reference field

foreach ($page->name_of_multiplepagereference_field as $t) { //this is for the current page

//do anything with $t

}

In the above example, $t will return Page Arrays. Hence, at your disposal are all the properties of each selected page, their titles, their names....everything about them..

If you want to simply check if something is selected (i.e. if there is a value) you do....

if ($x ) {// $x here is some previous page results returned from find() or get ()

//do stuff.
}

I have a feeling the above are not what you are asking since they seem rather obvious from the documentation :)

Written in a hurry, there could be mistakes.

Link to comment
Share on other sites

kodongo, you write

Not sure I follow correctly but the below will return all selected pages in a multiple pagereference field

The thing is:

foreach($page->field_page_reference as $t)

returns all possible options, not only the selected.

I'll try soma's solution. But feels a bit ..weird. Because I first have to do as follows to implement a "getSelected()' in a page type field. I still think I don't understand somethings properly..


$proChannelPage = $pages->find("name='pro'");
$hifiChannelPage = $pages->find("name='hifi'");

//channel = reference page field
$isProSelected = $page->channel->has($proChannelPage);

$isHifiSelected = $page->channel->has($hifiChannelPage);

I think this is weird.. Seeing how beautiful PW is crafted there must be a sort of $page->pageReferenceField->getSelected() and/or isSelected() method.

Or am I missing something here?

Link to comment
Share on other sites

jtborger,

You misunderstand something, but I'm not sure what exactly :)

The following code:

foreach($page->field_page_reference as $t)

does not return you all possible options, but the ones that are selected on the current $page

Let's say you have two pages with the same template, each assigned a Page field to it which can hold multiple pages references.

Page A: You select 30 Pages in the Admin

Page B: You select 2 Pages in the Admin

Then when doing the loop, you will iterate over the selected pages only.

  • Like 1
Link to comment
Share on other sites

kongondo, you write

The thing is:

foreach($page->field_page_reference as $t)
 returns all possible options, not only the selected.

I think this is weird.. Seeing how beautiful PW is crafted there must be a sort of $page->pageReferenceField->getSelected() and/or isSelected() method.

Or am I missing something here?

As Wanze has aptly put it, it will only return selected pages in the fields of the multiple page reference field. ProcessWire is more clever than that ;)

Edit: Just for info

Behind the scenes, what it actually returns are the page IDs of the selected pages in the multiple page reference field. If you echoed out a multiple page reference field (without doing a foreach, e.g. echo $page->multiplePageRef;) you will get the IDs of selected pages (e.g. 1407|1007). If you checked in the database the table holding the multiple page reference field (e.g. field_multiplePageRef), you will see those page IDs as the saved values. Now, if you have the ID of a page, you can do almost anything with that - find our who the parents are, if they have children, blah, blah..  :)

Edited by kongondo
Link to comment
Share on other sites

Well... OK there was some misunderstanding here. What you both describe here, is what I expected from the beginning. But, in my implementation, this didn't work: it gave all the possible options. Somehow.

Then I fiddled a bit further and found another way. And that worked. I checked this topic today and went to my code (which didn't work) again, trying to reproduce the problem, which I couldn't (of course..? hmmm ). Now, it behaved like it should: I got only one 'channel' and Page in PageArray and that is the selected page.

So it is solved.. by time and perhaps some better understanding of my own code.. ;)

Thanks for the help and my excuses for the misunderstanding of things.

Link to comment
Share on other sites

My bet would be that you changed the page field settings from multiple to single and vis versa... however this can give unexpected results as it doesn't remove values already saved values when switched from a multiple to a single. So be careful. 

As for when using a page field single select, you already have the page object and not a page array. So the selected page would be simply the field itself.

echo $page->channel->title // title of selected page 

A method to check for select page seems redundant and not necessary, as with multiple (page array) you already have has(). I don't see any benefit to add isSelected().

Link to comment
Share on other sites

My bet would be that you changed the page field settings from multiple to single and vis versa... however this can give unexpected results as it doesn't remove values already saved values when switched from a multiple to a single. So be careful. 

As for when using a page field single select, you already have the page object and not a page array. So the selected page would be simply the field itself.

echo $page->channel->title // title of selected page 

A method to check for select page seems redundant and not necessary, as with multiple (page array) you already have has(). I don't see any benefit to add isSelected().

It is just what I did along the way, changing from multiple to single (and back, for reproduction). But I'm sure that the first, it wasn't working as expected.

Strange...Is this some known quirk of PW that you 'guessed' this was the case why the strange behaviour came up?

As with ->has(), I now see that isSelected() isnt necessary anymore because the multiple page array contains only selected values and has() suits another need than what i was looking for.

I mark it as solved. Thanks kodongo for reminding.

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...