Function in Plugin

I was updating some old plugins to new V3 recently and had some problems with calling functions within the plugin.

Typically we have a function like:

function functionName($items, $options = array()) {...
};

Which of course we can put in a plugin like:

Kirby::plugin('XXX/pluginName', []);
function functionName($items, $options = array()) {...
};

But I was thinking the function should be inside the ‘plugin’, like the idea below, but of course then the function cannot be found? I think I am being a bit dense here, what is the right way todo this?

Kirby::plugin('XXX/pluginName', [
  'functionName' => function ($items, $options = array()) {...
  };
]);

Any suggestion about the best approach to this would be much appreciated.

You cannot put normal functions within the plugin function, they need to be outside of this. The plugin function is only for registering Kirby extensions: Extensions | Kirby CMS

Thanks pixelijn, so this is the right way then? Say for example, you pass the function an array of maybe pages to work with?

Yes, but I’d consider if functions should be/could be implemented as Kirby extensions.

Thanks again, is there some sort of example on how to do this?

See the documentation linked above. Since we don’t know what your functions are doing, we can’t really tell if it makes sense to convert them into page/site/field/file etc. methods.

But there’s nothing wrong with keeping your functions if that works for you.

Thanks texnixe, there were a few different functions, so I did not give an example, apologies. But I think I have it now… so if I want the functions to further process a filtered list of child pages, for example, it should be in a page method?

Page collections would be pagesMethods not pageMethods.

Thanks texnixe, understood.