Jump to content

Recommended Posts

Posted

Hi all,

I'm new to Processwire and PHP. I'm trying to adapt a module I found which interacts with Twilio. I can install the module no problem and save settings but when calling a module function in a template I get the error:

Quote

Fatal error: Uncaught Error: Call to a member function hi() on null in /opt/lampp/htdocs/processwire/site/templates/twiliochannels.php:18 Stack trace: #0 

After getting similar errors with more complex functions I've tried simplifying the function as much as possible.

My template is:

Quote
<?php namespace ProcessWire;
 
    include('./_head.php'); ?>
 
    <div id='content'><?php
    $module = $modules->get('TwilioChannelsConfig');
    echo "<p>" . $module->hi() . "</p>";
    ?></div>
 

and my .module file is:

Quote

class TwilioChannels extends WireData implements Module, ConfigurableModule {
    public function init() {
    }
}

my module config file is:

Quote
<?php
 
class TwilioChannelsConfig extends ModuleConfig {
     public function hi() {
     return "Hi there ";
     }
}
 

I've tried adding hi() to both the module config file and the .module file. I presume it needs to be in the .module file but neither work.


Thanks in advance for any help given!

Posted

If you see that "call ... on null", then your $module variable is null. Are you certain that installing the module actually worked? I at least miss something like a getModuleInfo method.

Posted

To add to the previous reply. I've tried printing the module info in my template to confirm the install and I get

$moduleInfo = $modules->getModuleInfo('TwilioChannelsConfig');
echo "<p>" . implode(" ", $moduleInfo) . "</p>";

and I get 

Quote

Notice: Array to string conversion in /opt/lampp/htdocs/processwire/site/templates/twiliochannels.php on line 7

0 TwilioChannelsConfig 0 0.0.0 Inactive Array Array Array Array 0 Unable to locate module

This is despite being able to edit the module settings via the admin page.

Posted

If you include a TwilioChannelsConfig.php file with your TwilioChannels module (see the blog post that introduced this approach), this file is only for defining the config fields for the module. It isn't a module in its own right that you can get with $module = $modules->get('TwilioChannelsConfig').

Put all your methods - including your hi() method - into the main TwilioChannels module file.

$module = $modules->get('TwilioChannels');
echo "<p>" . $module->hi() . "</p>";

 

  • Like 1
Posted

Hi Robin, it's working now I've put my functions in the module file, not the config file.

Apologies for the stupid question and thanks for your help!

  • Like 1

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...