Jump to content

Can not set template to new page created from CustomPage::staticMethod(). Corrupted admin.


Recommended Posts

Posted

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:

image.thumb.png.8c92364e254e4c234ffa053f6ac6fe14.png

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!

image.thumb.png.e64a798fdd99ff0cdc586d6c361dad09.png

 

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?

Posted

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?

Posted

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.

  • Like 1
Posted

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

  • Like 1
Posted

Just off the top of my head, and completely without testing: From my knowledge of the PW internals, shouldn't you

  1. wire the newly created page?
  2. call the save() or pages->save() function?

 

  • 4 weeks later...
Posted

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

  • 3 weeks later...
Posted
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.

  • Like 2

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