markhuot / craft-pest

https://craft-pest.com
Other
38 stars 11 forks source link

Fixed error when using Category factory #64

Closed nfourtythree closed 1 year ago

nfourtythree commented 1 year ago

Currently, when trying to use or extend a category factory there is an error on create().

This is due to the code calling fields() on the element. Within this it is trying to get the field layout. Due to it being a category the field layout comes from the category group.

Therefore at this point in the code with the groupId not already having been set an error occurs.

It, therefore, seems harmless to try and set this early. I wonder if there are potentially other attributes that might raise similar issues that really could do with being set earlier in the process.

markhuot commented 1 year ago

Interesting, I like this a lot. I think I might abstract it out though because an entry's section/type could use a similar early set, right?

The other alternative is instead of calling $element->fields() we could call a factory method that handles this for us specific to factories… Not sure of your thoughts?

nfourtythree commented 1 year ago

Yea interesting the thought of calling a factory method.

I still think that calling $element->fields() is probably the right thing to do as it does a lot of the legwork without reinventing anything. It just seems that there might need to be some attributes that are deemed a "priority" for getting set early in the process.

nfourtythree commented 1 year ago

I'm actually wondering how Entry works as-is since it would technically need to know the entry type to get the ->fields() in a similar way that categories needs to.

I think it is because there is a try-catch in Entry and the error is caught and null returned. So realistically this is a bug with the Category element in Craft.

markhuot commented 1 year ago

Ah, that makes sense!