Jump to content

Twilio Channels Module: Using Module Config Variables in .module file and templates


britnell
 Share

Recommended Posts

Hi All,

I'm new to developing in PHP and Processwire so apologies in advance for any stupid questions.


I'm working on developing a module for the Twilio Channels API. I'd like to be able to input the API credentials into the modules settings page and then call the API credentials in module functions and, where necessary, in templates.

I can currently add the API details and save them in my module config file, no problem.
ModuleConfig file:

<?php 

class TwilioChannelsConfig extends ModuleConfig {
    public function getDefaults() {
        return array(
            "id" => "",
            "token" => "",
            "from_number" => ""
        );
    }

    public function getInputfields() {
        $inputfields = parent::getInputfields();

        $f = $this->modules->get('InputfieldText');
        $f->attr('name', 'id');
        $f->label = $this->_('Twilio SID');
        $f->notes = $this->_('From your Twilio Account (at https://www.twilio.com/user/account)');
        $f->columnWidth = 50;
        $inputfields->add($f);

        $f = $this->modules->get('InputfieldText');
        $f->attr('name', 'token');
        $f->label = $this->_('Twilio API Token');
        $f->columnWidth = 50;
        $inputfields->add($f);

        $f = $this->modules->get('InputfieldText');
        $f->attr('name', 'from_number');
        $f->label = $this->_('Your Twilio Number');
        $f->description = $this->_('Will be used as the "from" number when sending messages or starting calls.');
        $inputfields->add($f);

        return $inputfields;
    }
}

What I'd like to be able to do is either call getInputFields() in a template, something similar to this:

$moduleConfig = $modules->get('TwilioChannelsConfig');
echo "<p>" . $moduleConfig->getInputFields('id') . "</p>";

or to call the credentials to my .module file.

I've tried a few different approaches but can't get it to work. Not sure what I'm missing.

Thanks in advance for any help.

  • Like 1
Link to comment
Share on other sites

Usual way is something like this in your module (adjust to suit your needs):

   /**
     * @return array
     */
    static public function getDefaultConfig()
    {
        return array(
            "my-module-config-1"          => "",
            "my-module-config-2"          => "",
            "my-module-config-3"          => "my default setting for this config field",
        );
    }

    /**
     * Populate default configuration (will be overwritten after constructor with user's own configuration)
     *
     */
    public function ___construct() {
        foreach(self::getDefaultConfig() as $key => $value) {
            $this->$key = $value;
        }
    }

    /**
     * @param array $data
     * @return InputfieldWrapper
     */
    public function getModuleConfigInputfields(array $data)
    {
        $inputfields = new InputfieldWrapper();

        $defaults = self::getDefaultConfig();
        $data = array_merge($defaults, $data);

        $f = $this->modules->get('InputfieldText');
        $f->name = 'my-module-config-1';
        $f->label = __('MyModule key 1');
        $f->value = $data['my-module-config-1'];
        $f->collapsed = 5 ; // collapsedPopulated
        $inputfields->add($f);

        $f = $this->modules->get('InputfieldText');
        $f->name = 'my-module-config-2';
        $f->label = __('MyModule Config key 2');
        $f->collapsed = 5 ; // collapsedPopulated
        $f->value = $data['my-module-config-2'];
        $inputfields->add($f);

        $f = $this->modules->get('InputfieldText');
        $f->name = 'my-module-config-3';
        $f->label = __('MyModule config key 3');
        $f->collapsed = 5 ; // collapsedPopulated
        $f->value = $data['my-module-config-3'];
        $inputfields->add($f);

        return $inputfields;
    }

 

Then you can call it in your template like:

<?php
    $myModule = $modules->get('MyModule');
?>
<p><?=$myModule->my-module-config-1?></p>

Best way to learn is to look into other popular PW custom modules ?

  • Like 2
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...