Jump to content

Recommended Posts

So I'm trying to create a module. And I would like to store the general settings that the module holds. 
I made a page with the admin template and my own process module. Can I store my data on that same page with the module/admin template?
Or is that not possible?

I tried to add them to the page like:
$this->page->$key = $value

I also tried to add them to the page data like:
$this->page->data($key, $value)

But after redirecting the keys & values are gone.

Kind regards,
Jonathan

Share this post


Link to post
Share on other sites
<?php namespace ProcessWire;

class ProcessCustomSettings extends Process {

  public static function getModuleinfo() {
    return [
      'title' => 'Processwire Custom Settings',
      'summary' => 'Create your own custom settings.',
      'author' => 'Jonathan Claeys',
      'version' => '1.0.1',
      'page' => [
        'name' => 'settings',
        'title' => 'Settings',
      ],
    ];
  }

  public function ___execute() {
    var_dump($this->page->data);

    $form = $this->modules->get('InputfieldForm');

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_name';
    $field->label = 'Sitename';
    $form->add($field);

    $fieldset = $this->modules->get('InputfieldFieldset');
    $fieldset->name = 'site_contact_information_fieldset';
    $fieldset->label = 'Contact information';
    $fieldset->collapsed = true;

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_email';
    $field->label = 'E-mail';
    $fieldset->add($field);

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_telephone';
    $field->label = 'Telephone';
    $fieldset->add($field);

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_fax';
    $field->label = 'Fax';
    $fieldset->add($field);

    $form->add($fieldset);

    $fieldset = $this->modules->get('InputfieldFieldset');
    $fieldset->name = 'site_address_information_fieldset';
    $fieldset->label = 'Address information';
    $fieldset->collapsed = true;

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_address';
    $field->label = 'Address';
    $fieldset->add($field);

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_postal_code';
    $field->label = 'Postal code';
    $field->columnWidth = 34;
    $fieldset->add($field);

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_city';
    $field->label = 'City';
    $field->columnWidth = 66;
    $fieldset->add($field);

    $form->add($fieldset);

    $button = $this->modules->get('InputfieldSubmit');
    $button->value = 'Save';
    $form->add($button);

    $form->action = './savecustomsettings';

    return $form->render();
  }

  public function ___executeSaveCustomSettings() {
    if(!$this->input->post('submit')) throw new Wire404Exception();

    foreach($this->input->post as $key => $value) {
      if(strpos($key, 'site_') === 0) {
        $this->page->data($key, $value);
      }
    }
    
    $this->message('Saved Settings');

    $this->session->redirect('./');
  }
}

 

Share this post


Link to post
Share on other sites

I think you need $this->page->save(); to actually save the settings, otherwise the values you set are not persistent?

 

Share this post


Link to post
Share on other sites

Even with page->save() it doesn't get saved. I think it is because there is no place to actually save the value in the database, I dynamically try to add data and save it. But I don't know the right way to do it.

Share this post


Link to post
Share on other sites

Ok small update, I am now able to save to data into the module config settings. But I can not retrieve them afterwards to prefill fields. The var_dump just returns an empty array. Am I doing something wrong to retrieve the config data? When I check the database, the values are actually saved in the data field, so I'm sure it's not empty.

<?php namespace ProcessWire;

class ProcessCustomSettings extends Process implements Module, ConfigurableModule {

  public static function getModuleinfo() {
    return [
      'title' => 'Processwire Custom Settings',
      'summary' => 'Create your own custom settings.',
      'author' => 'Jonathan Claeys',
      'version' => '1.0.1',
      'page' => [
        'name' => 'settings',
        'title' => 'Settings',
      ],
    ];
  }

  public function ___execute() {
    var_dump($this->modules->getConfig($this));

    $form = $this->modules->get('InputfieldForm');

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_name';
    $field->label = 'Sitename';
    $form->add($field);

    $fieldset = $this->modules->get('InputfieldFieldset');
    $fieldset->name = 'site_contact_information_fieldset';
    $fieldset->label = 'Contact information';
    $fieldset->collapsed = true;

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_email';
    $field->label = 'E-mail';
    $fieldset->add($field);

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_telephone';
    $field->label = 'Telephone';
    $fieldset->add($field);

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_fax';
    $field->label = 'Fax';
    $fieldset->add($field);

    $form->add($fieldset);

    $fieldset = $this->modules->get('InputfieldFieldset');
    $fieldset->name = 'site_address_information_fieldset';
    $fieldset->label = 'Address information';
    $fieldset->collapsed = true;

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_address';
    $field->label = 'Address';
    $fieldset->add($field);

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_postal_code';
    $field->label = 'Postal code';
    $field->columnWidth = 34;
    $fieldset->add($field);

    $field = $this->modules->get('InputfieldText');
    $field->name = 'site_city';
    $field->label = 'City';
    $field->columnWidth = 66;
    $fieldset->add($field);

    $form->add($fieldset);

    $button = $this->modules->get('InputfieldSubmit');
    $button->value = 'Save';
    $form->add($button);

    $form->action = './savecustomsettings';

    return $form->render();
  }

