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

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