Problems using File API with local plugin

Problems using File API with local plugin

by Kathrin Osswald -
Number of replies: 3

Hi there,

my task is to write a local plugin with which a user should be able to upload icons that will be added to the HTML header for homescreen webapp purposes. The most important functionality to be implemented is to get the url of the picture (saved in the db), so that the browser can download it and use it as the web app icon.

Unfortunately I'm pretty new to the Moodle Dev world and so I have some troubles with realizing this task.


I tell you, what I did so far:

I made a settings page and used the function admin_setting_configstoredfile to get the upload window. Thats working fine. When I drag a picture to this box and save, the picture is also saved to the database in the table mdl_files.

And now the challenge begin.

I found a lot of search results for functions in combination with the configstoredfile-element, but only in connection to theme programming, what is not suitable for the local plugin.

Then I had a look at the File API and there it says following:

Note: In most cases, when developing third party plugins, pluginfile.php looks for a callback function in the appropriate plugin. These functions are stored in lib.php files and are named component_name_pluginfile()

So I added the function 

function local_mobileheadertags_pluginfile($course, context $context,$filearea,$args,$forcedownload) { }

to my plugin's lib.php

Unfortunately this function is not called. I added some debug messages within this function but they are never called.

I think that this function will only work for those components mentioned here. Local plugins aren't mentioned.


So at the moment I'm totally stuck because I simply have no idea how to get to the images url, where it can be downloaded.


I hope some of you experienced guys can give me a hint!


Best regards,

Kathrin


Average of ratings: -
In reply to Kathrin Osswald

Re: Problems using File API with local plugin

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

pluginfile.php definitely works for local plugins.

Did you use moodle_url::make_pluginfile_url() to generate the URL to send to the browser?

If so, was the 'component' set to 'local_mobileheadertags' - if not, then Moodle won't know which plugin to call back to in order to send the file.

Average of ratings: Useful (1)
In reply to Davo Smith

Re: Problems using File API with local plugin

by Kathrin Osswald -

Hi Davo,

thanks for your help. I managed it to get the URL now.

Unfortunately when I open the url in the browser I get an empty image. I have to do some more resarch on this topic to get it working as intended.

Best regards,

Kathrin

In reply to Kathrin Osswald

Re: Problems using File API with local plugin

by Kathrin Osswald -

I just wanted to tell, that I managed it to get the files. Thanks for your help!