I’ve built a module that creates a new file from tagdata between a tag pair, before creating the new file if a similar file already exists it moves it to an archive folder, it then writes the path to the new file back to the template, this is the part that’s causing problems; the new file gets created, and the existing file gets archived, however if I try to return or echo the new file path back to the template it causes the new file to also get copied to the archive folder.

Template tags:

    Some data.....

Module code:

public function test()
    $page_data = ee()->settings_model->get_settings();

    //fetch all files from folder
    $directory = $page_data['folder_path'];
    $pattern = "custom.*txt";
    $files = ee()->file_model->folder_iterator($directory, $pattern);

    //move existing to achive folder

    //create new file and get link to it
    $name = ee()->file_model->create_new_file();

    $link_text = "<link href=\"$name\" rel=\"stylesheet\">;

    // this works as expected

    //this causes more than one new file to be created
    return $link_text;

So it seems that writing the new file path back to the template perhaps causes the module tag to be parsed again (I can't think of another explanation).

Is this what’s happening? How do I stop it?

  • Don't have time to recreate a test setup right now, but unclear whether you are saying that it is the addition of $link_text to the return line that causes the problem, or simply the inclusion of the return line with or without a return variable. Wondering what happens if you return $name rather than $link_text. Also - would be good form to include in your question the version EE you are working with. – JCOGS Design Apr 12 at 16:16
  • Thanks for commenting, if I add a link to the return it causes problems, an empty return or return true, works as expected, but i need to send a link to the page – wot Apr 12 at 16:21
  • Just a thought - what happens if you send back the $name value and build the http tag within your template? – JCOGS Design Apr 13 at 8:25
  • It works as expected then – wot Apr 13 at 10:57
  • 1
    Weird. I'll see if I can find time at weekend to simulate on a local system - if possible can you provide some more details of what kinds of files are in the folder that you are working on - if file type is an issue useful if I get the right ones. Also what version of EE / client software / browser are you using? – JCOGS Design Apr 14 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.