Jump to content
Robin S

Connect Page Fields

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

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites
15 hours ago, Zeka said:

As far as I see the module doesn't update connected fields when pages with "connected" fields are cloned via ProcessPageClone.

I've added support for this in v0.2.0.

  • Like 4

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
9 hours ago, Develon said:

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

Thanks, I could reproduce that. Should be fixed in v0.2.3.

  • Like 1

Share this post


Link to post
Share on other sites
11 hours ago, Robin S said:

Thanks, I could reproduce that. Should be fixed in v0.2.3.

Thanks to you.

Now it works, great job!

  • Like 1

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

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

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

line 13 is 

$selected_page->page_field_b->add($p);

o_O

  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)
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

Share this post


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

Share this post


Link to post
Share on other sites
9 hours ago, dragan said:

I tried your new code, but now I get a fatal error

Sorry, there was a silly error in the code. I've updated it so please try again.

Share this post


Link to post
Share on other sites

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.

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...