  public function ___executeSaveCustomSettings() {
    if(!$this->input->post('submit')) throw new Wire404Exception();

    $data = array();

    foreach($this->input->post as $key => $value) {
      if(strpos($key, 'site_') === 0) {
        $data[$key] = $value;
      }
    }

    $this->modules->saveConfig($this, $data);
    
    $this->message('Saved Settings');

    $this->session->redirect('./');
  }
}

 

Share this post


Link to post
Share on other sites

Here is a demo module:

<?php namespace ProcessWire;

class ProcessSaveSettings extends Process implements ConfigurableModule {

	/**
	 * Module information
	 */
	public static function getModuleinfo() {
		return array(
			'title' => 'Site Settings',
			'summary' => 'A demo module showing how inputfield values can be saved in the module config.',
			'version' => '0.1.0',
			'author' => 'Robin Sallis',
			'icon' => 'cogs',
			'requires' => 'ProcessWire>=3.0.0, PHP>=5.4.0',
			'page' => array(
				'name' => 'site-settings',
				'title' => 'Site settings',
				'parent' => 'setup',
			),
		);
	}

	/**
	 * Execute
	 */
	public function ___execute() {

		$modules = $this->wire()->modules;
		$input = $this->wire()->input;

		/** @var InputfieldForm $form */
		$form = $modules->get('InputfieldForm');

		// Add an inputfield
		/** @var InputfieldText $f */
		$f = $modules->get('InputfieldText');
		$f_name = 'site_name';
		$f->name = $f_name;
		$f->label = 'Site name';
		$f->value = $this->$f_name;
		$form->add($f);

		// Add another inputfield
		/** @var InputfieldSelect $f */
		$f = $modules->get('InputfieldSelect');
		$f_name = 'header_colour';
		$f->name = $f_name;
		$f->label = 'Header colour';
		$f->addOption('#F00', 'Red');
		$f->addOption('#0F0', 'Green');
		$f->addOption('#00F', 'Blue');
		$f->value = $this->$f_name;
		$form->add($f);
		
		// Add more inputfields to the form here...

		/** @var InputfieldSubmit $s */
		$s = $modules->get('InputfieldSubmit');
		$s->name = 'save_settings';
		$s->value = 'Save settings';
		$form->add($s);

		// If the form was submitted...
		if($input->save_settings) {
			// Let the inputfield modules process their input
			$form->processInput($input->post);
			// Get all the inputfields in the form
			$inputfields = $form->getAll();
			$data = [];
			// Add the inputfield values to $data
			foreach($inputfields as $inputfield) {
				// Skip the submit button
				if($inputfield->type === 'submit') continue;
				$data[$inputfield->name] = $inputfield->value;
			}
			// Save the config data
			$modules->saveConfig($this, $data);
			// Redirect: https://en.wikipedia.org/wiki/Post/Redirect/Get
			$this->wire()->session->redirect('./');
		}

		return $form->render();
	}

	/**
	 * Config inputfields: required for ConfigurableModule
	 *
	 * @param InputfieldWrapper $inputfields
	 */
	public function getModuleConfigInputfields($inputfields) {}

}

And there are a couple of relevant modules that might suit your needs, or that you could study for learning purposes:
https://modules.processwire.com/modules/process-general-settings/
https://modules.processwire.com/modules/settings-factory/

Share this post


Link to post
Share on other sites

The thing is that saving works perfect, but retrieving them from the database doesn't. It returns an empty array, and I would want to prefill my values ofc. 

Share this post


Link to post
Share on other sites

If you just want to store some general configuration values without the need of a seperate admin page you simply could provide some configuration fields which you can access via module settings. It's a fast and easy way and it's covered in this post by Ryan:

https://processwire.com/blog/posts/new-module-configuration-options/

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...