Forum

Thread tagged as: Error, Api, Runway

Headless request PHP error with categories in template

I'm getting a PHP error Fatal error: Call to a member function to_array_for_api() on boolean in /var/www/website/dist/perch/core/lib/PerchFieldTypes.class.php on line 2551 when trying to create a Headless response from a Collection that has a <perch:categories /> tag in the template.

I did some quick debugging and it looks like the get_api_value() method of the PerchFieldType_category class is expecting a numeric value to be passed to the call to $Categories->find() (line 2551, as above), but it's getting a string. Relevant templates below.

This isn't an immediate problem as I don't need the categories in the headless response so can just make another template with the <perch:categories /> tag removed, but I thought it should be logged, or perhaps there's something wrong in my templates?

/perch/templates/api/products.php

<?php
  $Headless = $API->get('HeadlessAPI');
  $Response = $Headless->new_response();

  $Collections = $API->get('ContentCollections');
  $Collection = $Collections->find('Books');

  $BooksSet = $Headless->new_set('books');
  $BooksSet->add_items($Collection->query([]));
  $Response->add_set($BooksSet);

  $Response->respond();

book.html (the collection template)

<perch:content id="title" type="text" label="Title" required="true" help="The book title e.g. \"Positive Poker\"." title="true" divider-before="Book details" />
<perch:content id="subtitle" type="text" label="Subtitle" help="The book subtitle e.g. \"A Modern Psychological Approach to Mastering Your Mental Game\"." />
<perch:related id="authors" collection="Authors" label="Authors" help="The author(s) of the book."></perch:related>

<perch:content id="bestseller" type="checkbox" label="Bestseller?" value="yes" help="Check to display this book in the 'Bestsellers' menu." />
<perch:content id="coming_soon" type="checkbox" label="Coming soon?" value="yes" help="Check to display this book in the 'Forthcoming' menu." />
<perch:content id="is_new" type="checkbox" label="New?" value="yes" help="Check to display this book in the 'New' menu." />
<perch:content id="show_on_schedule_page" type="checkbox" label="Show on schedule page?" value="yes" help="Check to display this book on the D&B schedule page." />

<perch:content id="image" type="image" label="Image" bucket="Books" help="The book cover. Will be cropped to 290px x 430px for website display." />
<perch:content id="image" type="image" width="290" height="430" crop="true" bucket="Books" suppress="true" />
<perch:content id="image" type="image" width="145" height="215" crop="true" bucket="Books" suppress="true" />

<perch:content id="isbn" type="text" label="ISBN" help="ISBN of the PHYSICAL book, e.g. \"9781904468431\"." />
<perch:content id="publication_date" type="text" label="Publication date" help="e.g. \"US Jun 2015 / Europe Jul 2015\"." />
<perch:content id="specifications" type="text" label="Book specifications" help="e.g. \"496 pages, 240 x 170mm/7 x 9\"." />

<perch:categories id="categories" label="Categories" set="poker" help="The book categories. Used for the 'Store' menu and filters on the Store page." />

<perch:content id="sku" type="text" label="SKU" help="Usually the eBook ISBN. MUST MATCH EXACTLY THE VALUE SET IN FETCHAPP." divider-before="eBook details" notes-before="D&B Poker eBooks are hosted by FetchApp (https://dandbpoker.fetchapp.com)." />
<perch:content id="price" type="text" label="Price" help="The customer is charged the price set here, in Perch. USD: Do NOT include the dollar symbol ($). Leave blank if an eBook is not available for purchase." />

<perch:content id="pre_orders" type="checkbox" label="Pre-orders available?" value="yes" help="Is this product available for pre-order? Requires a SKU and price to be entered above, and a FetchApp product with a dummy file attached." divider-before="Pre-orders" notes-before="Go to D&B Poker from the Apps menu for notes about pre-orders." />
<perch:content id="pre_orders_text" type="textarea" label="Text for pre-orders section" markdown="true" editor="markitup" size="xs" help="Appears in the popup when a user clicks 'About pre-orders'. Note: this is required if the pre-orders checkbox above is selected. e.g. \"Don't miss out! You'll be sent the book as soon as it's available.\"" bucket="Books" />

<perch:content id="description" type="textarea" label="Description" markdown="true" html="true" editor="markitup" size="xl" notes-before="The book description. Markdown, but minimal HTML is supported." divider-before="Content" bucket="Books" />

<perch:repeater id="extracts" label="Extracts" divider-before="Extracts" notes-before="NOTE: Only the first extract in this list will be displayed on the website. Additional extracts are still here as legacy - recommended: add them to the Description above with the file upload button if users require access.">
    <perch:content id="extract_file" type="file" label="File" bucket="Extracts" help="Should be a PDF file." />
    <perch:content id="extract_type" type="select" options="Extract, Intro, Contents, Appendix" required="true" label="Extract type" />
</perch:repeater>

<perch:repeater id="quotes" label="Quotes" divider-before="Quotes" notes-before="Displayed in the right-hand column of the book detail page.">
    <perch:content id="quote" type="textarea" label="The quote" markdown="true" editor="markitup" size="s" help="What was said?" bucket="Books" />
    <perch:content id="quote_source" type="text" label="Source" markdown="true" editor="markitup" help="Who said it?" />
    <perch:content id="quote_display" type="checkbox" label="Product page" value="yes" help="Should the quote be displayed on the book page?" />
</perch:repeater>

<perch:related id="related_books" collection="Books" label="Books" divider-before="Related products" help="Displayed in the \"You might also like\" section of the product page"></perch:related>

<perch:content id="slug" type="slug" for="title" label="Slug (title)" editable="true" indelible="true" help="A slug of the book title, that forms part of URLs e.g. \"lifes-a-gamble\". Note: once set, this field is not editable." divider-before="Miscellaneous" />

poker.html (the category template)

<perch:category id="catTitle" type="text" label="Title" required="true" help="e.g. \"Hold'em\", \"PLO\", \"Casino\", \"Tournaments\" etc." />
<perch:category id="catSlug" type="slug" for="catTitle" suppress="true" />

category.html (the category set template)

<perch:categories id="setTitle" type="smarttext" label="Title" required="true" />
<perch:categories id="setSlug" type="slug" for="setTitle" label="Slug" />
Simon Ansell

Simon Ansell 1 points

  • 3 years ago

When I query the Collection with

  $BooksSet->add_items($Collection->query([
    'template' => '/books/book_for_api.html',
  ]));

where /books/book_for_api.html is identical to the book.html template above except for having the <perch:categories /> tag removed, there is no error (but the categories do appear in the response nevertheless).

Drew McLellan

Drew McLellan 2638 points
Perch Support

Thanks, I've logged it.

Thanks Drew.