Posted 16th Dec 2017
#api #bootstrap4 #completeguide #fields #selector #urlsegments
Create the 'cat-index', 'cat-entry' and 'cat-attribute' templates.
Create the files 'cat-index.php' and 'cat-entry.php' files and create a new page called 'Cats' under 'Home' using the 'cat-index' template.
So, as things stand, you now have a path at '/cats/'. We can say this path resolves to a page on our site. That is, whatever code we put in 'cat-index.php' will now run at this URL. Now, this page has no child pages, therefore the path '/cats/persian/' does not resolve to a page on our site. Instead, this will generate a 404 error because the page does not exist.
We're going to make that URL (and others) work. That is, we're going to list our cats, within the same template 'cat-index.php', based on the end part of the URL which is this case was '/persian/'. If URL segments are enabled for the 'cat-index' template, and the path does not resolve to an actual page, then the '/persian/' part of the URL is known as URL segment number 1.
Now we can create a new page called 'Type' under 'Home' using the 'cat-attribute' template.
Now you could use two templates for this, one for the parent 'cat-attribute-index' and one for the children 'cat-attribute-entry'. This way you could easily get the types by:
<?php
// get children (PageArray) of page created with cat-attribute index template
$catTypes = $pages->find("template=cat-attribute-index")->children;
?>
However, you don't need to do this, you can just as easily get the children using the '/type/' path instead as this is a unique value (remember, paths and IDs are unique in processwire).
<?php
// get children (PageArray) from path '/types/'
$catTypes = $pages->get("/types/")->children;
?>
Next we need the page reference field which will reference the child pages under '/type/'. This will be used to select the type of cat when a page is created using the 'cat-entry' template.
Create a page reference field called 'cat-type'.
On the input tab, specify the input type as 'Radio buttons'.
Choose the parent of selectable pages as 'Type' and choose the template of selectable pages as 'cat-attribute'. You must select both of these in order to be able to create new pages from this field. Why you do this will become apparent later.
Allow new pages to be created from field? Yes, check this box.
Now you just need to set up the 'cat-entry' template. Add the new 'catType' field to this template, along with an image field which holds a single image.
Now you're all set and ready to create some content!