Jump to content

get field form another template


adinuno
 Share

Recommended Posts

I think I get what you are saying but not entirely. Data is not stored on a template basis but on a page basis. Yes, fields are associated with the templates in which they appear but ultimately, data stored is unique for each page using that template. By updating one template, do you mean update one page? You can also set some fields as Global (e..g the title field in the default install) but I don't know if that helps in this case. Using the API and hooks you can definitely update some fields automatically depending on some other fields. I believe I have seen such an example here in the forums...But, again, I am not sure I follow your question :D

  • Like 1
Link to comment
Share on other sites

I have a likely question, in a quite practical manner.

Say I have a list of names that includes a John Smith where every name has a page with some general information and one or more pictures. And I have a list of events that happened to all the people in my list of names, when they were born, when they got their drivers license, when they got married, etc...

After I added maybe 32 pages or more about John Smith I find out that his last name is not Smith but Smit. Instead of searching for all event pages relating to John Smith and changing his name to Smit it would be easy to just update the one page about John Smith in my list of names and have all other pages with stuff that happened to John Smith get his name from there and thus get updated automatically.

Link to comment
Share on other sites

What you are talking about is relational data, and you definitely would not want to draw these relations by searching for the person's name (after all, there might be two John Smith's too). You want to use the Page reference field type, which lets you reference one page from another. Create a field called "people", and choose "Page" as the field type. On the "input" tab, choose "person" as the template to use for pages (or whatever you've called your template for people), and asmSelect (or Checkboxes or Page Autocomplete) as the Input type. Save. Add your new field to your "event" template. Edit an event, and now you can select the people that are related to the event. If you later change the person's name or anything like that, then it doesn't matter. To list the people connected with an event, you'd just do this in your code:

foreach($page->people as $person) {
  echo "<p><a href='$person->url'>$person->title</a></p>"; 
}
Link to comment
Share on other sites

@kongondo My idea is based on a field's information basis. As if I change field A on a template, it should also change on field A in another template. Is this possible?

(sorry for the long delay, but for some unknown reason I wasn't notified of your contributions, even though I have e-mail notifications on :P)

Link to comment
Share on other sites

  • 2 weeks later...
@kongondo My idea is based on a field's information basis. As if I change field A on a template, it should also change on field A in another template. Is this possible?

It seems like you might be asking the same question that's already been answered? Either that or I don't understand the question. I'll step back and assume that maybe you aren't talking about page relations. A best practice would be to make sure you aren't duplicating any data. Meaning, pull the data from the source. For instance, if you wanted to retrieve the first image from the homepage, regardless of what page you were actually on in your site, then retrieve the homepage and pull the value from it. Same goes for anything else. Every page and its data is accessible to you from the API. 

$homepage = $pages->get('/');
$image = $homepage->images->first(); 
  • Like 1
Link to comment
Share on other sites

What you are talking about is relational data, and you definitely would not want to draw these relations by searching for the person's name (after all, there might be two John Smith's too). You want to use the Page reference field type, which lets you reference one page from another. Create a field called "people", and choose "Page" as the field type. On the "input" tab, choose "person" as the template to use for pages (or whatever you've called your template for people), and asmSelect (or Checkboxes or Page Autocomplete) as the Input type. Save. Add your new field to your "event" template. Edit an event, and now you can select the people that are related to the event. If you later change the person's name or anything like that, then it doesn't matter. To list the people connected with an event, you'd just do this in your code:

foreach($page->people as $person) {
  echo "<p><a href='$person->url'>$person->title</a></p>"; 
}

 Actually I want to use different fields from the 'people' template in a text field on the 'events' template. The 'people' template has beside 'firstname', 'lastname' also 'profession', 'alias',.... fields that are sometimes used and sometimes not. They will be used when 'event' text is 'Explorer David Livingstone meets explorer John Stanley' but not when '(victim of Albert DeSalvo) Mary Sullivan is strangled by (Serial Killer) Albert DeSalvo aka The Boston Strangler'.

Perhaps using a combination of the Page reference field and the Hanna Code module might do the trick? 'Event' in these examples should get field info from three different pages, two from the 'names' template (A meets B) and one from the 'places' template.

To make it even more complex, it would be ideal if 'profession' is called from a template 'professions'. It wouldn't be necessary to have duplicate fields for every explorer or serial killer in the 'people' pages, and easier to produce a list of all explorers or even all events involving explorers.

Ahum, just trying to get the most out of PW....

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