3

Wonder is anyone can help me convert this EE2 addon to EE3

<?php

$plugin_info = array(
                        'pi_name'           => 'String',
                        'pi_version'        => '1.2.0',
                        'pi_author'         => 'Brett Altworth',
                        'pi_description'    => 'Allows string variables to be manipulated and printed in ways that are inconvenient with template tags alone.',
                        'pi_usage'          => String::usage()
                    );

class String {
    var $return_data = "";

    function String() {
        global $SESS, $TMPL;
        if(!isset($SESS->cache['string'])) {
            $SESS->cache['string'] = array();
        }

        if($TMPL->fetch_param('name') && isset($SESS->cache['string'][$TMPL->fetch_param('name')])) {
            $this->return_data = $SESS->cache['string'][$TMPL->fetch_param('name')];
        }
    }

    function set() {
        global $SESS;

        if(($data = $this->_get_params()) === false) {
            return;
        }

        $SESS->cache['string'][$data['name']] = $data['text'];
    }

    function clear() {
        global $SESS, $TMPL;

        if($TMPL->fetch_param('name') && isset($SESS->cache['string'][$TMPL->fetch_param('name')])) {
            unset($SESS->cache['string'][$TMPL->fetch_param('name')]);
        }
    }

    function append() {
        global $SESS;

        if(($data = $this->_get_params()) === false) {
            return;
        }

        if(!isset($SESS->cache['string'][$data['name']])) {
            $SESS->cache['string'][$data['name']] = $data['text'];
        } else {
            $str = "{$data['glue']}{$data['text']}";
            if($data['sep'] != "") {
                $str = "{$data['glue']}{$data['sep']}" . $str;
            }
            $SESS->cache['string'][$data['name']] = $SESS->cache['string'][$data['name']] . $str;
        }
    }

    function prepend() {
        global $SESS;

        if(($data = $this->_get_params()) === false) {
            return;
        }

        if(!isset($SESS->cache['string'][$data['name']])) {
            $SESS->cache['string'][$data['name']] = $data['text'];
        } else {
            $str = "{$data['text']}{$data['glue']}";
            if($data['sep'] != "") {
                $str .= "{$data['sep']}{$data['glue']}";
            }
            $SESS->cache['string'][$data['name']] = $str . $SESS->cache['string'][$data['name']];
        }
    }

    function output() {
        global $SESS, $TMPL, $FNS;

        $name = $TMPL->fetch_param('name');
        $value = "";

        if($name !== false && isset($SESS->cache['string'][$name])) {
            $value = trim($SESS->cache['string'][$name]);
        }

        if(trim($TMPL->tagdata) == "") {
            return $value;
        } else {
            $vars = $SESS->cache['string'];
            $vars['string'] = $value;
            $tagdata = $FNS->prep_conditionals($TMPL->tagdata, $vars);
            foreach($vars as $k=>$v) {
                $tagdata = $TMPL->swap_var_single($k, $v, $tagdata);
            }
            return $tagdata;
        }
    }

    function _get_params() {
        global $TMPL;

        $ret = array();
        $ret['text']    = false;
        $ret['name']    = $TMPL->fetch_param('name')        ? $TMPL->fetch_param('name') : false;
        $ret['sep']     = $TMPL->fetch_param('separator')   ? $TMPL->fetch_param('separator') : "";
        $ret['glue']    = $TMPL->fetch_param('glue')        ? $TMPL->fetch_param('glue') : "SPACE";

        if($TMPL->tagdata) {
            $ret['text'] = trim($TMPL->tagdata);
        } else if($TMPL->fetch_param('text')) {
            $ret['text'] = trim($TMPL->fetch_param('text'));
        } else if($TMPL->fetch_param('default')) {
            $ret['text'] = trim($TMPL->fetch_param('default'));
        }

        if(!$ret['name'] || !$ret['text']) {
            return false;
        }

        switch($ret['sep']) {
            case "SPACE"   : $ret['sep'] = " "; break;
            case "TAB"     : $ret['sep'] = "\t"; break;
            case "NEWLINE" : $ret['sep'] = "\n"; break;
            case "NONE"    : $ret['sep'] = ""; break;
        }

        switch($ret['glue']) {
            case "SPACE"   : $ret['glue'] = " "; break;
            case "TAB"     : $ret['glue'] = "\t"; break;
            case "NEWLINE" : $ret['glue'] = "\n"; break;
            case "NONE"    : $ret['glue'] = ""; break;
        }

        return $ret;
    }

    function usage() {
        ob_start();
        ?>See http://www.south.com/add-ons/string-plugin/ for documentation.<?php
        $buffer = ob_get_contents();
        ob_end_clean(); 

        return $buffer;
    }
}
?>

1 Answer 1

6

Converting from EE2 to EE3 is easy. All the syntax's etc. are almost same. You just need to create an extra file named addon.setup.php and done.

