Jump to content

How to include data from another related field


Rob(AU)
 Share

Recommended Posts

Hi,
I am learning PHP while in the process of building a recipe website and enjoying the challenge.

One dilemma has stumped me. I have an Ingredients List page, which is parent to a series of ingredient pages. The ingredient pages has two fields, Title and ingredientType (for spices, dry, wet, vegetables etc). Thus there is one list of Ingredients available to be included in recipes. 

On the Recipe template there is an Ingredients Repeater field that includes fields for quantity and extra details (like room temperature for butter for example) as well as the ingredient field. If I include the ingredientType field in the repeater it shows as a blank field rather than showing the data saved from the ingredient page. It looks a little like this:

Ingredients List page
           ingredient page - title field
                                      - ingredientType  field

Recipe page
          title field
          description  field
           Ingredients repeater field
                    quantity field
                    ingredient field
                    ingredient details field

What I want to do is be able to include the ingredientType field in the array that the Recipes template uses to output the Recipe page. I will then use the ingredientType to sort the Ingredients Repeater data for output.

Conceptually this could either be at the adding a new recipe stage, or by somehow adding the ingredientType data to the array used for page output. 

I just can't work out how to do it. Any clues would be greatly appreciated.

Link to comment
Share on other sites

If I understand correctly, your "ingredient" field inside the repeater of a recipe is a Page reference field pointing to one "ingredient" page, correct? And the ingredientType is a field of the "ingredient" page?

In this case, you don't want to have the ingredientType in your repeater, because the ingredientType is already defined as a property in the ingredient page. You can access this property through the page reference:

// assuming the repeater field "ingredients"
foreach ($page->ingredients as $repeaterItem) {
	// assuming the page reference field "ingredient"
	$ingredientPage = $repeaterItem->ingredient;
	// "ingredientPage" is the page object referenced by the page reference field, so you can access it's fields		
	$ingredientTitle = $ingredientPage->title;
	$ingredientType = $ingredientPage->ingredientType;
}

You can than use the ingredient type to sort the ingredients, display them alongside the title or whatever you want to do.

Link to comment
Share on other sites

Thank you MoritzLost, you understood the structure correctly.

I see now how to access the ingredientType field for each of the ingredients included in the repeater. 

How do I include / append that data in the array from the repeater to then do the sort?

Link to comment
Share on other sites

The repeater field is an instance of RepeaterPageArray which inherits from WireArray, so you should be able to use WireArray::sort. This should work for sorting by the ingredientType:

$recipeIngredients = $page->ingredients;
$ingredientsSorted = $recipeIngredients->sort('ingredientType');

// if ingredientType is a page reference field itself, you may need to sort by it's title instead
$ingredientsSorted = $recipeIngredients->sort('ingredientType.title');

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...