Jump to content

How I can to link these two fields?


blad
 Share

Recommended Posts

I have two page fields (PageAutocomplete) and I want to link them ...

Templates:

- Clients (Parent)

    -client (Childrens)

         -Field "Client_properties" (Page autocomplete select template=Property)

- Properties (Parent)

    - Property (Childrens)

         -Field "Properties_client" (Page autocomplete select template=Client)

What should I do?

Link to comment
Share on other sites

Hi Blad,

I have in the past, created two-way relationships like this however, have you considered just having it one-way and using the API to find the reverse?

As an example

<?php

// get all clients properties
// assumes a $client variable

$properties = $client->properties;

// get all clients who have access to a property
// assumes a $property variable

$clients = $pages->find("template=client, properties=$property");

?>

It depends on whether you really need it to show both ways in the admin, or you just want to be able to display both directions somewhere on the frontend.

If you really do need it to show both ways in the admin, you can create a simple module that creates the reverse relationship when you update a certain page.

  • Like 2
Link to comment
Share on other sites

What Andre said. :)

However, If it is possible to populate one autocomplete field with the pages from another—I feel like it shouldn't allow those pages as values, especially since it looks like your autocompletes are populated based on template—it would seem an odd behavior. What happens if you then remove that page that was set from the other field? Would you expect it to get removed from the original autocomplete? Starts to get complicated quickly.

Perhaps I'm reading too much into what you are after. :)

  • Like 2
Link to comment
Share on other sites

Super quick (and completely untested) idea.

Maybe you could just add a markup field that shows the related pages?

This would happen after the form is built, so no need to have additional fields assigned to the template.

Again, haven't tested this, and wrote it mostly in the browser.

:)

<?php

class relatedPages extends WireData implements Module {
    
    public static function getModuleInfo() {
        return array(
            'title' => 'Related Pages', 
            'version' => 1, 
            'summary' => 'untested module to show related pages',
            'href' => '',
            'singular' => true, 
            'autoload' => true, 
            );
    }

    public function init() {
        $this->addHookAfter("ProcessPageEdit::buildForm", $this, "showRelatedPages");
    }
     
    public function showRelatedPages($event){
        
        // page being edited (viewed)
        $page = $event->object->getPage();

        // check if it's a template we want to do anything with.
        if($page->template == "client" || $page->template == "properties"){

            // form
            $form = $event->return;

            // build selector to find related pages.
            // you may want to find the related pages another way, this is just a quick and dirty example.
            if ($page->template == "client") $selector = "your selector here";
            if ($page->template == "properties") $selector = "some other selector";

            // find pages based on the selector above
            $related_pages = wire("pages")->find($selector);

            // our find returned some related pages
            if ($related_pages->count() > 0){

                // get the markup module
                $field = $this->modules->InputfieldMarkup;
                $field->label = "Related Pages"; // label
                $field->markupText = $related_pages->implode(", ", "title"); 

                // where do you want to insert this in the admin (after what field?)
                $form->insertAfter($field, $form->get("field_name_to_insert_after"));
            }  
        }
    }
}
  • Like 3
Link to comment
Share on other sites

Need to show when a page being edited the pages that have that page in the Page field. Page page page. :) I will try references tab.

Sorry, I can´t answer any more because I reserve my 100 post for something special.

Thanks! Page, page, page

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