Forum
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.
Ok have this working with a normal Runway page using a master template.
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
You'd use
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 -
Here is my code
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.
I think that where you have
you might need:
(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
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.
'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