In your case, follow the points below:

  1. Open FTP and go to your site/system/user/addons/
  2. Create a Folder named string. Enter in the folder.
  3. Create a file named pi.string.php and put this code inside:

    $plugin_info = array(
            'pi_name'           => 'String',
            'pi_version'        => '1.2.0',
            'pi_author'         => 'Brett Altworth',
            'pi_description'    => 'Allows string variables to be manipulated and printed in ways that are inconvenient with template tags alone.',
            'pi_usage'          => string::usage()
        );
    
    class String {
        var $return_data = "";
    
        function __construct() {
            if(!isset($this->EE->session->cache['string'])) {
                $this->EE->session->cache['string'] = array();
            }
    
            /* Load EE Instance */
            $this->EE =& get_instance();
    
            if($this->EE->TMPL->fetch_param('name') && isset($this->EE->session->cache['string'][$this->EE->TMPL->fetch_param('name')])) {
                $this->return_data = $this->EE->session->cache['string'][$this->EE->TMPL->fetch_param('name')];
            }
        }
    
        function set() {
            if(($data = $this->_get_params()) === false) {
                return;
            }
    
            $this->EE->session->cache['string'][$data['name']] = $data['text'];
        }
    
        function clear() {
            if($this->EE->TMPL->fetch_param('name') && isset($this->EE->session->cache['string'][$this->EE->TMPL->fetch_param('name')])) {
                unset($this->EE->session->cache['string'][$this->EE->TMPL->fetch_param('name')]);
            }
        }
    
        function append() {
            if(($data = $this->_get_params()) === false) {
                return;
            }
    
            if(!isset($this->EE->session->cache['string'][$data['name']])) {
                $this->EE->session->cache['string'][$data['name']] = $data['text'];
            } else {
                $str = "{$data['glue']}{$data['text']}";
                if($data['sep'] != "") {
                    $str = "{$data['glue']}{$data['sep']}" . $str;
                }
                $this->EE->session->cache['string'][$data['name']] = $this->EE->session->cache['string'][$data['name']] . $str;
            }
        }
    
        function prepend() {
            if(($data = $this->_get_params()) === false) {
                return;
            }
    
            if(!isset($this->EE->session->cache['string'][$data['name']])) {
                $this->EE->session->cache['string'][$data['name']] = $data['text'];
            } else {
                $str = "{$data['text']}{$data['glue']}";
                if($data['sep'] != "") {
                    $str .= "{$data['sep']}{$data['glue']}";
                }
                $this->EE->session->cache['string'][$data['name']] = $str . $this->EE->session->cache['string'][$data['name']];
            }
        }
    
        function output() {
            global $FNS;
    
            $name = $this->EE->TMPL->fetch_param('name');
            $value = "";
    
            if($name !== false && isset($this->EE->session->cache['string'][$name])) {
                $value = trim($this->EE->session->cache['string'][$name]);
            }
    
            if(trim($this->EE->TMPL->tagdata) == "") {
                return $value;
            } else {
                $vars = $this->EE->session->cache['string'];
                $vars['string'] = $value;
                $tagdata = $FNS->prep_conditionals($this->EE->TMPL->tagdata, $vars);
                foreach($vars as $k=>$v) {
                    $tagdata = $this->EE->swap_var_single($k, $v, $tagdata);
                }
                return $tagdata;
            }
        }
    
        function _get_params() {
            global $TMPL;
    
            $ret = array();
            $ret['text']    = false;
            $ret['name']    = $this->EE->TMPL->fetch_param('name')        ? $this->EE->TMPL->fetch_param('name') : false;
            $ret['sep']     = $this->EE->TMPL->fetch_param('separator')   ? $this->EE->TMPL->fetch_param('separator') : "";
            $ret['glue']    = $this->EE->TMPL->fetch_param('glue')        ? $this->EE->TMPL->fetch_param('glue') : "SPACE";
    
            if($this->EE->TMPL->tagdata) {
                $ret['text'] = trim($this->EE->TMPL->tagdata);
            } else if($this->EE->TMPL->fetch_param('text')) {
                $ret['text'] = trim($this->EE->TMPL->fetch_param('text'));
            } else if($this->EE->TMPL->fetch_param('default')) {
                $ret['text'] = trim($this->EE->TMPL->fetch_param('default'));
            }
    
            if(!$ret['name'] || !$ret['text']) {
                return false;
            }
    
            switch($ret['sep']) {
                case "SPACE"   : $ret['sep'] = " "; break;
                case "TAB"     : $ret['sep'] = "\t"; break;
                case "NEWLINE" : $ret['sep'] = "\n"; break;
                case "NONE"    : $ret['sep'] = ""; break;
            }
    
            switch($ret['glue']) {
                case "SPACE"   : $ret['glue'] = " "; break;
                case "TAB"     : $ret['glue'] = "\t"; break;
                case "NEWLINE" : $ret['glue'] = "\n"; break;
                case "NONE"    : $ret['glue'] = ""; break;
            }
    
            return $ret;
        }
    
        static function usage() {
            ob_start();
            ?>See http://www.south.com/add-ons/string-plugin/ for documentation.<?php
            $buffer = ob_get_contents();
            ob_end_clean(); 
    
            return $buffer;
        }
    }
    ?>
    
  4. Create a file named addon.setup.php and put this code inside:

    <?php
    return array(
        'author' => 'Brett Altworth',
        'author_url' => '#',
        'description' => 'Allows string variables to be manipulated and printed in ways that are inconvenient with template tags alone.',
        'docs_url' => '#',
        'name' => 'String',
        'settings_exist' => false,
        'version' => '1.2.0',
        'namespace'   => 'EllisLab\Addons\String',
    );
    
  5. Install the plugin from backend.

3
  • Thanks Mufi, I tried your code but I get this error: Fatal error: Call to a member function fetch_param() on null in /home/H/xx/www/xx/user/addons/string/pi.string.php on line 21
    – Kippi
    Mar 2, 2017 at 12:24
  • Gotcha. I have edited answer. put the new code in your pi file Mar 2, 2017 at 13:15
  • 1
    Hi Mufi, you are a champ. Thank you so much :D
    – Kippi
    Mar 2, 2017 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.