How to use file_rewrite_pluginfile_urls function in a local plugin?

How to use file_rewrite_pluginfile_urls function in a local plugin?

by Sameera K -
Number of replies: 2

Hello smile


I created a local plugin called local_xxx and this has a moodle editor to add content to the user. I could successfully save images user's add to the text editor using following methods.


$entry = file_prepare_standard_editor($entry, 'description', $definitionoptions, $context, 'local_xxx', 'entry');

#After getting data
$data->id = $DB->insert_record('table_name', $data);
$entry = file_postupdate_standard_editor($data, 'description', $definitionoptions, $context, 'local_xxx', 'entry', $data->id);
$DB->update_record('table_name', $entry);

But when I tried to use file_rewrite_pluginfile_urls to show the content, it doesn't show the image properly.

$description = file_rewrite_pluginfile_urls($data->description, 'pluginfile.php', $context->id, 'local_xxx', 'entry', $postid);

But uploaded  image is existing in filedir. How can I make this work?


Thanks you

Average of ratings: -
In reply to Sameera K

Re: How to use file_rewrite_pluginfile_urls function in a local plugin?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Have you created a MYPLUGIN_pluginfile() function in lib.php in order to validate the request and serve the file back to the end user? As described in the Moodle docshttps://docs.moodle.org/dev/File_API#Serving_files_to_users


In reply to Davo Smith

Re: How to use file_rewrite_pluginfile_urls function in a local plugin?

by Sameera K -

Thanks Davo, I didn't knew about that. Now its working perfectly.