Simple Add-ons

Part I: Plugins

is.gd/j4EIoz

 

Isaac Raway (@airways, airways@mm.st)

MetaSushi / The Nerdery

Custom plugins - why?

Getting started

Getting started

/**
 * ExpressionEngine - by EllisLab
 *
 * @package		ExpressionEngine
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2003 - 2011, EllisLab, Inc.
 * @license		http://expressionengine.com/user_guide/license.html
 * @link		http://expressionengine.com
 * @since		Version 2.0
 * @filesource
 */
 
// ------------------------------------------------------------------------

Anatomy of a plugin

system/expressionengine/third_party/member_info/
system/expressionengine/third_party/member_info/pi.member_info.php
    
class Member_info {
    

Anatomy of a plugin

$plugin_info = array(
    'pi_name'           => 'Member_info',
    'pi_version'        => '1.0',
    'pi_author'         => 'Isaac Raway',
    'pi_author_url'     => '',
    'pi_description'    => 'Gets member information for the supplied ID',
    'pi_usage'          => Member_info::usage(),
);
    

Anatomy of a plugin

class Member_info {
    public function __construct()
    {
        $this->EE =& get_instance();
    }
    public static function usage()
    {
        // pkg.io uses ob_start, HEREDOC does the same thing, but it's shorter:
        return <<<END
How to call this plugin:
{exp:member_info:query member_id="something"}
{/exp:member_info:query}
END;
    }
}

    

Anatomy of a plugin

$plugin_info = array(
    ...
    'pi_usage'          => Member_info::usage(),
);
    

And now the most important bullet point in the presentation:

Creating a tag

public function query() {
}
    
{exp:member_info:query}
{/exp:member_info:query}
    

Creating a tag

public function query() {
    // 1. Get params from fetch_param() - default value is FALSE
    // 2. Get the contents of the pair tag
    // 3. Setup a default response
    // 4. Run our query
    // 5. Parse results variables into the output
    // 6. Return the results as a string
}
    

Tag processing - step 1

// 1. Get params from fetch_param()
$requested_member_id = $this->EE->TMPL->fetch_param("member_id");
    
$test = $this->EE->TMPL->fetch_param("test", -1);
    

Tag processing - step 2

// 2. Get the contents of the pair tag
$result = $this->EE->TMPL->tagdata;
    
{exp:member_info:query}
    This is the part that comes back in tagdata.
{/exp:member_info:query}
    

Tag processing - step 3

// 3. Setup a default response
$vars = array(
    'found' => FALSE
);
    
{exp:member_info:query}
    {if found} ... {if:else}
        No results found for that ID!
    {/if}
{/exp:member_info:query}
    

Tag processing - step 4

// 4. Run our query
if($requested_member_id) {
   $query = $this->EE->db->where('member_id', $requested_member_id)
                         ->get('members');
    

Tag processing - step 4

        // Check that we have at least one row
        if($query->num_rows() > 0) {
            // Replace the default return vars array
            // with the result row
            $vars = $query->row_array();
            $vars['found'] = TRUE;
        }
    

Tag processing - step 4

        // Check if request is for their own data,
        // if not we can provide less info:
        $vars['is_current_user'] =
          (
            $requested_member_id 
               == $this->EE->session->member_id
          );
    }
    

Tag processing - step 5

// 5. Parse results variables into the output
$result = $this->EE->TMPL->parse_variables($result, array($row));
    

Tag processing - step 6

// 6. Return the results as a string
return $result;
    

Using the tag

{exp:member_info:query member_id="{segment_3}"}
    {if found}
        Screen Name: {screen_name}<br/>
        {if is_current_user}
            Registered Email Address: {email}<br/>
            Username: {username}<br/>
            <a href="{site_url}account/edit/{segment_3}">Edit</a>
        {/if}
    {if:else}
        Invalid request.
    {/if}
{/exp:member_info:query}
    

Questions?












is.gd/j4EIoz

Isaac Raway (@airways, airways@mm.st)