Jump to content

Getting option fields for module


Harmster
 Share

Recommended Posts

Hey everyone,

I would like to start off saying that I am sorry for posting this but I couldn't find something about this and I really need to know this ASAP.

So I want to create a module where the admin can enter an API key in the module settings, how do i create that input field and more important, how and where do i save the information he admin has entered?

Much appreciated and thanks in advance!

Link to comment
Share on other sites

Your module has to implement the ConfigureableModule Interface:

class MyModule extends WireData implements Module, ConfigurableModule

Then you can implement the following method, PW will handle saving and displaying the module options!

static public function getModuleConfigInputfields(array $data) {
 $fields = new InputfieldWrapper();

 //Generate a Text field for entering the API Key
 $field = $modules->get("InputfieldText");
 $field->attr('name+id', 'apiKey');
 $field->attr('value', $data['apiKey']);
 $field->label = "API Key (Developer Key)";
 $field->description = 'Enter the API key';
 $fields->append($field);
 return $fields;
}

The code above should let you save the API Key in the module config options. Now in your module, you can retrieve the key:

$apiKey = $this->get('apiKey');
//or $apiKey = $this->apiKey;

Hope this helps!

Also I suggest looking at other modules which have config options

  • Like 3
Link to comment
Share on other sites

Thank you Wanze, it worked.

However for some reason it seems that I can't use the methods from my class in the website.

I've set autoload to true and i've set a fuel like this

public static function getModuleInfo() {
 return array(
	 'title' => 'Mailchimp',
	 'version' => 101,
	 'summary' => 'An example module used for demonstration purposes. See the /site/modules/Helloworld.module file for details.',
	 'singular' => true,
	 'autoload' => true,
	 );
}
public function init()
{
 $this->setFuel("mailchimp", $this);
}
public function get_apikey(){
return $apiKey = $this->get('apiKey');
}

But when i try to use it like this:

$api_key = $mailchimp->get_apikey();
echo $api_key;

With the error:

Error Call to a member function get_apikey() on a non-object (line 11 of /home/harm/NetBeansProjects/phpScriptjes/mailchimp/site/templates/basic-page.php)

This error message was shown because you are logged in as a Superuser. Error has been logged.

Any thoughts?

EDIT:

Also when i try to use $this in any context within the class i get errors like these:

Error Using $this when not in object context (line 34 of /home/harm/NetBeansProjects/phpScriptjes/mailchimp/site/modules/Mailchimp.module)
Link to comment
Share on other sites

Error Call to a member function get_apikey() on a non-object

in this case $mailchimp isn't an object.

Maybe you have set autload to true after installing module? Try reinstall it, as it won't change it if youc hange the code solely.

Also when i try to use $this in any context within the class i get errors like these:

This is if you call $this inside an static method.

Link to comment
Share on other sites

Error Call to a member function get_apikey() on a non-object

in this case $mailchimp isn't an object.

Maybe you have set autload to true after installing module? Try reinstall it, as it won't change it if youc hange the code solely.

This is if you call $this inside an static method.

I tried reinstalling and i got this error:

Error Using $this when not in object context (line 96 of /home/harm/NetBeansProjects/phpScriptjes/mailchimp/wire/core/Data.php)

Link to comment
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
 Share

×
×
  • Create New...