Typografics Posted October 25, 2017 Share Posted October 25, 2017 Hello, I've been developing a general settings module for our websites. I'm storing the settings data in the module config data, but I can't seem to get the data working at the front-end of our website. I'm trying to call the data in my template like this: modules()->getConfig('TypoSettings'); Wich returns me an empty array. Am I doing something wrong? Or has it something to do with module permissions? Full module code: <?php namespace ProcessWire; /* * TypoSettings */ class TypoSettings extends Process implements Module, ConfigurableModule { public static function getModuleInfo() { return array( 'title' => "TypoSettings", 'version' => "0.0.1", 'summary' => "An awesome module for saving your general site settings.", 'author' => "Jonathan Claeys", 'href' => "", 'icon' => "cog", 'autoload' => false, 'singular' => true, 'requires' => "ProcessWire>=2.5", "permissions" => array( "setting-view" => "View the general sitewide settings.", "setting-edit" => "Edit the general sitewide settings." ), ); } public function init(){ modules()->get('JqueryWireTabs'); parent::init(); } public function ___execute(){ return $this->createForm($this->getDefaultData()); } static public function getDefaultData(){ return array( /* * General */ 'settings_general_tab' => array( 'type' => 'InputfieldTab', 'label' => __('General') ), 'settings_client' => array( 'type' => 'InputfieldText', 'label' => __('Client'), 'value' => 'Typografics', 'required' => 1 ), 'settings_email' => array( 'type' => 'InputfieldEmail', 'label' => __('E-mail'), 'value' => 'info@typografics.be', 'required' => 1 ), 'settings_telephone' => array( 'type' => 'InputfieldText', 'label' => __('Telephone'), 'value' => '', 'required' => 0 ), 'settings_fax' => array( 'type' => 'InputfieldText', 'label' => __('Fax'), 'value' => '', 'required' => 0 ), 'settings_general_tab_end' => array( 'type' => 'InputfieldTabEnd' ), /* * Location */ 'settings_location_tab' => array( 'type' => 'InputfieldTab', 'label' => __('Location') ), 'settings_address' => array( 'type' => 'InputfieldText', 'label' => __('Address'), 'value' => 'Zwaarveld 45', 'required' => 1 ), 'settings_postal_code' => array( 'type' => 'InputfieldText', 'label' => __('Postal code'), 'value' => '9220', 'required' => 1, 'columnWidth' => 33 ), 'settings_city' => array( 'type' => 'InputfieldText', 'label' => __('City'), 'value' => 'Hamme', 'required' => 1, 'columnWidth' => 67 ), 'settings_location_tab_end' => array( 'type' => 'InputfieldTabEnd' ), /* * Social media */ 'settings_social_tab' => array( 'type' => 'InputfieldTab', 'label' => __('Social media') ), 'settings_facebook' => array( 'type' => 'InputfieldText', 'label' => __('Facebook'), 'value' => '', 'required' => 0 ), 'settings_linkedin' => array( 'type' => 'InputfieldText', 'label' => __('LinkedIn'), 'value' => '', 'required' => 0 ), 'settings_google_plus' => array( 'type' => 'InputfieldText', 'label' => __('Google Plus'), 'value' => '', 'required' => 0 ), 'settings_twitter' => array( 'type' => 'InputfieldText', 'label' => __('Twitter'), 'value' => '', 'required' => 0 ), 'settings_pinterest' => array( 'type' => 'InputfieldText', 'label' => __('Pinterest'), 'value' => '', 'required' => 0 ), 'settings_instagram' => array( 'type' => 'InputfieldText', 'label' => __('Instagram'), 'value' => '', 'required' => 0 ), 'settings_social_tab_end' => array( 'type' => 'InputfieldTabEnd' ), ); } public function __construct(){ foreach (self::getDefaultData() as $key => $value) { $this->$key = $value; } } static public function getModuleConfigInputfields(array $data) { $defaults = self::getDefaultData(); $data = array_merge($defaults, $data); $fields = new InputfieldWrapper(); foreach($data as $key => $inputfield){ if (!isset($inputfield['type'])) { continue; } switch ($inputfield['type']){ case 'InputfieldTab': break; case 'InputfieldTabEnd': break; default: $field = modules()->get($inputfield['type']); $field->attr('id+name', $key); $field->label = $inputfield['label']; $field->required = $inputfield['required']; $field->value = $data[$key]['value']; $fields->append($field); } } return $fields; } public function createForm($inputfields){ /* * Create form */ $form = $this->modules->get('InputfieldForm'); $form->action = './'; $form->method = 'post'; $form->attr('id+name', 'TypoSettings'); /* * Add inputfields */ $data = modules()->getConfig('TypoSettings'); foreach($inputfields as $key => $inputfield){ switch ($inputfield['type']){ case 'InputfieldTab': $tab = new InputfieldWrapper(); $tab->attr("title", $inputfield['label']); break; case 'InputfieldTabEnd': $form->append($tab); unset($tab); break; default: $field = modules()->get($inputfield['type']); $field->attr('id+name', $key); $field->label = $inputfield['label']; $field->required = $inputfield['required']; if(isset($inputfield['columnWidth'])){ $field->columnWidth = $inputfield['columnWidth']; } $field->value = (isset($data[$key]) ? $data[$key] : $inputfield['value']); $tab->append($field); } } /* * Add submit */ $submit = modules()->get("InputfieldSubmit"); $submit->attr("value", __("Save")); $submit->attr("id+name","submit"); $form->append($submit); /* * Process form */ if(input()->post->submit){ $data = modules()->getConfig('TypoSettings'); foreach($this->getDefaultData() as $key => $inputfield){ if($inputfield['type'] != 'InputfieldTab' && $inputfield['type'] != 'InputfieldTabEnd'){ if($inputfield['required'] && empty(input()->post->{$key})){ page()->error(sprintf(__('%1$s is a required field. (%2$s field was not updated)'), $inputfield['label'], $inputfield['label'])); }else{ $data[$key] = input()->post->{$key}; } } } modules()->saveConfig('TypoSettings', $data); page()->message($this->_('Saved Settings')); session()->redirect(page()->url); }else{ $out = $form->render(); } return $out; } const modulePage = 'settings'; const permissionView = 'setting-view'; const permissionEdit = 'setting-edit'; public function ___install(){ $page = $this->pages->get('template=admin,name='.self::modulePage); if (!$page->id) { $page = new Page(); $page->template = $this->templates->get('admin'); $page->parent = $this->pages->get($this->config->adminRootPageID); $page->title = 'Settings'; $page->name = self::modulePage; $page->process = $this; $page->save(); } $permissionView = $this->permissions->get(self::permissionView); if (!$permissionView->id){ $permissionView = new Permission(); $permissionView->name = self::permissionView; $permissionView->title = $this->_('View the general sitewide settings.'); $permissionView->save(); } $permissionEdit = $this->permissions->get(self::permissionEdit); if (!$permissionEdit->id){ $permissionEdit = new Permission(); $permissionEdit->name = self::permissionEdit; $permissionEdit->title = $this->_('Edit the general sitewide settings.'); $permissionEdit->save(); } } public function ___uninstall() { $page = $this->pages->get('template=admin, name='.self::modulePage); if ($page->id) { $page->delete(); } $permissionView = $this->permissions->get(self::permissionView); if ($permissionView->id){ $permissionView->delete(); } $permissionEdit = $this->permissions->get(self::permissionEdit); if ($permissionEdit->id){ $permissionEdit->delete(); } } } Link to comment Share on other sites More sharing options...
kongondo Posted October 25, 2017 Share Posted October 25, 2017 Try $typoSettingsConfigs = $modules->getModuleConfigData('TypoSettings'); Edit: Oops..just noticed you are in PW3...so, not sure my suggestion would work. Edit 2: Hmm, it should work. OK, I don't know what's going on. I'm having one of those mornings...ignore me, please . Link to comment Share on other sites More sharing options...
Typografics Posted October 25, 2017 Author Share Posted October 25, 2017 It returns the same empty array. I think it might be something with the permissions. But I'm not seeing what is wrong. Link to comment Share on other sites More sharing options...
kongondo Posted October 25, 2017 Share Posted October 25, 2017 Just now, Typografics said: it might be something with the permissions. Maybe test for this then? Loosen perms and see if it works? Link to comment Share on other sites More sharing options...
Macrura Posted October 25, 2017 Share Posted October 25, 2017 ran into this problem with the General Settings module and my fork of the module consists of 2 separate modules, one process module for editing settings and one for storing the settings; because you can't access control the process module without preventing its data from being accessible to the front end. Settings Factory should work in this case, but i haven't tried required fields yet, and see that you have those implemented; (i should probably improve that part). I also haven't tried tabs yet... You could also just split your module into 2 separate modules... Link to comment Share on other sites More sharing options...
kongondo Posted October 25, 2017 Share Posted October 25, 2017 3 minutes ago, Macrura said: because you can't access control the process module without preventing its data from being accessible to the front end. Please clarify this. Were you not able to access configs data of a Process module? Link to comment Share on other sites More sharing options...
adrian Posted October 25, 2017 Share Posted October 25, 2017 Does this help? Looks like calling the module and using getDefaultData() on it works here: Link to comment Share on other sites More sharing options...
Macrura Posted October 25, 2017 Share Posted October 25, 2017 the scenario i encountered was this: - ProcessGeneralSettings original version did not permit non-superusers to access the settings page; in order to achieve that i added the permission and permissions properties to the module config 'permission' => 'general-settings', "permissions" => ["general-settings" => "Access general settings page"], ); then i gave my 'manager' role that permission. However once i did that, the settings were no longer available on the front end, (using the global variable that the module sets in wire), i assume i would have had to give the guest role that permission; so instead i just split the module into 2 and then could enforce access control on the process without preventing the settings from being accessed on the site. But if there is/was a better way that would be interesting to know about... the getDefaultData() as adrian posted... 1 Link to comment Share on other sites More sharing options...
adrian Posted October 25, 2017 Share Posted October 25, 2017 The other tool we have that might be useful is: permissionMethod 1 Link to comment Share on other sites More sharing options...
kongondo Posted October 25, 2017 Share Posted October 25, 2017 8 minutes ago, Macrura said: then i gave my 'manager' role that permission. However once i did that, the settings were no longer available on the front end, (using the global variable that the module sets in wire), i assume i would have had to give the guest role that permission; so instead i just split the module into 2 and then could enforce access control on the process without preventing the settings from being accessed on the site. Thanks for clarifying. I thought you were saying Process module's configs cannot be accessed in the frontend which confused me because I have been able to access a Process module's configs in the frontend as a non-superuser using getModuleConfigData(). , for instance, in Blog's demo template files. 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