Jump to content

Connect Page Fields


Robin S

Recommended Posts

I just discovered the module today and it is very handy for my Cients <-> Contracts many_to_many relation. Now I can connect those from both ends. Works! Fantastic!

Thank you very much for this time-saver.

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

@Robin S

As far as I see the module doesn't update connected fields when pages with "connected" fields are cloned via ProcessPageClone. Do you think is it possible to handle this scenario? 

P.S. Obviously, in this scenario "owner" operator is more suitable.

  • Like 2
Link to comment
Share on other sites

  • 4 months later...

Hello

I get an error using Connect Page Fields with Repeater Matrix.

PW 3.0.123
RepeaterMatrix 0.0.4
Connect Page Fields 0.2.2

When I click for add a repeater matrix, the first time the loading wheel spins but at the end nothing happens. If I click again then a line appear. In the meanwhile on error log I got this.

Fatal Error: Uncaught Error: Call to a member function find() on null in /Applications/MAMP/htdocs/jesse/site/modules/ConnectPageFields/ConnectPageFields.module:144 Stack trace: #0 /Applications/MAMP/htdocs/jesse/site/modules/ConnectPageFields/ConnectPageFields.module(76): ProcessWire\ConnectPageFields->updateConnectedFields(Object(ProcessWire\RepeaterMatrixPage), Object(ProcessWire\Field)) #1 /Applications/MAMP/htdocs/jesse/wire/core/Wire.php(383): ProcessWire\ConnectPageFields->pageSave(Object(ProcessWire\HookEvent)) #2 /Applications/MAMP/htdocs/jesse/wire/core/WireHooks.php(825): ProcessWire\Wire->_callMethod('pageSave', Array) #3 /Applications/MAMP/htdocs/jesse/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\Pages), 'added', Array) #4 /Applications/MAMP/htdocs/jesse/wire/core/PagesEditor.php(746): ProcessWire\Wire->__call('added', Array) #5 /Applications/MAMP/htdocs/jesse/wire/core/PagesEditor.php(440): ProcessWire\PagesEditor->savePageFinish(Object(ProcessWire\RepeaterMatrixPage), true, Ar (line 144 of /Applications/MAMP/htdocs/jesse/site/modules/ConnectPageFields/ConnectPageFields.module)

  • Like 1
Link to comment
Share on other sites

  • 2 months later...
On 9/14/2017 at 12:30 AM, Robin S said:

If you already have some Page Reference values in place before you start using the module on those fields then you can get things set up with a one-off API operation:


// Get all pages where page_field_a is not empty
$pgs = $pages->find("page_field_a.count>0");
foreach($pgs as $p) {
    // For this page, get the pages selected in page_field_a
    $selected_pages = $p->page_field_a;
    foreach($selected_pages as $selected_page) {
        // Add this page to page_field_b on the selected page
        $selected_page->of(false);
        $selected_page->page_field_b->add($p);
        $selected_page->save();
    }
}

You can do a similar operation for page_field_b -> page_field_a if needed.

With similar, do you mean simply replacing field A and B? Or would I have to do some addtl. checks or queries?

In my case, I have much more existing items where B was populated, before field A was even created...

Link to comment
Share on other sites

On 4/17/2019 at 2:22 AM, dragan said:

Actually, I just tried out your code, but got this error:

Exception: Call to a member function add() on boolean on line: 13

Looks like that code needs a change to account for "single" Page Reference fields:

// Get all pages where page_field_a is not empty
$pgs = $pages->find("page_field_a.count>0");
// Get derefAsPage property of page_field_b to distinguish multiple/single page fields
$deref = $fields->get('page_field_b')->derefAsPage;
foreach($pgs as $p) {
    // For this page, get the pages selected in page_field_a
    $selected_pages = $p->page_field_a;
    // Standardise $selected_pages to PageArray in case of "single" field
    if($selected_pages instanceof Page) $selected_pages = $selected_pages->and();
    foreach($selected_pages as $selected_page) {
        // Add this page to page_field_b on the selected page
        $selected_page->of(false);
        if($deref) {
            $selected_page->page_field_b = $p;
        } else {
            $selected_page->page_field_b->add($p);
        }
        $selected_page->save();
    }
}

 

On 4/17/2019 at 2:12 AM, dragan said:

With similar, do you mean simply replacing field A and B? Or would I have to do some addtl. checks or queries?

In my case, I have much more existing items where B was populated, before field A was even created...

I don't fully understand what you're asking here, but in general you can switch "page_field_a" and "page_field_b" and run the code again if you need to. Of course make a DB backup before running any bulk API action in case something unexpected happens.

Edited by Robin S
Fix code error
  • Like 1
Link to comment
Share on other sites

On 4/17/2019 at 1:05 AM, Robin S said:

$selected_page->of(false);

Thanks a lot @Robin S. Guess I have to do some essential reading... I never had to use derefAsPage until now...

