Skip to content

Using tags made with addFunction(Smart) not usable when nested. #327

@dimitrihilverda

Description

@dimitrihilverda

I found this bug

When you create your own function with addFunction or addFunctionSmart and like to use the results in another fenom tag/function, i get errors

This is Fenom and PHP version and environment (server/fpm/cli etc) I am using

I use Fenom parser in MODx on php7.4

This is the function in my class

public function getHolderByVakgebied($vakgebied): ?string {
		if ($website_config_resource = $this->modx->getObject('modResource', $this->modx->getOption('website_config_id'))) {
			if (!empty($website_config_resource->getTvValue('vakgebied_' . $vakgebied))) {
				return $website_config_resource->getTvValue('vakgebied_' . $vakgebied);
			}

			if (!empty($website_config_resource->getTvValue('vakgebied_default'))) {
				return $website_config_resource->getTvValue('vakgebied_default');
			}
		}

		return 'No holder found for: ' . $vakgebied.' Config resource id: '. $this->modx->getOption('website_config_id');
	}

This is a plugin code snippet I use to add the tag

$fenom->addFunction("getHolderByVakgebied", [$modx->adwUbeeo, 'getHolderByVakgebied']);

Calling this as: {getHolderByVakgebied $_modx->resource.ubeeo_vakgebied} works fine. but using it like this:

{foreach json_decode(getHolderByVakgebied $_modx->resource.ubeeo_vakgebied, true) as $block index=$index}
does not work and gives errors, I also tryed to set it as a variable first like this:
{var $blocks = getHolderByVakgebied $_modx->resource.ubeeo_vakgebied}
same errors.
This function uses $this->modx->..., so I can not make it static...

The only way I got this to work on my tests is to allow php in tags and skip the whole addFuction part and call the function directly, but that is not so safe I think:
{foreach json_decode($modx->adwUbeeo->getHolderByVakgebied($_modx->resource.ubeeo_vakgebied), true) as $block index=$index}
but that works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions