Marc H. Posted July 17, 2019 Share Posted July 17, 2019 Hi Folks, Unfortunatly my first post will be a asking for help :) I hope anyone can help me with their knowledge. The Repeater Fieldtype does have an api call like $page->repeater_field->getNew() My question is, does something similar exist for the ProFields Module "Repeater Matrix"? For further clarification on what im trying to accomplish: I have a Template with a lot of Fields and one of them is a Repeater Matrix. I use this Matrix to give the Creator the ability to have custom content and can change the order as he likes. But the new Page should already have some Items of this Matrix when the Page gets created. I cant create static fields for those, because the Creator should be able to place Content above it, for example. So my itention was to add specific Items to the Matrix when a Page will be created, via: $pages->addHookAfter('Pages::added', function (HookEvent $event){ // SOME CODE } but i can't figure out how to add Items to a Repeater Matrix. I would be pleased if anyone of you have any advise. If you need some more clarification, i wont hasitate to provide anything you'll need. Thanks in advance, Marc H. Link to comment Share on other sites More sharing options...
thetuningspoon Posted July 17, 2019 Share Posted July 17, 2019 Repeater Matrix extends the regular Repeater, so it supports all of the same methods as the repeater field, including getNew(). I am not sure how you would set the type for the new item, however. Link to comment Share on other sites More sharing options...
Marc H. Posted July 17, 2019 Author Share Posted July 17, 2019 I have crawled the Web and the Forum for solutions, and i might have found some other issues im running into. Im trying to add Repeater Matrix Items via a hook in the init.php, it might be an issue, because if i try to call the function RepeaterMatrixPageArray::getNew() it throws an Uncaught Error: Call to a member function getNew() on null It might be, that i have to try to add the Hook at a different place. Thank you for your reply :) Link to comment Share on other sites More sharing options...
elabx Posted July 17, 2019 Share Posted July 17, 2019 20 minutes ago, thetuningspoon said: Repeater Matrix extends the regular Repeater, so it supports all of the same methods as the repeater field, including getNew(). I am not sure how you would set the type for the new item, however. When creating the page you have to set the number in the field "repeater_matrix_type". $repeater_field->repeater_matrix_type = 2 Now the problem is how to get the numbers, if you inspect the repeater matrix module configuration ($modules->getConfig('FieldtypeRepeaterMatrix')), you could get the info from there. Basically every repeater matrix type has a number. You can also get this info by inspecting the html from a repeater matrix field, you will see that the fields have a number prepended. 15 minutes ago, Marc H. said: Uncaught Error: Call to a member function getNew() on null That sounds like you are referencing the page in the wrong way, if you can paste all the code of your hook we could help a little bit more, the page being saved should be accessed like this: $page = $event->arguments(0); Link to comment Share on other sites More sharing options...
Marc H. Posted July 18, 2019 Author Share Posted July 18, 2019 @elabx Thank you for you reply, I figured out that RepeaterMatrixPageArray extends the RepeaterPageArray, so according to this, i should be able to call getNew(). If i can call getNew(), there shouldnt be any problem on adding new Items. My Code: site/ready.php <?php namespace ProcessWire; if(!defined("PROCESSWIRE")) die(); // For Debugging im using the ::saveReady Hook, to not have to create a new page // $pages->addHookBefore('Pages::added', function (HookEvent $event){ $pages->addHookBefore('Pages::saveReady', function (HookEvent $event){ // Fetch Pagedata $data = $event->arguments(0); // Fetch Page Object $page = $this->pages->get($data->id); // Fetch RepeaterMatrix Types $repeaterMatrix = $this->fields->get('product_matrix'); $matrixTypes = $repeaterMatrix->type->getMatrixTypes($repeaterMatrix); // Debuggin Messages $this->message($page->product_matrix->getNew()); // Error-Line }); // Throws following error: // ncaught Error: Call to a member function getNew() on null in site/ready.php:19 Link to comment Share on other sites More sharing options...
Marc H. Posted July 18, 2019 Author Share Posted July 18, 2019 Thanks for everyone helping me out here. I found a solution, but if anyone could explain why this works all of a sudden, i would be very grateful. The Code i posted above, all i need to do, was a conditional check if the RepeaterMatrix isnt NULL, then the getNew() call worked: site/ready.php <?php namespace ProcessWire; if(!defined("PROCESSWIRE")) die(); $pages->addHookBefore('Pages::added', function (HookEvent $event){ // Fetch Page Data $data = $event->arguments(0); // Fetch Page Object $page = $this->pages->get($data->id); // Check if the RepeaterMatrix is not NULL if($page->product_matrix != null) { // Save the new RepeaterMatrixPage im going to add, in a Variable $newItem = $page->product_matrix->getNew(); } // Safetycheck if i got anything if($newItem == null) return; // Set RepeaterMatrixPage Type, so the Correct Type of Item gets created and the Data gets saved in the Correct Fields // The Field Types are in the Order of their creation in the RepeaterMatrix Field, and they beginn counting at 1 $newItem->repeater_matrix_type = 2; $newItem->product_properties_title = "Zusätzliche Informationen"; // Save the Item, as it is a Page $newItem->save(); // Add new Children to the RepeaterMatrixPageArray $page->product_matrix->add($newItem); // Save newly created Page again, so the RepeaterMatrix Items are saved in the List. $page->save(); }); I dont know why this works all of a sudden, but im glad it does. kind regards Marc H. 2 Link to comment Share on other sites More sharing options...
elabx Posted July 18, 2019 Share Posted July 18, 2019 4 hours ago, Marc H. said: I found a solution, but if anyone could explain why this works all of a sudden, i would be very grateful. I THINK, in your saveReady code you are trying to get a page that is not yet saved, so that's why you get a Null reference. 5 hours ago, Marc H. said: // Fetch Pagedata $data = $event->arguments(0); // Fetch Page Object $page = $this->pages->get($data->id); This two lines are king of redundant, because argument 0 will always be a Page object, but saveReady is happening like the hooks says, just before saving, so when you try to find a page with $pages it doesn't find it even if you already have a Page with an id. Link to comment Share on other sites More sharing options...
Marc H. Posted July 18, 2019 Author Share Posted July 18, 2019 I tried it without the redundant fetch of the page, it still throws an error of Null reference. Besides your explanation, which sounds reasonable, the following line does return a valid Page object: $page = $this->pages->get($data->id); Even if the Page isnt saved, it might be, because it isnt an ::saveReady Hook anymore, instead i changed it to ::added Hook. I still need to check the RepeaterMatrix in my Page to not be Null. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now