Replacement for get_categories() ?

Replacement for get_categories() ?

by Geoffrey Rowland -
Number of replies: 3
Picture of Plugin developers

Hi folks

I'm attempting to 'tidy up' some 3rd-party plugin code and getting a deprecated function warning for the get_categories() function.

I've found a Tracker Entry that the Deprecated function get_categories has no replacement, but I'm not quite sure how to interpret this. sad

https://tracker.moodle.org/browse/MDL-42323DL

Just need a 'drop in' replacement for the line:

$cats = get_categories();

Thanks for any clarification

Geoff



Average of ratings: -
In reply to Geoffrey Rowland

Re: Replacement for get_categories() ?

by Dan Poltawski -

Hi Geoff,

Did you read the phpdocs of the deprecated function? They were updated in MDL-42323 to outline the alternatives you can use.

From your description it sounds like coursecat::make_categories_list() is most appropriate:

 * coursecat::make_categories_list()
 * - returns an array of all categories id/names in the system.
 * Also only returns categories visible to current user and can additionally be
 * filetered by capability, see phpdocs to {@link coursecat::make_categories_list()}
In reply to Dan Poltawski

Re: Replacement for get_categories() ?

by Geoffrey Rowland -
Picture of Plugin developers

Hi Dan

Thanks for your speedy response. Yes, I did scan the phpdocs. It was specifically the following comment which left me wondering if there was a better alternative.

Also there is no substitute for retrieving the category with all it's subcategories. Plugin developers may re-use the code/queries from this function in their plugins if really necessary.

So, I guess I'll just do what it says and 'not lose any sleep' over continuing to use get_categories().

In reply to Geoffrey Rowland

Re: Replacement for get_categories() ?

by Rex Lorenzo -
For our own plugin we are just going to copy the existing function, but without the deprecated debugging message.