Jump to content

Saving moduledata issue


Recommended Posts

Hi all,

First of all - keep in mind I'm not that experienced creating modules / general ProcessWire...

I'm creating a module which can be used to tweet a page when saving it.

When building this module, the following issue came up;

My module has multiple (text) inputfields for connecting to OAuth (http://oauth.net), so as minimal as four fields should be set before an attempt to connect will be done, so no default values can be set.

Only when saving the module, I cannot test the connection directly with the new posted data, why?

When tested multiple times with a lot of different settings, the first message is a succesful connection to Twitter, and a few lines later the message is failed (or vice-versa depending on posted data). What am I missing/doing wrong?

edit: I'm overwriting the $data variables with post data, but in my opinion this shouldn't be the case

$wp = wire()->input->post;

/**
* Set required fields for use of this module
*
* @return InputfieldWrapper
*/
public function getModuleConfigInputfields(array $data) {
    $modules = wire('modules');
    $templates = wire('templates');
    $fields = wire('fields');
    $fieldgroups = wire('fieldgroups');

    foreach(self::getDefaultConfig() as $key => $value) {
        if(!isset($data[$key])) $data[$key] = $value;
    }

    // Get default Twitter settings
    $_ck = $data['publish_to_twitter_consumerkey'];
    $_cs = $data['publish_to_twitter_consumersecret'];
    $_at = $data['publish_to_twitter_accesstoken'];
    $_as = $data['publish_to_twitter_accesstokensecret'];

    // update/check data only after post
    $wp = wire()->input->post;
    if (wire('page')->template == 'admin' && count($wp)) {
        $_ck = $wp->publish_to_twitter_consumerkey;
        $_cs = $wp->publish_to_twitter_consumersecret;
        $_at = $wp->publish_to_twitter_accesstoken;
        $_as = $wp->publish_to_twitter_accesstokensecret;

        if(isset($_ck) && isset($_cs) && isset($_at) && isset($_as)) {
    // wire('session')->message("ck = $_ck");
    // wire('session')->message("cs = $_cs");
    // wire('session')->message("at = $_at");
    // wire('session')->message("as = $_as");

            // now we try to connect to Twitter with an OAuth connection and search for hashtag processwire
            require 'twitteroauth/twitteroauth.php';
            $query = array(
                "q" => '#processwire',
                "count" => "5",
                "result_type" => "recent",
                // "include_entities" => "true",
            );
            $toa = new TwitterOAuth($_ck, $_cs, $_at, $_as);
            $results = $toa->get('search/tweets', $query);

            // check if we got any response
            if (isset($results)) {
                // check for errors
                if(isset($results->errors) && count($results->errors)) {
                    foreach($results->errors as $error) {
                        wire('pages')->error("Twitter response: $error->message (Error code: $error->code)");
                    }
                } else {
                    // successful connected
                    wire('session')->message("Connection to Twitter was successful");
                }
            }
        }
    }

    $inputfields = new InputfieldWrapper();

    // Twitter settings:
    // Twitter Consumer Key
    $f = $modules->get('InputfieldText');
    $f->attr('name', 'publish_to_twitter_consumerkey');
    $f->label = __('Twitter Consumer Key');
    $f->required = true;
    $f->columnWidth = 50;
    $f->attr('value', $data['publish_to_twitter_consumerkey']);
    $inputfields->add($f);

    // Twitter Consumer Secret
    $f = $modules->get('InputfieldText');
    $f->attr('name', 'publish_to_twitter_consumersecret');
    $f->label = __('Twitter Consumer Secret');
    $f->required = true;
    $f->columnWidth = 50;
    $f->attr('value', $data['publish_to_twitter_consumersecret']);
    $inputfields->add($f);

    // Twitter Access Token
    $f = $modules->get('InputfieldText');
    $f->attr('name', 'publish_to_twitter_accesstoken');
    $f->label = __('Twitter Access Token');
    $f->required = true;
    $f->columnWidth = 50;
    $f->attr('value', $data['publish_to_twitter_accesstoken']);
    $inputfields->add($f);

    // Twitter Access Token Secret
    $f = $modules->get('InputfieldText');
    $f->attr('name', 'publish_to_twitter_accesstokensecret');
    $f->label = __('Twitter Access Token Secret');
    $f->required = true;
    $f->columnWidth = 50;
    $f->attr('value', $data['publish_to_twitter_accesstokensecret']);
    $inputfields->add($f);
    
    return $inputfields;
}
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

  • Recently Browsing   0 members

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