Jump to content

Referencing field element from another page


Den S
 Share

Recommended Posts

Hello

 

Is there a ready way/module to select/reference an element of a field (i.e. particular element of a repeater) from another page?

For example I'd like to have a page with quotes from people, where each quote is an elements of repeater field on person's page.

Then on another page i'd like to be able to select/reference the quotes by selecting them them from a tree, like in the pagereference plugin, but having last element of the tree (leaves) presenting those quotes from the repeater field.

 

The reason i am looking to do it this way is because sometimes the quotes are longer then allowed title length, so it is cumbersome to make them as independent pages, as far as i understand.

 

Thank you.

Link to comment
Share on other sites

The tree selection is probably not possible without some serious coding. You can get a long way though by using what's already built into PW. I'd do it like this:

  • Create your repeater field (lets call it "quotes") with the field for the quote text (I'll call it "quote") and add it to a template
  • Create pages with that template and add a few quotes
  • Add a field of type "Page Reference" (let's call it "quotelink"), set it to "Single Page or Null Page"
  • On the Input tab for that field, select "Custom format (multiple fields) ..." in the "Label field" box, and in the "Custom page label format" input enter "{custlabel}" (we will fill that virtual property later)
  • Expand the "Custom PHP Code" option in the "Selectable pages" section, copy the code there to site/ready.php and adapt it to walk through the persons and add all quote repeater items to the output.
  • In that code, populate the "custlabel" property for each repeater item so the displayed text makes sense (e.g. by prefixing the first 80 characters of the quote with the person's name). Here's an example:
<?php

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
  if($event->object->hasField == 'quotes') {
  	$out = new PageArray();
  	$persons = $event->pages->find('template=person');
  	foreach($persons as $person) {
  		foreach($person->quotes as $quoteitem) {
  			$quoteitem->custlabel = $person->fullname . ": " . substr($quoteitem->quote, 0, 80);
  			$out->add($quoteitem);
  		}
  	}
    $event->return = $out;
  }
});
  • Add the quotelink field to a template
  • Now you should be able to add a page with that template and select an arbitrary quote in your quotelink field
  • In your template code, output $page->quotelink->quote

1091096894_2018-08-2319_25_14-EditPage_PageWithQuoteLinklocalhost.png.62e45863d3326a51b0e7324af6bea7fb.png

  • Like 4
  • Thanks 1
Link to comment
Share on other sites

Thank you very much! It is working very nicely.

Would you mind to also provide some pointers on how to maybe get the tree working? With large number of persons and several quotes per person this select will get very big very fast so finding correct element would be difficult.

I notice that custom PHP is disabled when Page List is selected. Is there are another way to hook to this field to supply custom-built page array?

In any case, thanks a lot!!!

Link to comment
Share on other sites

The Page List Select (InputfieldPageListSelect) is rather specific and can't deal with a flat list of pages, that's why the code option is disabled there. I don't think there's a way to extend it to do what you want.

To implement something similar, you'll need to write your own Inputfield implementation (and perhaps even extend InputfieldPage itself), that's what I meant above with "serious coding". You'd need to write both the PHP module code and the JS that does AJAX pagination and tree expansion, so it's not something done in an hour.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

I don't quite fully understand the scenario, but maybe you could use the inputfield selector. As a default selector you could point to the repeater parent (under Admin), and then go from there?

Or maybe try the Connect Page Fields module; perhaps it'll simplify the cross-referencing

https://modules.processwire.com/modules/connect-page-fields/

  • Thanks 1
Link to comment
Share on other sites

Dragan, thanks for response.

I am looking for a way to reference one element of a repeater from another pages. Almost the same as page reference does, but go one level deeper and reference not the whole page but a particular element of the repeater field on that page. Also I'd prefer to select the elements from a tree rather than from a select.

Not looking to do cross-referencing, just one-way link, so the cross-referencing plugin is not needed. Also, it should be able to reference repeaters from different pages (in my example—different people), so pointing to a single parent won't work either, if I understood you correctly.

@BitPoet's solution works quite nicely, with the exception of the selection bit. For now it will do and later I will try to change it. I don't quite understand yet why the code that is already there for the tree cannot be re-used for another purpose in the same way the select is. In the code of InputfieldPage I found a reference to deprecated method findPagesCode, so maybe i'll be able to leverage that.

 

 

Link to comment
Share on other sites

Thanks, Soma. I was not aware of that option, will look into that.

I used to have the same thing with autocomplete in drupal and while it is working better than plain select, still not as effective and accurate as the tree. With the tree long lines can be wrapped, this allows to see the difference in the long stings that may be different in the middle/end (i.e. when the same quote has been submitted in slightly different variations either due to an error or intentionally). But select is limited to one line. Not sure about autocomplete yet.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...