Jump to content

Selecting Page from List to Used as URL


louisstephens
 Share

Recommended Posts

I appreciate all the help thus far in learning how to navigate Processwire. However, I have hit a road block. I have an Image Field in one of my templates (it is being used in a repeater), and I would like to assign a preexisting page's url to the image .

Ie:

<a href="url-to-page"><img src="code-for-image" /></a> 

However, maybe I have missed something, as I set up the field (homepageImg) to be a "page select" but nothing is currently populating the dropdown. I am merely trying to get all the children of the parent (that the current page is in) and display them. I thought this would make it easy for someone to set the URL on the fly. Any help would be appreciated.

Link to comment
Share on other sites

The following code snippet wraps an image with an anchor tag, if the first word of the image description matches a pagename of a page in your page tree

$image = $page->images->first();

$selector = strstr($image->description,' ',true)?strstr($image->description,' ',true):$image->description;
$selector = trim($selector,"\W"); // trim punctuation

$linkpage = $pages->get('name='.$selector.',has_parent!=2'); // modify selector for your needs
$anchor = $linkpage->id?"<a title=\"go to $linkpage->title\" href=\"$linkpage->url\">":null;

echo  "$anchor<img src=\"$image->url\" alt=\"$image->description\"/>".($anchor?'</a>':'');

to get all siblings of your page use

$pages->siblings($selector);
Link to comment
Share on other sites

Thank you all for the quick responses. After rereading my original post, I think I need to clarify the issue. 

I have a repeater set up with:

Name

Image

PageUrl (this is the field set to a page field).

I thought by setting pageURL to a page field type, I could then select a page, and then pull that url using something similiar to what kixe describes. However, nothing is showing up in the actual dropdown. Is there a better way to allow a user to select a "url" to use for linking the image?

Link to comment
Share on other sites

Maybe I am not understanding, but it should be as simple as returning this:

$homepageImg->url

Just make sure that the homepageImg field is set to "PageListSelect" without the "Multiple" and that the "Dereference in API as" is set to Single.

That should then return the URL to the page they selected.

Link to comment
Share on other sites

Thanks Adrian. The idea in my head might not be coming out in the actual post as well as I intended. The basic set up is thus:

I have an Image field in my template as well as a field called 'imageURL' (all in a repeater). My hopes were that I could easily use a drop down to allow a user to change the url of the image on the fly without having to manually enter the information.

The page Structure I have is:

Home

 - Sub Page 1

   - Sub 'Sub' Page

   - Sub 'Sub' Page

   - Sub 'Sub' Page

   - Sub 'Sub' Page

   - Sub 'Sub' Page

 - Sub Page 1

 
 
For the url field, I was hoping to just grab the list of "Sub Sub Pages", and snag the url from that to link the image with.
Link to comment
Share on other sites

I think I had the same problem recently. I'll trawl through a project I had in progress and see how I solved it.

You basically want to allow clients to apply a link to an image. The link around the image would direct front end users to an internal page.

On the Admin side, you're using the pageselect field to achieve this.

Any code examples and screenshots of the Admin for us?

Link to comment
Share on other sites

Ah ha.. That does the ticket. And now for something probably too confusing. I know this method would only work for this repeater on this page. However, what If I wanted to reuse this repeater and its fields on another page tree all together (without recreating the repeater everytime). 

Link to comment
Share on other sites

Ah ha.. That does the ticket. And now for something probably too confusing. I know this method would only work for this repeater on this page. However, what If I wanted to reuse this repeater and its fields on another page tree all together (without recreating the repeater everytime). 

Depending on your setup, you could probably make use of the "Custom PHP code to find selectable pages" option and reference the $page->parent or $page->parent->parent - hopefully you get the idea.

  • Like 2
Link to comment
Share on other sites

Perhaps a dumb question Adrian, I see that return $page->parent->parent(); returns the top level so:

- Sup Page 1

- Sup Page 2

- Sup Page 3

- Sup Page 4

How would I drill down and list all the children of the parent? I know about first();, but didnt know if there was an "All" selector.

I do appreciate all the help as I stumble through this.

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

×
×
  • Create New...