Jump to content

Displaying the Title when using Repeaters and Page Reference Fields


cstevensjr
 Share

Recommended Posts

Good Day.  I have created a repeater field on a website that I am building.  Three of the fields within the repeater uses page reference fields (I hope that's the correct terminology).   Everything works fine, except that I cannot get those three fields to display the Title and not the page IDs.  I've tried different syntax, however I cannot seem to get this to work.

post-756-0-80412400-1399517905_thumb.png

post-756-0-26702700-1399517924_thumb.png

post-756-0-15363800-1399517952_thumb.png

Implementing templates for repeater fields

While not commonly done, you can actually implement template files for your repeater fields, if you want to.

If you want to implement a template file for your repeater, you create a file in your /site/templates/ named "repeater_[field name].php", i.e. in the case of the buildings field, it would be "repeater_buildings.php". That template file will receive a $page variable that is represents one 'buildings' record. So you could do something like this:

/site/templates/repeater_buildings.php

  • echo "<h2>{$page->building_name}</h2>";
  • echo "<p>{$page->floors} floors, {$page->height} feet</p>";

The template used by your page with a 'buildings' field could then do this:

/site/template/basic-page.php

  • foreach($page->buildings as $building) {
  • echo $building->render();
  • }
  •  

I used a repeater template (repeater_sd_note.php file), as Ryan had mentioned.  This works great.

post-756-0-58092800-1399517977_thumb.png

post-756-0-91710200-1399518023_thumb.png

post-756-0-52649000-1399518005_thumb.png

I have included these screenshots, that will hopefully help someone to guide me in the right direction.

Screenshot 1 - A portion of what the web display looks like.

Screenshot 2 - A portion of what the backend display looks like of the Screenshot 1 web page.

Screenshot 3 - The sd_note repeater field contents.

Screenshot 4 - The template file used on the web page.

Screenshot 5 - A portion of the Zurb Foundation _main.php file that covers the rendering of the repeater.

Screenshot 6 - The contents of the repeater_sd_note.php file.

I have read quite a few forum articles and website documentation on repeaters, however I didn't find anything that I thought covered this subject. I apologize if a solution to this can be easily found.  I worked on this for two days already.  Thanks in advance.

Using ProcessWire 2.4.1

Edited by cstevensjr
Link to comment
Share on other sites

Hi, 

May be my little thought would help...

In repeater_sd_note.php - Please try to change with following statements:

echo "<p><strong>Type:</strong> $page->sd_type->title</p>";
echo "<p><strong>Status:</strong> $page->sd_status->title</p>";
echo "<p><strong>Priority:</strong> $page->sd_priority->title</p>";

Thanks

Link to comment
Share on other sites

Thank you for your response, however that was one of the first things I tried.  That normally is what is required with page IDs.  I'm sorry if I didn't state I had already tried the obvious syntax (i.e title, name, description).

  • Like 1
Link to comment
Share on other sites

Those fields are page fields with multiple pages you select. Thus the field returns a PageArray. So you simply foreach through them and echo out something from those selected pages.

$page->sd_status <- PageArray!

foreach($page->sd_status as $status){
      echo $status->title;
}
  • Like 1
Link to comment
Share on other sites

Thanks Soma.  This worked for two of the fields, which were multiple selects.

echo "<strong>Type:</strong>";
foreach($page->sd_type as $type){
      echo " | $type->title";
}
echo "</p>"

The third field is a single page select.

post-756-0-79218600-1399545233_thumb.png

I still can't get this one to work within the repeater.

Link to comment
Share on other sites

Sometimes I look at a problem and get consumed with a particular way of trying to solve the issue.  I had to step back and rethink about how I was going to solve the issue of the "Priority" field not displaying as I wanted.

When I rested, I actually thought "Hey, you already know what the answer is, why not work from that position?

Problem

I have a Priority field within a note form where the choices are "Not Applicable", "Urgent", "Important", "Routine" or "Low".  I chose to use the Page relation field.  The only problem I ended having is that I was dealing with a repeater to create the notes within the page.  Not normally a bad problem, but I couldn't get the singular page reference field to display it's content.

Solution

Since I already know what I wanted to display, I worked out a way to make it happen.   I modified the repeater_sd_note.php file to accomplish this.  The contents of the updated file is shown below.

<?php
echo "<h4>$page->title</h4>"; 
echo "<p><strong>Date:</strong> $page->sd_date</p>";
echo "<p><strong>Note:</strong> $page->sd_body</p>";
echo "<strong>Type:</strong>";
foreach($page->sd_type as $type){
      echo " | $type->title";
}
echo "</p>";
echo "<strong>Priority:</strong>";
$priority = $page->sd_priority;
$p = "1098";
if ( strcmp ( $priority, $p) == 0  ){
  echo " | Not Applicable |";
}
$q = "1099";
if ( strcmp ( $priority, $q) == 0  ){
  echo " | Urgent |";
}
$r = "1100";
if ( strcmp ( $priority, $r) == 0  ){
  echo " | Important |";
}
$s = "1101";
if ( strcmp ( $priority, $s) == 0  ){
  echo " | Routine |";
}
$t = "1102";
if ( strcmp ( $priority, $t) == 0  ){
  echo " | Low |";
}
echo "</p>";
echo "<strong>Status:</strong>";
foreach($page->sd_status as $status){
      echo " | $status->title";
}
echo "</p>";

echo "<p><strong>Attached Documents:</strong> [ $page->sd_documents ] </p>";
?>

I'm no coding wizard, however I knew if I could compare the known page IDs of the choices to value of the sd_priority variable I could do the rest easily.  It took awhile but I eventually thought about doing a string compare, which gave me logic needed to display the data on the website.

It's not elegant, but it works!  Thanks once again to Soma for setting me straight on the foreach on the array.  I'm still learning after all these years.  Thanks also to Valery for getting my mind working by providing me with a detailed start and tutorial on creating custom modules.  Good Day.

  • Like 1
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

×
×
  • Create New...