Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not access ID of a FieldtypeOptions option #28

Closed
hiboudev opened this issue Jul 12, 2023 · 3 comments
Closed

Can not access ID of a FieldtypeOptions option #28

hiboudev opened this issue Jul 12, 2023 · 3 comments

Comments

@hiboudev
Copy link
Contributor

I'm using the default PW "Select Options" component and when trying to get ID of the option from a Twig, it throws an error:

Fatal Error: Uncaught ArgumentCountError: Too few arguments to function SelectableOptionArray::hasID(), 0 passed in E:\path\vendor\twig\twig\src\Extension\CoreExtension.php on line 1635 and exactly 1 expected in E:\path\wire\modules\Fieldtype\FieldtypeOptions\SelectableOptionArray.php:262

#0 E:\path\vendor\twig\twig\src\Extension\CoreExtension.php(1635): SelectableOptionArray->hasID()
#1 E:\path\site\assets\cache\TemplateEngineTwig_compile\72\7271faee4ef038b8f49fabbd4afa01b2.php(137): twig_get_attribute(Object(Twig\Environment), Object(Twig\Source), Object(SelectableOptionArray), 'id', Array, 'any', false, false, false, 53)

Code:
{{ user.membershipStatus.id }}

The same code in PHP works fine:
$user->membershipStatus->id

@wanze
Copy link
Owner

wanze commented Jul 31, 2023

Twig has some issues calling dynamic properties using PHP's __get which is used quite a lot in ProcessWire. Try accessing it explicitly, something like {{ user.get('membershipStatus').id }} or even {{ user.get('membershipStatus').get('id') }}. Does that help?

@hiboudev
Copy link
Contributor Author

hiboudev commented Oct 10, 2023

Hello @wanze

I faced the same kind of issue today.

At first I thought is was a PW error, so I reported this issue.

I have a Profields Table containing a Text field. If the field is empty I get an error from Twig, if the field is filled it works.
{{ penalty.reason }}

I tried to follow your instructions and it works:
{{ penalty.get('reason') }}

This is strange, because it seems it doesn't happen with an Integer field. It looks like we need to use get() only in some cases, depending on the field type and if it has content. I'm afraid to get some random errors in the future if I don't use get() everywhere.
I don't know if there's something you can do on your side to make this more consistent, or if it's outside your range in Twig core code?

@wanze
Copy link
Owner

wanze commented Jan 20, 2024

Hi @hiboudev
As far as I know, there is no way to configure Twig to handle magic getters differently. So I think the best strategy is to always access values via the WireData::get() method if your object extends WireData.

@wanze wanze closed this as completed Jan 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants