Forumindlæg af Urs Hunkler

Billede af Core developers

You are completely right Tim - what I did was nonsense. I tried to get started by reading the docs and looking at some code. Both lead to not understanding the inner workings and to writing strange code that seamed to work after a lot of trail and error. Thanks for your offer to dig deeper into my issue. 

I took the time to better understand Moodle file handling and file backup/restore. The result is easy as to declare the »filearea« where the image gets stored in the »get_fileareas« function in the backup and restore task classes of the block and in the functions dealing with the image. Moodle handles the file backup/restore then automatically.

I hope I got the image handling right this time.

Billede af Core developers

I found a way to solve the task.

In the »after_restore« function I read the block instance from the database and then call the  »file_save_draft_area_files« function with the same parameters as in the block's »instance_config_save« function where the image reference is created. The only difference is that after the restore the new context id from the restored block is used.

Billede af Core developers

Thank you very much for your fast answer Tim. That would have been an easy one blinker

I tried to use the backup/restore functions from the »HTML« block but without success. I changed the block name and the referenced field name in the code. The situation may be different because in the »HTML« block image references in a text are manipulated during restore. I only have the image reference number for the »files« table entry saved in the block configdata.

Maybe some post processing may be needed? Or a new image reference in the »files« table needs to be added with the changed context id?

Billede af Core developers

A block displays an image. The block image is set with the filemanager.

How do I backup and restore the image with the course backup. I have searched the Moodle documentation for block backup, for the file API, the backup API etc. but found no description how to backup and restore images/files in blocks. I didn't find Moodle core blocks that handle file/image backup and restore and I didn't find other blocks that backup and restore images.

When I backup and restore a course with this image block no image is shown after the restore. When I open the block settings the filemanager shows the image which has been chosen before the course has been backuped and restored. When I save this image again it is shown again. The »itemid« of the image is saved in the block »configdata« and restored with the course restore. Is there a way to create a new file entry for this image? There should be a possibility to write the image information to the database during the block restore or after restore. 

Please can somebody explain how to backup and restore an image in a block? 

Thank you very much for your help.

Gennemsnitsbedømmelse: -