I tried your new code, but now I get a fatal error on the above line: Call to a member function of() on integer.

Guess there's something else in my setup that is not quite kosher - will go back to the drawing board tomorrow with a fresh set of eyes...

  • Like 1
Link to comment
Share on other sites

  • 6 months later...

Maybe I've been a bit silly, but I re-used some field names already connected with this module on another template. Originally I connected the member_lists field on the Member template with the list_members field on the List template and everything worked fine. However, I then added those fields to another template (Mailing) where there was no intention to connect the fields. The selectable pages for each of these fields restricts the template to the intended one in the connection (i.e. list_members is restricted to the Member template and member_lists is restricted to the List template) and the input process works fine. However, when I save a List page I get an error message like

Page 8287 is not valid for member_lists (Page 8287 does not match findPagesSelector: has_parent=/lists/, template=List|DynamicList, id=8287)

Page 8287 is a Mailing page, so it looks like Connect Page Fields is attempting to update a connection between list_members on the Mailing page and member_lists on the Member page and then complaining because Mailing is the wrong template for member_lists (it should be List). 

I guess I could fix this by creating some more fields and only using unique field in the connection, but I wonder if there is a better solution.

Link to comment
Share on other sites

@MarkE, this module connects two Page Reference fields across the site - the module config doesn't allow for limiting the connection by template. Nor would I want to add that because it would make the config more complicated than it needs to be for the majority of users.

I suppose it might be possible to add a hookable method to the module that could let you opt out of updating connected fields in certain circumstances, but I doubt that would be preferable to simply creating a different Page Reference field that isn't connected to other fields by this module.

  • Thanks 1
Link to comment
Share on other sites

Thanks for the quick reply @Robin S. I guess I was assuming that the connections would only be followed where permitted by the "Selectable Pages" constraints, but it is now clear that this is not the case. I can't envisage a use case where it would be desirable to follow connections which are outside the constraints, but maybe I lack imagination ?. Perhaps it could be made clearer in the documentation somehow that all connections will be followed regardless of any constraints on "Selectable Pages"?

Link to comment
Share on other sites

4 minutes ago, MarkE said:

I can't envisage a use case where it would be desirable to follow connections which are outside the constraints

Yes, but it's up to you to connect only fields that have the appropriate allowed pages for the templates they are added to. From the module readme:

Quote

Make sure you have set the "Selectable Pages" settings for each Page field correctly:

The settings for PageField A should allow pages using the template(s) that PageField B has been added to.
The settings for PageField B should allow pages using the template(s) that PageField A has been added to.

 

  • Thanks 1
Link to comment
Share on other sites

  • 1 month later...

Hey,

is there a way pages will be added at the top of the list?

When I add connected pages directly through the dropdown it works when altering the asmSelect js.
It should also add new pages to the top that has been assigned in the background.

Link to comment
Share on other sites

1 hour ago, sww said:

is there a way pages will be added at the top of the list?

Not really. Could you explain why you want to do this?

Rather than modifying asmSelect.js and needing to make changes to the Connect Page Fields module I think it might be better to adapt to how PW handles the sorting of newly added pages by default and reverse the sort order of the Page Reference field when you get it via the API.

Example:

2020-01-21_105021.png.7a17e368cd6275673c6e9d9a7d3c57a0.png

 

Link to comment
Share on other sites

9 hours ago, Robin S said:

Could you explain why you want to do this?

Because I want to display connected pages on my site by the order of that list … and make it easier for my client to re-order. new = on top = common sense.

Thanks for the hint. I was thinking the same this morning. I'll check.

Link to comment
Share on other sites

  • 4 weeks later...

I've ran into some trouble where the 'Connect Page Fields' module has been working fine but on some recent additions it's not adding the connection (so I have to remove, save, re-add, save).

Is there a way to run a query/hook to mass remove and re-add these, or force a re-check?

Link to comment
Share on other sites

  • 3 weeks later...
On 2/14/2020 at 9:37 PM, a-ok said:

I've ran into some trouble where the 'Connect Page Fields' module has been working fine but on some recent additions it's not adding the connection (so I have to remove, save, re-add, save).

Is there a way to run a query/hook to mass remove and re-add these, or force a re-check?

Just on this. I think it's due to the client duplicating a page (and thus not changing or 're-setting' the field)? Do you know if it's meant to work with duplicating the page?

  • Like 1
Link to comment
Share on other sites

v0.3.0 released. Notable changes in this version:

1. Fix for page clone issue reported by @a-ok in the post above.

2. The module now hooks Pages::save() and Pages::saveField() instead of Pages::saveReady() and Pages::saveFieldReady() in order to work around this core issue. Hopefully that issue gets resolved and then the module can go back to saveReady hooks because those would be preferable, but for now something had to be done because the issue was making page changes unsaveable when "single" Page Reference fields were configured in this module.

  • Like 4
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
  • Recently Browsing   0 members

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