cosmicsafari

Members
  • Content count

    28
  • Joined

  • Last visited

Community Reputation

4 Neutral

About cosmicsafari

  • Rank
    Jr. Member
  1. That works a treat, thanks man!
  2. Hi all, I have a module which creates numerous fields, pages etc. I would like to disable the ability to uninstall the module if any of the generated pages have children, as a mistaken uninstall could really mess up any website which is using it. Im curious about what is the best way to achieve this, currently I have some like: public function ___uninstall(){ error_log('---- UNINSTALL!! ----'); //check to see if any config pages have been added and abort uninstall if true //to avoid uninstalling and losing any work which has been done up to this point. if($this->canWeUninstall()) { //remove pages $this->deletePages($this->cPages->getPages()); //remove templates and field groups $this->deleteTemplates($this->cTemplates->getTemplates()); //remove repeaters $this->deleteRepeaters($this->cRepeaters->getRepeaters()); //delete fields $this->deleteFields($this->cFields->getFields(), $this->cRepeaters->getRepeaters()); } else { error_log('Uninstall aborted because config settings exist!'); $this->error('Whoops! Module cant be uninstalled as it appears to be in use.'); return; } } This works in the sense that it doesn't remove any of the generated content, which is good as it wont break the website. However it still shows the module as uninstalled, I can achieve what i'm looking for by replacing 'return' with 'die' but that results in a white page and looks rather rubbish so its not really a great solution. I would imagine there must be a better way to achieve this, any thoughts? Would it be possible to do this check via a hook before uninstallation and abort before we get to the point of running the uninstall() method, as I imagine that is what updates the DB to turn the module off so to speak?
  3. public function fieldGroupOverrides($name, $overrides){ foreach($overrides as $key => $field){ if (is_array($field)) { error_log('==> Override Field: ('.$key.')'); $t = wire('templates')->get($name); $fg = $t->fieldgroup; $f = $fg->getField($key, true); //All Fields, override label if(isset($field['label'])) { $f->label = $field['label']; error_log('===> Override Label Success! ('.$key.') -> ('.$field['label'].')'); } //Page Field, overide selectable pages if(isset($field['parent_of_selectable'])){ error_log('===> Override Parent of Selectable'); $parent = wire('pages')->get('name='.$field['parent_of_selectable']); $f->set('parent_id', $parent->id); error_log('===> Override Success! Parent of Selectable = ('.$field['parent_of_selectable'].')'); } wire('fields')->saveFieldgroupContext($f, $fg); } } }
  4. I tried using $f->set('parent_id', $parent->id); But I get a warning on the page This field needs to be configured before it can be used.
  5. This was what I ended up at and it seems to work ok even if it is a bit hacky. $sql = "SELECT data FROM fields WHERE id=:fieldid"; $query = wire('database')->prepare($sql); $query->bindValue(':fieldid', $f->id); $query->execute(); $data = $query->fetchAll(); $parts = json_decode($data[0][0],true); $parts['derefAsPage']='0'; $parts['collapsed'] = '0'; $parts['parent_id'] = $parent->id; $parts['labelFieldName'] = 'title'; $parts['inputfield'] = 'InputfieldSelect'; $data = json_encode($parts); $sql = "UPDATE fields SET data=:data WHERE id=:fieldid"; $query = wire('database')->prepare($sql); $query->bindValue(':data', $data); $query->bindValue(':fieldid', $f->id); $query->execute();
  6. So from messing around in the database it looks like the when you manually select a parent under the 'Selectable Pages' section and save, it adds "parent_id":X," to the 'data' cell for the field in questions record within the fields table. In context {"tags":"Page-Multi-Select","derefAsPage":0,"collapsed":0,"parent_id":1,"labelFieldName":"title","inputfield":"InputfieldSelect"} So in theory is there any reason why I can't just update the database directly without going through the API? Seems hacky but still can't find any examples of being able to do it through the API.
  7. Hi all, I am creating a page field (field of type FieldtypePage) via the API, however im still trying to find some documentation as to how I would go about setting the Selectable Pages for said field using the API. From what I have found it looks like it involves the use of, albeit this looks like a getter rather than a setter: $field->getInputfield($page) Which looks like it would make sense if I wanted to specify the selectable pages by a parent page, but what if I wanted to specify it by say a template?
  8. Hmm so I edited my code to test this an it appears that no would be the answer. It appears that you can't assign multiple as it will just overwrite the existing field group for that template.
  9. Im an idiot, turns out I had already figured this out previously somewhere else among the code for the same module! Anyway for anyone wondering I managed to retrieve them with: $fg = wire('fieldgroups')->get('field-group-name');
  10. Hi all, Just another quick question, how would I select a previously created field group so that I can add it to a template using the API? All the examples I have seen thus far are creating the field group and adding it to a template at that point, I would rather create all my field groups beforehand and then assign them to their templates in batch.
  11. Hi all, Just a quick question before I jump in and inevitably break the module I am working on. Is it possible to assign multiple field groups to a single template via the api? I am creating a whole bunch of fields, templates and pages on the fly at install and being able to assign multiple field groups to a template would be quite advantageous.
  12. Magic, thanks!
  13. Hi all, I am working on a module which creates templates, fields & pages upon install. I have ran into an issue where I have a numerous fields which I would like to override their labels depending on the template they are used in. Is it possible to do this dynamically via API at the point where I am creating the field group and template using the API. The following is the code which I am using at the moment. foreach($templatesArray as $name => $cTemplate) { if(array_key_exists('fieldGroup',$cTemplate)) { $fg = new Fieldgroup(); $fg->name = $name . '-fg'; foreach ($cTemplate['fieldGroup'] as $field) { //possibly at this point? $fg->add($this->fields->get($field)); } $fg->save(); } $t = new Template(); $t->name = $name; $t->fieldgroup = $fg; $t->noChildren = 1; $t->setIcon($cTemplate['icon']); $t->save(); } Is this even possible?
  14. Nice, thanks man!
  15. That would go some way as to why I couldn't find anything then I suppose lol I just want the page to appear at the bottom of the list under Pages > Home