Jump to content

Displaying content of page in Page field type


mikeuk
 Share

Recommended Posts

I'm confused by how Processwire should display custom fields.

I have Home page set up with a field type Page (called 'blocks'), and that field has one page selected (called 'footer'), and that page contains 3 fields.

The idea is that this footer page is reusable as a block on any other page, while also being fully editable in the backend (and only needing to be edited once to change on all pages). This is as close to a Drupal blocks approach as I could get.

In principal, certainly from the backend, this is working fine. It's clear enough for a site editor.

The issue is how to display this 'blocks' field (the page attached to it and content of fields that page contains). This system only works if I can display that field, without needing to specify or hard code the page name within the field, or fields within that page (because this may change later). The one constant will be that the 'blocks' field is always linked to selected page templates.

Based on what I understand of Processwire logic, because I display the title with $page->title, I would have expected the following to work:

$page->blocks  

or 

$page->fields->get('blocks')  

This however only returns what I presume is the field ID.

I have looked through the docs, but they are light on highlighting typical uses (perhaps an example for each api element that shows how it works and why it exists in relation to backend data to frontend display, similar to the PHP manual. Something perhaps I can help with at a later date).

I also think this is a type of issue can be a deal-breaker for new users because of the logic gap ($page->title outputs field content, $page->blocks outputs what might be field ID), and us newcomers can be an impatient group!

So I hope solutions here will be a useful addition to the docs. After I learn how to deal with this, I'll add any info that might be useful for newcomers.

Link to comment
Share on other sites

4 minutes ago, adrian said:

You need to specify what fields you want to output from blocks, so for example, $page->blocks->title

 

Do you know of a way to do this without knowing in advance what fields blocks contains? This is important because I want the site editor / manager to have the ability to add / change the fields (pages) within 'blocks'

I'm thinking aloud now..... would it be a case of getting the array of the fields within blocks for the displayed page and then going through each array element to output? I didn't initially look at this area as I'm seeing what is possible before going into pure PHP territory.

Link to comment
Share on other sites

6 minutes ago, adrian said:

$page->blocks->render()

will output the content of the selected blocks page based on its template. Does that do what you need?

Yes! Thanks that's exactly what i need.

I missed render() in the docs. Just searched for that function and saw a page that i need to know more about (and may be useful to other newcomers): https://processwire.com/api/arrays/page/

 

  • Like 1
Link to comment
Share on other sites

One addtional question.

with $page->blocks->render() where blocks is a field type Page (and the page attached has three text fields), I'm getting the blocks rendered fine, but when logged in, underneath is the following error:

Error: Call to a member function render() on a non-object

In this case, does render() need a parameter?

 

 

Link to comment
Share on other sites

If the page field is returning an array (multiple vs single), then you either need to do: $page->blocks->first()->render() or you need to iterate through the selected blocks, eg:

foreach($page->blocks as $block) {
    echo $block->render();
}

 

Link to comment
Share on other sites

1 hour ago, adrian said:

If the page field is returning an array (multiple vs single), then you either need to do: $page->blocks->first()->render() or you need to iterate through the selected blocks, eg:


foreach($page->blocks as $block) {
    echo $block->render();
}

 

The foreach displays the same error but also without the display of the block (whereas before the block was rendered with the error after). 

With the first() example, I got a 'not callable in this context' error.

I tried all variations I can think of, including a looping again though $block (which displayed nothing). I'm guessing this is an issue with the field type Page. Perhaps there is an issue with the way the object is being built by PW?

I guess I need to change the whole approach here. It's frustrating because I could just at footer php file which calls the footer fields, and hard code (include) it into the template, but that kinds of defeats the purpose (I really want the site editor to be able to add, for example an area above or below the footer in the backend which relates to a change of frontend display.

This is where systems not getting in the way of how we work, get into the way of what we want to do........ Frustration aside, I guess what I really need is a blocks module. For now I'll just hard code this and later see if I can create a module to do this.

EDIT:  One thing I notice here is when outputting the whole object of $page->blocks, it is huge, over a minute to display it all. So I'm guessing that is the heart of the issue. After seeing that I'm guess field type Page is not ready to be used as I would have though an object that size must impact on site performance.

Interested in any thoughts on this. I'm guessing all PW objects are not so large.

 

Link to comment
Share on other sites

Not sure why that foreach version isn't working for you. Is "blocks" the name of your page reference field? Is Details > Page field value type set to multiple? Do you have a php template file for the template attached to the pages for each block?

 

 

Link to comment
Share on other sites

10 hours ago, adrian said:

Not sure why that foreach version isn't working for you. Is "blocks" the name of your page reference field? Is Details > Page field value type set to multiple? Do you have a php template file for the template attached to the pages for each block?

 

 

I will test this again later. I'll need to re-do it as I changed it to hard-coding it into the template just to get it done. "blocks" was the name of the field, there was a template attached, not sure what the value type was set to (but i guess it must have been multiple)

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...