Select field to query subpages and retrieve URL


I’ve setup a select field in a blueprint which displays a list of subpages which use a specific template which is working well, however after researching and trying some options I cannot figure out how to retrieve the full URL to the selected subpage for use within a template.

To get around this, I’ve done the below which is giving me the correct URL but wondering if there is a better / cleaner way?

<?php echo $site->url().'/'.$project->project() ?>

Here is the field within the blueprint:

    label: Project URL
    type: select
    options: query
    query: site.index.template('portfolio-project').listed

Any help / advice would be much appreciated!

Thank you,

<?php if ( $p = $project->project()->toPage(',') ): ?>
    <?= $p->url(); ?>
<?php endif; ?>

Not 100% sure if the comma is needed here as a parameter in the toPage() function. I usually use the pages field instead of a select field to select pages.

Hi Texnixe,

Ahhh, I didn’t even see the pages field, I thought I had to query via a select field. I’ve just updated the blueprint to use a pages field which is working perfectly.

In the template I’m tried using the e() helper to check if the field exists before outputting the value but I’m getting a ‘Call to a member function url() on null’ error when appending url() to the value string:

<a href="<?php e($project->related(), $project->related()->toPage()->url()) ?>">

Do I receive the error because I need to convert the page to a page object using the toPage method before being able to output its URL? If so, how would I go about doing this within the e() helper or is this not possible?

When I omit url() I effectively output a relative URL but I was aiming to output the absolute URL:

<a href="<?php e($project->related(), $project->related()->toPage()) ?>">

I think I’ve also just confirmed my suspicion around having to convert to a page object first, so I’ve implemented the below:

    if ($project->related()->isNotEmpty()):
        $projecturl = $project->related()->toPage()->url();

<a href="<?php echo $projecturl ?>">View Project</a>

In short, just wondering if I can simplify and grab the pages URL within the e() helper alone?

Thank you,

The e() helper is not useful here, you have to use an if statement like I posted above (only I was very stupid and wrote toFile() when I meant toPage(), but this is corrected now.

You don’t have to check if the field exists or contains a value in this case, because we have to make sure that we have a page object, so even if the field exists and is not empty, we cannot be sure the page hasn’t been deleted in the meantime. And if the field is empty, the check for the page object will return false anyway.

So please use the if statement I posted above.

And your link should go inside the if statement, because you don’t want a link if there is no url (plus your $projecturl would be undefined and throw an error if the page doesn’t exist).

<?php if ( $p = $project->project()->toPage() ): ?>
    <a href="<?php echo $p->url() ?>">View Project</a>
<?php endif; ?>

That makes sense, and I thought I had figured that one out :wink:.

I’ve implemented the if statement you suggested which is working perfectly, thank you! :+1:

1 Like