Jump to content
blad

How I can to link these two fields?

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?

Share this post


Link to post
Share on other sites

What do you mean link them? Do you want to have the value selected in one show in the other and vice versa? That isn't possible.

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Thank you. I'll try to make a module that relates these two fields. I'll post here when I get it.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Although I do see the value in being able to "see" the reference two-ways in the admin. Might be nice for a backend user to see it from either angle.

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

wow!! Thanks @renobird i will try it, love you man

  • Like 3

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
      Thanks
    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.

      Step1:

      Step 2: Select the images with the trashcan symbol

       
      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
      /EyeDentify
    • By EyeDentify
      I have been experimenting with the new $page->meta() method and find it useful.

      Once i figured out that the data i "save" with it is tied to the page where i called the method from.

      So this is not obvious at least not for me in the documentation:
      https://processwire.com/api/ref/page/meta/
       
      So i just wanted to share that revelation with the community so you don´t get as confused as i was.

      Happy Coding Everyone.
    • By stanoliver
      In the new page-meta-method (https://processwire.com/blog/posts/pw-3.0.133/#comments) there is an example how to ouput "world". 
      Could may someone give me a snippet of code so that the output is one of the colors (red, green, blue).
    • By VeiJari
      Hello forum. I'm trying to figure out how to detect if current form has errors after saving it in pw admin page. This is so that our event location isn't added in database if we detect errors. But I can't seem to find a correct way to detect errors? I only found the wire()->errors() and it always triggers even if I don't have any errors. 
      How can I detect errors in a form?
      By error I mean $page->error('this is the red error');
×
×
  • Create New...