da² Posted April 18 Share Posted April 18 Hi, I post here before to report an issue, just in case I'm missing something (using PW 3.0.229). To create instances of a custom page class, I'm adding a static method on it: class IndividualCarChoicePage extends Page { public static function create(UserPage $user, ChampionshipPage $championship): void { $carChoiceParent = wire()->pages->get("parent_id=$championship->id, template=individual-car-choices"); $carChoicePage = new IndividualCarChoicePage(); $carChoicePage->of(false); $carChoicePage->template = "individual-car-choice"; $carChoicePage->parent = $carChoiceParent; $carChoicePage->title = $user->displayUsername; DevUtils::prettyPrintObject($carChoicePage); exit(); } } But the template is not defined: ProcessWire\IndividualCarChoicePage Object ( [id] => 0 [name] => [parent] => /assetto-corsa-competizione/championnats-acc/championship/choix-de-voiture/ [template] => [title] => MyName [data] => Array ( [title] => MyName ) ) And then the admin is corrupted: The blue page is the parent page of the newly created, you see the arrow on left indicating it has a child, but this child is not displayed. And there's not way of solving this issue, I can not delete the parent page "Choix de voiture", the trash refuses to delete it! Now, if I move this code to another class, that is not extending Page and not located in /site/classes/ directory: IT WORKS! The new page is created with its template. Why? Link to comment Share on other sites More sharing options...
BrendonKoz Posted April 18 Share Posted April 18 I'm responding with no experience using custom page classes, so if my reply is just silly, I apologize. ? Perhaps I'm missing something, but I think my question here is also the question that ProcessWire is throwing an error about. Where do your ProcesswireTemplate::INDIVIDUAL_CAR_CHOICES and ProcesswireTemplate::INDIVIDUAL_CAR_CHOICE variables' values come from? In fact, where is the ProcessWireTemplate class located? There's a Template class in wire/core, but I'm not seeing a Processwire* class anywhere. If those classes/values don't exist, that could cause the errors to be thrown. If you're moving the code to another class, maybe it's not actually being called/used and ProcessWire is falling back to using the standard Page class? Link to comment Share on other sites More sharing options...
da² Posted April 19 Author Share Posted April 19 Hello @BrendonKoz, On the code above I didn't copy the namespace and use statements but this is not the issue, instead of using ProcesswireTemplate I can directly use strings but this is not working either. EDIT: I've edited my code to remove this ambiguity, using strings instead of class ProcesswireTemplate. 1 Link to comment Share on other sites More sharing options...
BrendonKoz Posted April 22 Share Posted April 22 Are you calling your create() method from the constructor? Link to comment Share on other sites More sharing options...
da² Posted April 26 Author Share Posted April 26 @BrendonKozNo, from a template if I remember good. The method create() is not complete in my post, the page is saved at the end and I have these strange bugs in admin, so the code executes well, and the same code in another class (not the custom page class) is working fine. Looks like a bug in Pw, maybe something related with PHP namespaces and template retrieval. 1 Link to comment Share on other sites More sharing options...
poljpocket Posted April 28 Share Posted April 28 Just off the top of my head, and completely without testing: From my knowledge of the PW internals, shouldn't you wire the newly created page? call the save() or pages->save() function? Link to comment Share on other sites More sharing options...
da² Posted May 21 Author Share Posted May 21 @poljpocket The same code works if called from another class. The above code is incomplete to create a page, and just illustrates that template is empty when called from static method in the custom page class. If I fully create the instance, I end with that strong bug in admin. Link to comment Share on other sites More sharing options...
Jonathan Lahijani Posted June 6 Share Posted June 6 @da² Did you figure this out? Link to comment Share on other sites More sharing options...
Jonathan Lahijani Posted June 10 Share Posted June 10 I've created a GitHub issue here: https://github.com/processwire/processwire-issues/issues/1938 1 Link to comment Share on other sites More sharing options...
bernhard Posted June 10 Share Posted June 10 On 4/18/2024 at 3:39 PM, da² said: $carChoicePage->template = "individual-car-choice"; I can't remember exactly but Ryan mentioned some time ago that in some situations it's not enough to set the template as string. ProcessWire usually converts that into a template object, but in the wrong context this does not work. When calling the method statically this seems to be the case. So just like wire()->pages->get() works for the parent you can also use wire()->templates->get(...) for the template instead of a plain template name. When using a function call like @Jonathan Lahijani does in the issue the context is different again and makes the code work. 2 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