This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Say you have a model with an attached text file and you wish to edit that file instead of re-uploading it, you can use this little snippet to load it up without having to fiddle with config settings and things not saving and whatnot. There are probaly much better ways to go around this, but I haven't found any easy examples and this works for me :-) but I'm open for improvements.
For this you need a Model. In my case I named it ModelBlock and in it is an uploaded html file called contentfile.
In the controller for model block I addeded these two methods
// You request this by using editblock instead of update or create in your model url.
// http://mydomain.com/backend/authorname/pluginname/modelblock/editblock/:recordid
public function editblock($recordId=null) {
// Find the right model for this controller by id
$model = $this->formFindModelObject($recordId);
//Don't forget to use Backend\FormWidgets\CodeEditor;
$editor = new CodeEditor($this,new DummyOptions($model->contentfile));
// Load up the partial variables. Feel free to do this in a seperate function
$this->vars['editor'] = $editor->render();
// Any url you wish to redirect to after a cansel
$cancelurl = Backend::url('authorname/pluginname/modelblock');
$this->vars['recordId']=$recordId;
$this->vars['cancelurl']=$cancelurl;
// return the contents of bacend/authorname/pluginname/controllers/modelblock/_codeeditor.htm
return $this->makePartial('codeeditor');
}
// The saving function where it's written to disk.
public function onFileSave() {
// Retrieve current record id from the post. There may be easier methods to get hte current
// Model but documentation is lacking
$record = post('record');
$model = $this->formFindModelObject($record);
$cancelurl = Backend::url('authorname/pluginname/modelblock');
// The new file contents
$contents = post('editfile');
// Compute the path to the upload directory where the file is
$path = Config::get('filesystems.disks.local.root', storage_path().'/app'). '/'.$model->contentfile->getDiskPath();
file_put_contents($path,$contents);
Flash::success("File saved");
// If people wish to exit after saving allow them to
$closeAfterSave = post('close');
if($closeAfterSave) {
return Redirect::to($cancelurl);
}
}
In it's own file or just within the ModelClass controller file, whatever suits you make this class. There must be an easier way to do this/achieve this, but unfortunately I haven't found any good documentation for it yet. Anyone pointing me to a more "friendly" way to do this will be my hero.
// Dummy options to initialise the code editor. it needs these options at a minimum, but I have clue what it accepts totally or what it does with the values.
class DummyOptions{
public $fieldName = 'editfile';
public $valueFrom = 'editfile';
public $stretch=true;
public $size='giant';
public $file;
public function getName() {
return $this->fieldName;
}
// I know it uses this to load up the contents of the editor. it's something.
public function getValueFromData() {
return $this->file->getContents();
}
// Construct it with a file reference for our own convenience. You could also provide your own string if you wanted to,
// But this howto is about editing uploaded files in models.
public function __construct($file) {
$this->file = $file;
}
}
in your backend/authorname/pluginname/controlelrs/modelblock/_codeeditor.htm you can place the following html
<form class="form-elements" style="height:90%" role="form" data-request="onFileSave" data-request-data="record:<?= $recordId ?>">
<button class="btn btn-primary" data-hotkey="ctrl+s, cmd+s" type="submit" data-request="onFileSave">
<u>S</u>ave
</button>
<button class="btn" data-hotkey="ctrl+enter, cmd+enter" type="submit" data-request-data="close:1" data-request="onFileSave">
Save and Close
</button>
or <a href="$cancelurl">Cancel</a>
<?=$editor?>
<button class="btn btn-primary" data-hotkey="ctrl+s, cmd+s" type="submit" data-request="onFileSave">
<u>S</u>ave
</button>
<button class="btn" data-hotkey="ctrl+enter, cmd+enter" type="submit" data-request-data="close:1" data-request="onFileSave">
Save and Close
</button>
or <a href="$cancelurl">Cancel</a>
</form>
Last updated
Hello Tschallacka, I am interesting with your topic, Would you share your full code or your github repository ? Thanks
1-3 of 3