Forum

Thread tagged as: Question, Runway

Ajax endpoint in Runway to return collection article

I am trying to set up a small php file that will return a collection article based on an ajax request via URL. I am unsure where best I can put this small php file within the runway directory and how I get it to access the runtime functions. I am getting errors like this - Call to undefined function perch_collection()

The aim is to have a page load an article item from a collection via ajax into an overlay based on the parent page (as the collection title) and the article slug. We have this working in a non-runway version of the site using static pages but I need to replicate in Runway.

Here is my first attempt at the code test. I have tried this file in numerous locations with the directory to no avail.

I can get this to work fine within a page created from a master template but not as a standalone file? I want to pass the collection slug in the URL too. eg.

article.php?collection=collectiontitle&article=articleslug

Then in file = // get collection title $parentTitle = $_GET['collection'];

The following code works with a normal runway page


<?php $p = perch_pages_parent_page(array( 'skip-template'=>true )); $parentTitle= $parentPage[0]['pageTitle']; // article retrieval based on slug $article = perch_collection($parentTitle, [ 'filter' => 'slug', 'match' => 'eq', 'value' => perch_get('article'), //get article slug 'skip-template' => 'false', 'return-html' => 'true', ]); // test output print_r ($article); ?>

I should add I am new to Runway and not an experienced PHP coder so keep it simple please. Any help much appreciated.

Keith Winter

Keith Winter 0 points

  • 4 years ago

Ok have this working with a normal Runway page using a master template.

// get collection and article from URL
$parentTitle = $_GET['collection'];
$test = perch_collection($parentTitle, [
        'template'   => 'article.html',
        'filter' => 'slug',
        'match' => 'eq',
        'value' => perch_get('article'),
        'skip-template' => 'true',
    ]);

print_r ($test);

I would like to have that run within a small php file outside of the Runway admin etc How do I get the runtime functions to load in a php file?

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd use

include('perch/runtime.php');

Ah ok did not realise that would work with Runway as it does in Perch. Didn't bother to try, sorry. Will give it a try, thanks.

Brilliant, thanks that works as you'd expect.

I now need to build a text string of slugs from a collection of articles. The following code almost works but throws an error at the same time -

This is the error - Warning: Illegal string offset 'slug' in /Applications/MAMP/htdocs/sqhc/site-admin/templates/pages/subpage.php on line 251

I have 2 'articles' test and test-2 in collection

$list2 ARRAY returns this -

Array
(
    [0] => Array
        (
            [_id] => 4
            [Title] => Test
            [_title] => Test
            [slug] => test
            [page-image] => 
            [alt] => 
            [TextLeft] => <p>adsfaead</p>
            [TextRight] => <p>fdfdafds</p>
            [_sortvalue] => 1000
        )

    [1] => Array
        (
            [_id] => 5
            [Title] => Test 2
            [_title] => Test 2
            [slug] => test-2
            [page-image] => 
            [alt] => 
            [TextLeft] => <p>222222</p>
            [TextRight] => <p>22222</p>
            [_sortvalue] => 1001
        )

    [html] => <h2>Test</h2>

Here is my code


$list2 = perch_collection('Medical Services', [ 'skip-template' => 'false', 'return-html' => 'false', ]); foreach( $list2 as $item2 ) { $commaList2 .= $item2['slug'].','; } echo "var contentList2 = ['".$commaList2."'];";

The output is

var contentList2 = ['test,test-2,<,'];

As you can see there is an unexpected <,

I want to get var contentList2 = ['test,test-2,'];

Thanks again Drew.

Duncan Revell

Duncan Revell 78 points
Registered Developer

I think that where you have

$list2 = perch_collection('Medical Services', [ 
'skip-template' => 'false', 
'return-html' => 'false', 
]); 

you might need:

$list2 = perch_collection('Medical Services', [
 'skip-template' => 'false', 
'return-html' => 'false', 
], true); 

(an extra "true" at the end of the line).

Hi Duncan Thanks but still get this error - Warning: Illegal string offset 'slug' in /Applications/MAMP/htdocs/sqhc/site-admin/templates/pages/subpage.php on line 247

and the output is still

var contentList2 = ['test,test-2,<,'];

That odd < is still there>

K

Duncan Revell

Duncan Revell 78 points
Registered Developer

Ah, it's probably something to do with $list2 having the [html] element - you're stepping through the array with foreach and hitting [html] which isn't an array and therefore doesn't have a 'slug' key. And the < is ending up in the output...

Ah ok, thanks, makes sense but how do I fix that? I'm new to this so please bear with me.

Fixed it myself by removing the attribute 'return-html' => 'false',

This works - $list2 = perch_collection('Medical Services', [ 'skip-template' => 'false', ]);

Thanks again for your help, much appreciated.

Duncan Revell

Duncan Revell 78 points
Registered Developer

'skip-template' should be false by default, but I've just realised something:

When you're setting options to be true or false, don't use single quotes (which makes it a string, which is evaluated as true by PHP).

So, 'return-html' => 'false' was actually the same as writing 'return-html' => true. Which means you are setting skip-template to true as well.

If you get my drift...

I think so yes. I got it working by removing those 2 attributes altogether. Many thanks