fenom icon indicating copy to clipboard operation
fenom copied to clipboard

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

Open dimitrihilverda opened this issue 3 years ago • 0 comments

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.

dimitrihilverda avatar Oct 07 '21 07:10 dimitrihilverda