Fetch content from children > structured field > related pages

I have two foreach loops where I am trying to fetch the content from related pages where the URI for the related page is stored in a structured field. However, I try I keep getting the same error. My code is below and the error:

“Call to a member function displayname() on boolean”

<?php foreach($pages->children() as $work): ?>
  
<ul>
  
<?php foreach($work->object_related_constituent()->toStructure() as $constituent): ?>
  
<?php   

  $uri = 'constituents/'.$constituent->constitute_id();
  echo '<li>'.page($uri)->displayname().'</li>';
  
?>

<?php endforeach ?>

</ul>

<?php endforeach ?>

Any assistance gratefully received :slight_smile:

Unfortunately, the information you provide is not sufficient to really help you. The error points to the fact that the page does not seem to exist. Could you please post the blueprint of the page that contains the structure field and what exactly is saved in the text files of these pages. Thank you.

Hi @texnixe and many thanks for your swift reply.

There isn’t a blueprint to share in this case as the pages are generated from a script but below is an example of a child.

Object_ID: 43447

----

Reference: LEEAG.1949.0009.0086

----

Alternate_numbers:

-
  type: Other number
  number: 9.86/49
-
  type: Previous Number
  number:  LEEAG.PA.1949.0009.0086
-
  type: Negative Number
  number:  862/20 (29)
-
  type: Kitson Number
  number:  KITSON.093.G.03

----

Object_related_constituent: 

-
  constitute_id: constituents/6953
  x_rank: 1
  x_role: Artist
  x_prefix: 
  x_suffix: 
-
  constitute_id: constituents/31886
  x_rank: 2
  x_role: Associated Person
  x_prefix: 
  x_suffix: 
-
  constitute_id: constituents/7173
  x_rank: 3
  x_role: Associated Person
  x_prefix: 
  x_suffix: 

----

Acquisition_related_constituent: 

-
  constitute_id: 6951
  x_rank: 1
  x_role: Bequeathor
  x_prefix: Bequeathed by
  x_suffix: 

----

Title: A calm harbour scene; Called 'Yarmouth Harbour', but possibly a study after a picture.

----

Begin_date: 1830

----

End_date: 1830

----

Date_label: c. 1830

----

Object Name: drawing

----

Medium_materials: graphite on wove paper

----

Support: Smooth, cream, wove paper

----

Dimensions: 

-
  rank: 2
  description: Mount
  height: 197
  h_unit: mm
  width: 281
  w_unit: mm
  depth: 0
  d_unit: mm
-
  rank:  1
  description: Sight Size
  height: 106
  h_unit: mm
  width: 172
  w_unit: mm
  depth: 0
  d_unit: mm

----

Description: A calm harbour scene, with a rowing boat in the foreground left of centre, flanked by fishing boats receding into the distance.

----

Inscriptions: 

----

Provenance: One of perhaps two thousand pencil drawings mounted by Cotman on large sheets sometime after 1820 and by descent to the 'Highly important sale by auction of a rare and valuable collection of works of art, by the following artists: John Sell Cotman, Miles Edmund Cotman, Varley, Sillett, Hobart, and Teniers, Pritchett, Hannah, S D Colkett, Nursey, and others..' at Murrell's Bazaar salerooms, Norwich, 26-27 November 1862;

G.A.King, stained-glass artist of Norwich, to;

W. Boswell & Son, Norwich, where bought September 1926 by S. D. Kitson (1871 - 1937);



----

Credit_line: Bequeathed by Sydney Decimus Kitson, 1949

----

Exhibition_history: 

----

Published_references: Sydney Decimus Kitson, �Pencil Sketches by J.S.Cotman�, unpublished manuscript notebook (Leeds Art Gallery: The Kitson Archive, 1926) cat.G.03;

Sydney Decimus Kitson, �The Catalogue of the Collection of Drawings by J. S. Cotman�, unpublished typescript (Leeds Art Gallery: The Kitson Archive, 1924-1937) cat.93;

�Leeds City Art Galleries: Concise Catalogue�, (Leeds: Leeds City Council, 1976) cat. 9.86/49;






----

Related_objects: Shipping ?The Pool of London', Leeds Art Gallery, 1949.0009.0085;

----

Latitude: 

----

Longitude: 

----

Comentary: A calm harbour scene, with a rowing boat in the foreground left of centre, flanked by fishing boats receding into the distance.
Kitson identified the subject as Yarmouth Harbour sometime between making his initial notes in the 1926 notebook and compiling the typescript of his collection c.1937. It is not, however, self-evident what might be the identifying landmarks. The composition is strongly reminiscent of Willem van de Velde or Aelbert Cuyp and it may be that this is a study after a picture.
The confident, loose manner seems dateable to the late 20s or early 30s. The Leeds collection contains quite a number of drawings on a similar kind of smooth, cream, wove paper.
David Hill, April 2015

----

Kitsons_notes: Sydney Decimus Kitson, �The Catalogue of the Collection of Drawings by J. S. Cotman�, unpublished typescript (Leeds Art Gallery: The Kitson Archive, 1924-1937) cat.93;
YARMOUTH HARBOUR.
4�" x 7"
Pencil, on thin buff paper.
Norwich Collection. Sheet G3.
Bought from Boswell. Sept: 1926.

----

Subjects: 

----

Collection: Leeds Art Gallery 

And below is an example of a related page

Title: Cotman, John Sell, British, 1782 - 1842

----  

constituentID: 6953

----

DisplayName: John Sell Cotman

----

AlphaSort: Cotman, John Sell

----

Honorific: 

----

FirstName: John

----

MiddleName: Sell

----

LastName: Cotman

----

Suffix: 

----

DisplayBio: British, 1782 - 1842

----

BeginDate: 1782

----

EndDate: 1842

----

Nationality: British

----

Institution: King's College London

----

Biography: Artist John Sell Cotman was born in Norfolk in 1782. At the age of 16 he left his hometown for London, where he became apprentice to book publisher Rudolf Ackerman, leaving just a year later to work for the patron Dr Thomas Munro. 

He published his first series of etchings 'Miscellaneous Etchings and Architectural Antiquities of Yorkshire' in 1811 based on three tours of the county between 1803 and 1805. It was on these tours that he visited wealthy patrons, Mr and Mrs Cholmeley of Brandsby Hall, near York and stayed with the Morritts of Rokeby.

A leading member of the Norwich school of Landscape painting and prominent member of Thomas Girtin's Sketching Club, Cotman was appointed Drawing Master at Kings College London in 1834 holding the position until his death in 1842

I have since revised my script a little as updated the data in the children text files to simplyfly further. See below for updated script.

<?php foreach($pages->children() as $work): ?>

<ul>

  <?php foreach($work->object_related_constituent()->toStructure() as $constituent): ?>

  <?php   

  
  echo '<li>'.page($constituent->constitute_id())->displayname().'</li>';
  
?>

  <?php endforeach ?>

</ul>

<?php endforeach ?>

Your constitute_id already includes the parent page, but you add it again in your $uri variable.

I revised my script a little as updated the data in the children text files to simplyfly further. See below for updated script.

<?php foreach($pages->children() as $work): ?>

<ul>

  <?php foreach($work->object_related_constituent()->toStructure() as $constituent): ?>

  <?php   

  
  echo '<li>'.page($constituent->constitute_id())->displayname().'</li>';
  
?>

  <?php endforeach ?>

</ul>

<?php endforeach ?>

Error persists :thumbsdown:

I cannot think what is going on as all the pages exist.

Then let’s see what you get if you dump the page instead of calling the display name:

<?php foreach($pages->children() as $work): ?>

<ul>

  <?php foreach($work->object_related_constituent()->toStructure() as $constituent): ?>

  <?php   

  dump(page($constituent->constitute_id()));
  //echo '<li>'.page($constituent->constitute_id())->displayname().'</li>';
  
?>

  <?php endforeach ?>

</ul>

<?php endforeach ?>

And if that does not lead anywhere, please post the folder structure of your site.

That seems to work. See here:

https://cotmania.org/works-of-art

There are around 900 works of art so takes a few seconds to load.

For belt and braces, folder structure as requested:

domain/works-of-art/children
domain/constituents/related pages

Well, yes, you do get a lot of pages, but first you get a whole bunch of empty list tags. So maybe the error is in the $pages->children(), maybe you mean $page->children() or page('works-of-art')->children()?

In any case, I suggest you always test if the page exists before calling a method on it (that is a general best practice for all kinds of object methods):

<?php foreach($page->children() as $work): ?>

<ul>

  <?php foreach($work->object_related_constituent()->toStructure() as $constituent): ?>

    <?php if($p = page($constituent->constitute_id())): ?>
     <li><?= $p->displayname() ?></li>
    <?php endif ?>

  <?php endforeach ?>

</ul>

<?php endforeach ?>

That way, you are on the safe side if the page does not exist.

Side note: Don’t rely on what you see in the browser, always check the resulting HTML in dev tools.

Many thanks @texnixe but despite all your efforts we’re not much further :disappointed:

Error now reads: syntax error, unexpected ‘:’ on line 7 which makes no sense to me.

Oops, mea culpa, one of my favorites, missing parenthesis (in the if statement), corrected above :pray:

Amazing! Cannot thank you enough @texnixe :smiley:

Truly, many, many thanks

1 Like