Jump to content
cstevensjr

Displaying the Title when using Repeaters and Page Reference Fields

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By wesp
      Hello everyone,
      Is there a way to access the key number of an item in a page reference field and then output it as a value?
      So for example I want to automatically output the number 1 if the referenced page is the first in the list and have it be updated when the list is reordered via drag and drop.
      Thanks in advance!
    • By Manuel
      Hi everybody!
      I'm new to PW and just did the beginner-tutorial “Hello Worlds”, a beginning ProcessWire tutorial --> Link
      After extending the template from the original tutorial with a page_reference-Field (Page-Field value type is configured as single page),
      PW throws a fatal error when i try to output multiple pages:
      $planets = $pages->find("template=planet, sort=-title"); foreach($planets as $planet) { echo $planet->title; echo $planet->planet_reference->title; } Should return three Pages, but throws the following error:
      When i try to output the field with a single page, everything just works fine:
      $planet = $pages->get(1018); echo $planet->title; echo $planet->planet_reference->title; I've tried to solve the problem with myself (and google), but i can't fix the error.
      Thanks a lot!

      My enviroment:
      OSX Mojave / MAMP (PHP 7.2), PW v 3.0.123
    • By ttttim
      Hi,
      I'm working on a website where in the back-end in one template i have multiple page reference fields using the same conditions (parent and template). Is there a way if i selected a page in field A it won't show up anymore in the list of field B or field C etc?
      I was looking at creating a custom string but i couldn't wrap my head around it.
    • By dragan
      Is it somehow possible to limit the number of pages one can select with the page reference field?
      I have a case where I need to have exactly three, not more, not less. Is this doable with a hook? Or would you suggest another approach? (three single pageref fields and make each mandatory)
    • By verdeandrea
      Hi guys,
      I'm getting crazy trying to figure out what is the cause behind a weird problem is giving me some bad moments with my client.
      I have a repeater field, with some fields as childre. From one day to the other the children fields of the repeater change and i find other fields as children. Also, all the content my client inserted in that repeaters are gone, cause now we have the new empty children fields. I really don't understand what can be the reasons behind this.
      This happened more than once.
      It' s like there is a repeating action that changes the repeater's children. The only repeating action I can think of is the chronjob for the database. Could this depend on that? Have you any idea or suggestion of what should i check or ho to solve this weird problem?
      Thank you very much!
×
×
  • Create New...