Get URI from Select field

This seems really obvious, but I cannot get it to work for me. Basically, I want the user to select a child page from the Select box in the panel, and from there, I want to use that Page to echo out the title, etc.

But I can’t get the page called in my code. ‘Home’ is the parent page, ‘category()’ is the child page name from the select dropdown in the panel.

<?php $feature = $pages->find('home')->category(); ?>

<h1><?php echo $feature->title()->html() ?></h1>
<h3><?php echo $feature->date('l, F j, Y') ?></h3>
<p><?php echo $feature->text()->kirbytext()->excerpt('400') ?></p>

Thanks so much for any help!

In the first line you are fetching the homepage’s category field itself. On its own it has no relation to the page object you are trying to access (it is simply the string you entered into the field).

If the pages are placed on the top-level, you can access them using the following:

$category = (string)$pages->find('home')->category();
$feature = $pages->find($category);

Otherwise you will need to prefix the second line with the parent page:

$feature = $pages->find('parent/' . $category);
1 Like

Thanks for your reply @lukasbestle, but this didn’t work either.

What exactly didn’t work? Unfortunately, my crystal ball is being repaired right now. :smiley:

lol, Sorry for the vagueness… :no_mouth:

So it looks like that is still not outputting anything. I tried both versions you mentioned, just in case – and neither one seems to be converting that text from the Select field into a usable Page for the echos below it.

The rest of the page below that bit of code does not load. So it’s breaking there. And thanks for your help!

What happens if you insert a var_dump($category) right after the first line?

That produced:

string(9)"EventName"

“EventName” being the title of the page I’m trying to call items from.

If Eventname is the title, then it obviously isn’t the uri you are trying to get here. Could you post your blueprint?

Absolutely, here you go! It’s a mostly static home page, with an intro blurb and a featured event.

title: Home

pages: false

fields:
	title:
		label: Title
		type:  text
	intro:
		label: Introduction
		type: markdown
	category:
		label: Featured Event
		type: select
		options: query
		query:
			page: events
			fetch: children

Like I mentioned earlier, I would like the user to select the event to be featured from a Select dropdown. I thought that as long as it was outputting the name of the event page, I could easily pull that “category” (ex. EventName) and get my event page title, date, description, etc. from there. But that has not happened.

Thank you guys!

I guess one of the two solutions below should work:

$featuredEvent = $site->index()->findBy('uid', $category);

or

$featuredEvent = page('events')->children()->findBy('uid', $category);

I still wonder why $category outputs the title, the default is the uid …

1 Like

@texnixe – That second one got it!! Thank you so much, I really appreciate it.
And reading your solution makes sense… I just couldn’t get there myself!

Having the same issue — using a select dropdown in the panel to choose from a list of venues for an event. Here’s my implementation of the suggested solution:

Event snippet:

<?php $featVenue = page('venues')->children()->findBy('uid', $venue); ?>

<p>Venue: <a href="<?php echo $featVenue->url() ?>">
  <?php echo $featVenue->title() ?>    
  </a>
</p>

Event blueprint:

venue:
    label:   Venue
    type:    select
    options: query
    query:
      page: venues
      fetch: visibleChildren

Just to clarify, when I add a venue from the dropdown in the Panel, only the bare page uid is returned. So for example, if the venue title was Coffee Shop, it gets saved in the markdown file as venue: coffee-shop.

What exactly doesn’t work for you? And how do you get the $venue variable?

Spoiler alert!

It looks like it should be $page->venue() instead of $venue

Well, that’s why I asked how he got the variable. :slight_smile:

1 Like

@Thiousi @lukasbestle YES! That’s it! Probably spent two hours trying every syntax combo I could think of… thanks again :sweat_smile:

Sometimes you’re just with your head so focused on the code that you don’t even see the obvious just in front of you :slight_smile:

Glad we could help!

A little bit of that, and a lot of:

1 Like