bernhard Posted April 14, 2015 Share Posted April 14, 2015 hi, i want to populate a textarea on render based on the content of a file that is stored in a hidden file field my template looks like this: title ____________ _____________ | | code |___________| codefile code.inc here is my module code: public function init() { $this->addHookBefore('Inputfield::render', $this, 'render'); // populate textarea field from file content $this->pages->addHookAfter('saveReady', $this, 'createCodeFile'); // create code file from textarea field } public function createCodeFile($event) { $page = $event->arguments('page'); if($page->template == 'code') { // create file if textarea is not empty if($page->code) { $filename = "tmpupload/code" . $page->id . "_" . $page->name . ".inc"; // create temp file file_put_contents($filename, $page->code); // remove old files $page->codefile->removeAll(); // ad new file to hidden field $page->codefile->add($filename); // delete temp file unlink($filename); } } } public function render(HookEvent $event) { $field = $event->object; if($field->name == "code") { $field->value = "test"; // works, but what i want is something like this: $field->value = file_get_contents($page->codefile); } } how can i access the page object from the page that contains the field that im hooking? or should i do that completely different? help would be very appreciated as i've been searching quite for a while... Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 14, 2015 Share Posted April 14, 2015 The inputfield never knows where it's used. $this->addHookAfter('ProcessPageEdit::buildForm', $this, 'populateField'); public function populateField($event){ $page = $event->object->getPage(); $form = $event->return; } Link to comment Share on other sites More sharing options...
adrian Posted April 14, 2015 Share Posted April 14, 2015 I haven't looked through all your code properly, but the obvious issue first is the way you are doing file_get_contents. You need to point it to the path of the file. I think this should work: file_get_contents($page->codefile->filename); If $field->value = "test"; is working, then I think this is the only change you should need. I am curious though why you are converting a textarea field to a file and then on render getting the contents back out of that file. I am sure there is a good reason - just seems weird at first glance Link to comment Share on other sites More sharing options...
bernhard Posted April 14, 2015 Author Share Posted April 14, 2015 thank you guys! this works: public function init() { $this->addHookBefore('ProcessPageEdit::buildForm', $this, 'populateField'); // populate textarea field from file content $this->pages->addHookAfter('saveReady', $this, 'createCodeFile'); // create code file from textarea field } public function createCodeFile($event) { $page = $event->arguments('page'); if($page->template == 'code') { // create file if textarea is not empty if($page->code) { $filename = "tmpupload/code" . $page->id . "_" . $page->name . ".inc"; file_put_contents($filename, $page->code); $page->codefile->removeAll(); $page->codefile->add($filename); unlink($filename); } } } public function populateField($event) { $page = $event->object->getPage(); if($page->template == "code") { if($page->codefile) { $page->code = file_get_contents($page->codefile->first()->filename); } } } @adrian I am curious though why you are converting a textarea field to a file and then on render getting the contents back out of that file. I am sure there is a good reason - just seems weird at first glance you are absolutely right on that - i wanted to explain it but was in a hurry i'm working on an update of geowire. you can devide the map javascript into some logical parts (pw pages) and reuse parts that you need more often. for example you can create a layer like this: var opencyclemap = new OpenLayers.Layer.OSM("OpenCycleMap", ["http://a.tile.opencyclemap.org/cycle/${z}/${x}/${y}.png", "http://b.tile.opencyclemap.org/cycle/${z}/${x}/${y}.png", "http://c.tile.opencyclemap.org/cycle/${z}/${x}/${y}.png"] ); map.addLayer(opencyclemap); this works great using the new ACE editor field! on the other hand, if you have complicated functions/controls with lots of code ACE is not ideal, so i thought it would be great to have the opportunity to edit those files via your favourite editor without having to copy/paste between ACE and your IDE. therefore i need to update the textarea field if the codefile has changed in the meantime! outsourcing the code to a file also has the benefit that i can execute php code like this: var opencyclemap = new OpenLayers.Layer.OSM(<?php echo $page->title ?>, [...] ); but there is one more little problem: when i save the page, the file gets created - filename code1010_test.inc when i save the page again, the old file gets removed, the new file gets created, but the name is code1010_test-1.inc when i save it again, the filename is code1010_test.inc again it seems that the original filename is still blocked although i did a codefile->removeAll() any help for this? Link to comment Share on other sites More sharing options...
adrian Posted April 14, 2015 Share Posted April 14, 2015 when i save the page, the file gets created - filename code1010_test.inc when i save the page again, the old file gets removed, the new file gets created, but the name is code1010_test-1.inc when i save it again, the filename is code1010_test.inc again Have you tried the "Overwrite existing files" option in the field's Input tab? Link to comment Share on other sites More sharing options...
bernhard Posted April 15, 2015 Author Share Posted April 15, 2015 thank you for that suggestion - unfortunately that has no effect. ha - often asking for help brings up the solution: saving the field before adding the new file does the trick $page->codefile->removeAll(); $page->save('codefile'); $page->codefile->add($filename); 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now