Jump to content

Selectable pages: mutual relation


rooofl
 Share

Recommended Posts

I have a “related pages” field that list same template’s pages in a SelectMultiple input. This allow to create relations between pages of the same kind. But those relations are not mutual: I would like to get B related to A when I link A to B.

Is this possible?

Link to comment
Share on other sites

It is possible, this is the code you would use to get all pages that link to the current page from that field:

$pages->find("related_pages=$page")

You can even create a page field where you can select pages from all pages that link to the page you are editing. For this, just add this as the "custom php code to find selectable pages" on the "input field settings":

return $pages->find("pageField=$page"); // where pageField is the name of the selectable pages template

You should still define the parent or template of the selectable pages either on the code or on the other options from the field. <- forget the last part. The code overrides the previous options

  • Like 5
Link to comment
Share on other sites

By coincidence I'm working on a website that needs this feature (that's where the second tip from above came from).

I also had the need to limit the selectable elements to those that were not selected yet by other pages. This is what I came up with:

// this code goes on the "custom php" of the field settings
// this field is included in the template "team", and the goal is to choose players for that team
// players should be available (not selected yet by other teams)

$players = $pages->find("template=player");
$teams = $pages->find("template=team, players.count!=0")->remove($page); // important to remove this page

foreach($teams as $team) {
    foreach($team->players as $player) {
        $players->remove($player); // remove from our list the players that are in other teams
    }
}

return $players;
 

edit: added "players.count!=0" to the teams selector. It's not my case, but if there are lots of teams this filter could make some difference.

  • Like 2
Link to comment
Share on other sites

Thank you diogo, as I tried your method, I realized that it can control the display of the pages but it can’t make the pages selected without an intervertion of the user. Am I right?

The solution I am hoping to find would select the related pages within the full list of available pages.

As an idea for a field, would it be possible to have a two-way page reference field (as opposed to doing all the work in the API)?

It would be really handy for large number of projects!

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...

@rooofl, sorry, I didn't see your questions before. Yes, that's right, you can't have pages previously selected on a page field. What you want would require a module that hooks on page save and selects the page being saved on all the selected pages from the field on that page. Sounds confusing but it's quite simple.

$selectedPages = wire($page)->field;
foreach($selectedPages as $p){
  $thisPage = wire($page);
  $p->field->add($thisPage);
  $p->save();
}

Written on the browser and I don't have time to make it more complete. Just to give an idea.

  • Like 1
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...