## General developer forum

### Best way to debug pluginfile errors in editor field of form

Best way to debug pluginfile errors in editor field of form

Hi

I'm struggling mightily with a file not found error for an image in my editor.  On the instance settings I have an editor field which saves images correctly.  When the view.php file is displayed I see:

<img src="http://192.168.1.120/moodle/pluginfile.php/53/mod_simplelesson/firstpage/1/faq.jpg" alt="" width="200" height="200" role="presentation" class="img-responsive atto_image_button_text-bottom">

Wonderful.

However, when I use a different page (edit_page.php) and file area (declared in get_file_areas), the image is in the editor but not when I move to show the page (showpage.php).  It looks like this:

<img src="http://192.168.1.120/moodle/pluginfile.php/53/mod_simplelesson/pages/1/moodle-partner-CSP-horizontal-small-trans-gray%20copy.png" alt="" width="462" height="83" role="presentation" class="img-responsive atto_image_button_text-bottom">

But there is a 404 not found error and no image is displayed.  I'm using pretty much the same process in both cases (as from https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#Simple_use).  Incidentally I don't see mod/glossary/comment.php (real world example given).

What is the most likely error here and/or what is the best way to track it down?

Is there perhaps a difference in processing or callbacks for a form that extends moodleform_mod and for one that just extends moodleform?

Average of ratings: -
Re: Best way to debug pluginfile errors in editor field of form

We already had similar discussion(s). As a result of one of those I created a minimal example of  a plugin that does what you are trying to do.

Average of ratings: Useful (1)
Re: Best way to debug pluginfile errors in editor field of form

Thank you for your quick response Darko, sorry if I'm missing the point - is the code you referenced a diagnostic tool for examining file areas?  It just seems to enable loading and displaying of an image.

I'm not generally having trouble getting pluginfiles to work in editor areas, the instance settings form has an additional editor area, similar to mod_page and that's fine.

It's just this one case where I'm using a Moodle form, which is not part of the instance settings, and the same techniques are working to the extent that they create the @@PLUGINFILE@@ code in the editor field in the database and the re-write plugin method produces what seems to be a valid URL, but the URL results in a 404 not found.  The file is also present in the user draftfile area as it reappears when the edit form is shown again.

Thanks again.
Richard

Average of ratings: -
Re: Best way to debug pluginfile errors in editor field of form

My first port of call whenever I have a problem serving saved files is to stick a breakpoint in pluginfile.php, then step through to see what is happening.

Of course, before you can do that, you need to set up xdebug on your server (usually just install the library + add a couple of lines to php.ini), configure your IDE to use xdebug (PHPStorm pretty much does this out of the box, other IDEs may need more or less configuration) then turn on debugging (xdebug helper for Chrome helps here). Time spent setting up a debugger will be paid back within a day or two.

Average of ratings: Useful (1)
Re: Best way to debug pluginfile errors in editor field of form

Thanks Davo

I have Eclipse (but usually just use it for browsing code).  I tried to set up xdebug, was a bit of a mission, but I suppose I will have to at some point.

Average of ratings: -
Re: Best way to debug pluginfile errors in editor field of form

I misunderstood your initial post thinking that you might need an example. Will take a look at the code in question and get back to you.

Average of ratings: -
Re: Best way to debug pluginfile errors in editor field of form

Much appreciated.

Average of ratings: -
Re: Best way to debug pluginfile errors in editor field of form

Since you have two file areas could it be that in one case you are storing file in the wrong file area? I suggest to check the actual file table and see what you have there.

There is nothing obviously wrong in your code so the error is something small . I would recommend you to pass the code through style checker since there are quite a few stylistic errors.

Given that you already use github writing a travis script could be the easiest way for this.

Average of ratings: Useful (1)
Re: Best way to debug pluginfile errors in editor field of form

Hi Darko

Richard

Average of ratings: -
Re: Best way to debug pluginfile errors in editor field of form

Excuse me if I am stating the obvious, but when writing code that stores files it is a very good idea to keep some kind of database query available, e.g. select * from mdl_files order by id desc.  I like to use something like mysql workbench of phpmyadmin to do that. It has helped massively when writing file writing code.

Average of ratings: Useful (2)
Re: Best way to debug pluginfile errors in editor field of form

Thanks for the suggestion Marcus.

Average of ratings: -
Re: Best way to debug pluginfile errors in editor field of form

Just reporting back (maybe others have the same issue):

The problem (tracked down by a combination of the helpful advice above) was the image itemid included in the path.  This needed to be the id of the page that had the image (when re-writing the pluginfile) and not the module id that I was previously using.

Average of ratings: -