Jump to content

If Page is selected, display Output


louisstephens
 Share

Recommended Posts


**Let me try to clear up what I am trying to do. Ill start by breaking the frontend up from the backend.

Backend:

I have a pageField (that is inside a repeater with 3 other fields) that allows a user to select only one page from the page tree which the template will use to grab the link to:

<a href="$page->pageField->url;>Link to Selected Page</a>

So basically, the repeater is set like:

pageRepeater

  - FieldOne

  - FieldTwo

  - FieldThree

  - pageField

 
 

Front End (template):

My template was set like:

<?php
foreach($page->pageRepeater as $myVariable) {
echo "<img src='{$myVariable->fieldOne->url}' class='img-responsive' />";
echo "<p>{$myVariable->fieldTwo} AS SHOWN</p>";
echo "<p>{$myVariable->fieldThree}</p>";
echo "<a href='{$myVariable->pageField->url}'>Link to Selected Page</a>";

}
?>

All of this worked quite well, however I ran into one small issue. Occasionally, the user will not need the pageField, and will not won't the "Link to Selected Page" to display on site. I had thought that I could use an if/else statement to display the link only if a page is selected. Which is why I was trying to figure out if a page was selected.

  1. check if a page is selected using pageField

  2. if a page is selected: "grab the pages url" and display the link on the frontend.

  3. if a page is not selected: do not display the link on the frontend

I should have been a lot more clear when I originally wrote the post, but I had a lot running through my head regarding the if/else statement. I have updated the original post with this information.

Link to comment
Share on other sites

Could you please pick one:

a) You need to check whether the page field has ANY value or

b) You need to check whether the page field has a specific value (a specific page) or

c) You need to check whether the page field even exist on the page's template.

If a) or b) please check the Details tab of you field to see how is the field presented in the API - as an array or single page.

  • Like 1
Link to comment
Share on other sites

Hmm, Ill have to play around with this. 

foreach($page->pageField as $testing) {
echo "<div class='mix col-xs-12 col-sm-6 col-md-3 '><img src='{$page->pageField->url}' class='img-responsive' />";
						
	if($page->pageField->has($page)){
	echo "test";
	}
	else {
	echo "bob";	
	}
}

It seems like it is not outputing anything at all now (currently do not have any page selected).

Link to comment
Share on other sites

That code won't work...
 
First, grab some pages, then check if they are selected in the current (or some other) page's pagefield:

$pagesPotentiallySelectedInNameOfYourPageField = $pages->find('template=products, limit=50');
$someOtherPageWithNameOfYourPageField = $pages->get(1234);

foreach ($pagesPotentiallySelectedInSomePageField as $p) {
	if($page->name_of_your_page_field->has($p)) echo 'yes, am right here, wohoo!<br>';
	//if($someOtherPageWithNameOfYourPageField->name_of_your_page_field->has($p)) echo 'yes, am right here, wohoo!<br>';
	else echo 'not in here!<br>';
}

http://cheatsheet.processwire.com/?filter=has

Link to comment
Share on other sites

Well, I am in confusion. I cannot understand what are you trying to do Could you write a bit more about you case?

1) What fields does the $page we are writing output for have? I assume it does have a Page Field named pageField, is that right?

2) What pages are allowed to be selected for pageField?

3) Is that right that you can only select 1 page for pageField (as you stated before)?

And so on...

Please provide more details.

Link to comment
Share on other sites

I do apologize Ivan. Let me try to clear up what I am trying to do. Ill start by breaking the frontend up from the backend.

Backend:

I have a pageField (that is inside a repeater with 3 other fields) that allows a user to select only one page from the page tree which the template will use to grab the link to:

<a href="$page->pageField->url;>Link to Selected Page</a>

So basically, the repeater is set like:

pageRepeater

  - FieldOne

  - FieldTwo

  - FieldThree

  - pageField

 
 

Front End (template):

My template was set like:

<?php
foreach($page->pageRepeater as $myVariable) {
echo "<img src='{$myVariable->fieldOne->url}' class='img-responsive' />";
echo "<p>{$myVariable->fieldTwo} AS SHOWN</p>";
echo "<p>{$myVariable->fieldThree}</p>";
echo "<a href='{$myVariable->pageField->url}'>Link to Selected Page</a>";

}
?>

All of this worked quite well, however I ran into one small issue. Occasionally, the user will not need the pageField, and will not won't the "Link to Selected Page" to display on site. I had thought that I could use an if/else statement to display the link only if a page is selected. Which is why I was trying to figure out if a page was selected.

  1. check if a page is selected using pageField

  2. if a page is selected: "grab the pages url" and display the link on the frontend.

  3. if a page is not selected: do not display the link on the frontend

I should have been a lot more clear when I originally wrote the post, but I had a lot running through my head regarding the if/else statement. I have updated the original post with this information.

Link to comment
Share on other sites


// You need to to have the setting set: return null page if none selected.

// When there's no page, a nullpage is returned, that page has ID 0, thus evaluates to false

if ($myVariable->pageField->id) echo "<a href='{$myVariable->pageField->url}'>Link to Selected Page</a>";

  • Like 4
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...