- π©πͺGermany donquixote
The Reflection::getParameterClassName() needs to somehow deal with union types.
But what would be a suitable return value in case of union types?A possible backwards compatible solution could be:
- Let Reflection::getParameterClassName() return NULL, if the type is a union or intersection type.
- Optionally, add another parameter to control throwing an exception instead of returning NULL. OR introduce another method like "::demandParameterClassName()",
- Add a new method Reflection::getParameterClassNames() that also supports union types. This one would return NULL for intersection types.
- Alternatively, a new method could receive the type itself as a parameter, so it could also be used with return types.
- All the existing calling code should treat the NULL case as if there is no parameter type at all. Gradually, each piece of calling code could be updated to also support union types.---------
This said: For the example controller, I think it would be better to simply use "Node" or "NodeInterface" as the parameter type, and then within the method call instanceof to work with the more specific bundle classes.
As a workaround, you could also rename the parameter from `$node` to something like `$article_or_post` where you are sure that no entity type exists with this name. - π³π±Netherlands spadxiii
I ran into this with a rest controller myself where it had "string|int" as its parameter.
Added a small change to the if-statement in \Drupal\Component\Utility\Reflection::getParameterClassName:
method_exists($parameter->getType(), 'isBuiltin')
I'm not sure how to handle union types in this case, but this patch allowed me to prevent the error from happening :)