For whatever reason, EE dev is just not clicking with me at this point. For some befangled reason, using the EE hooks has me confused. 😉
I have started an extension that I have been able to code up the _lang, ext., and views/index.php files to allow you to install the extension and set the settings, which are saving to the db. Basically, I just want to, for simplicity purposes, ‘automagically’ if installed, take each entry as it is displayed and run through the installed fieldtypes, adding a class of say ‘test_seen’ to each of those fieldtypes. I have an array of fieldtypes installed already, I just don’t know how to trigger the extension to parse the entry before rendering and make the class addition.
If someone can help me make this ‘click’ in my head, that would be great.
Thanks so much.
Brian
*** EDIT ***
Ok, I can now call my method, but I keep getting a blank white screen anytime I try to modify or simply output a js alert. I know this is probably simple, I am just overlooking something
function activate_extension()
{
$data = array(
'class' => 'myExtension_ext',
'method' => 'myMethod',
'hook' => 'sessions_end',
'settings' => serialize($this->settings),
'priority' => 10,
'version' => $this->version,
'enabled' => 'y'
);
$this->EE->db->insert('extensions', $data);
}public function myMethod()
{
if (REQ == 'PAGE')
{
$this->EE->javascript->output("alert('test');");
}
return $this->EE->load->view('index', $vars, TRUE);
}Anything pop out? Do I need to output something different, different hook?
Thanks guys.
I’m not really sure I follow with the goal of the add-on, but…
I can now call my method, but I keep getting a blank white screen anytime I try to modify or simply output a js alert. I know this is probably simple, I am just overlooking something
I suggest turning on “debug” mode in EE. I development you should seldom see a white screen. In the least you should have some PHP error messages to help tell you why the page didn’t load properly.
Erik,
Thanks. I think I was looking at it all wrong. This wasn’t really a ‘functional’ test here. Basically, I just wanted to be able to throw an alert on the page when it opened. That was all. I figured if I could get that far, I could better understand hook usage. Once the page loads, I am fine. I am just trying to get my head better wrapped around the usage of the EE hooks. (How they work, how to use them, what to return, etc…)
I figured if I could get that far, I could better understand hook usage. Once the page loads, I am fine. I am just trying to get my head better wrapped around the usage of the EE hooks. (How they work, how to use them, what to return, etc…)
Then I’d suggest you take a look at the sessions_end hook for starters. Take a look at the docs and consider the data that is passed to the extension and what data it expects back. Then based on certain session user data perform certain actions or set other variables etc. That might help get a grasp of how EE hooks work.
Erik
I read through the docs earlier this week, but still lacked a good understanding.
So on ‘session_end’, it reads: Modify the user’s session/member data, also allows for additional session or login methods (ex: log in to other system)
That hook looks like :
$edata = $this->extensions->call('sessions_end', $this);
if ($this->extensions->end_script === TRUE) return;Being that it returns ‘void’, I initially tried to just output the data on the screen of the session inside.
At this point there wouldn’t be any entry data per say, but the user and other session data held within, correct?
If I were to do nothing more than just want to see what was passed through the hook/method call, how would I be able to view the data, like a
var_dump($this)or
var_dump($edata), and halt the page load so I can analyze the available elements? Or am I still looking at it wrong? ;/
I get the following:
PHP Fatal error: ob_start() [ref.outcontrol]: Cannot use output buffering in output buffering display handlers in C:\websites\EE2Dev\system\codeigniter\system\core\Exceptions.php on line 166
From:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Alertmee_ext
{
public $settings = array();
public $name = 'AlertM:ee';
public $version = '0.0.1';
public $description = 'A sample extension...';
public $settings_exist = 'y';
public $docs_url = 'http://www.google.com';
// --------------------------------------------------------------------
/**
* Alertmee_ext
*/
public function __construct($settings = '')
{
$this->EE =& get_instance();
$this->settings = $settings;
}
// --------------------------------------------------------------------
/**
* Activate Extension
*
* @return void
*/
function activate_extension()
{
//$this->settings = $settings;
$data = array(
'class' => __CLASS__,
'method' => 'myMethod',
'hook' => 'sessions_end',
'settings' => serialize($this->settings),
'priority' => 10,
'version' => $this->version,
'enabled' => 'y'
);
$this->EE->db->insert('extensions', $data);
}
// --------------------------------------------------------------------
/**
* Update Extension
*
* @return mixed void on update / false if none
*/
function update_extension($current = '')
{
if ($current == '' OR $current == $this->version)
{
return FALSE;
}
if ($current < '0.0.1')
{
// Update to version 1.0
}
$this->EE->db->where('class', 'Alertmee_ext');
$this->EE->db->update(
'extensions',
array('version' => $this->version)
);
}
// --------------------------------------------------------------------
/**
* Disable Extension
*
* @return void
*/
function disable_extension()
{
$this->EE->db->where('class', 'Alertmee_ext');
$this->EE->db->delete('extensions');
}
// --------------------------------------------------------------------
/**
* settings
*
* @return void
*/
public function settings()
{
$settings = array();
// Creates a text input with a default value of "AlertM:ee"
$settings['name'] = array('i', '', "AlertM:ee");
// Creates a textarea with 20 rows and a default value of 'This will throw a javascript alert on page load.'
$settings['description'] = array('t', array('rows' => '20'), 'This will throw a javascript alert on page load.');
// Creates a set of radio buttons, one for "Yes" (y), one for "No" (n) and a default of "Yes"
$settings['cool'] = array('r', array('y' => "Yes", 'n' => "No"), 'y');
return $settings;
}
// --------------------------------------------------------------------
/**
* Save Settings
*
* @return void
*/
function save_settings()
{
$this->EE->db->where('class', __CLASS__);
$this->EE->db->update('extensions', array('settings' => serialize($this->settings)));
}
/**
* showAlert
*
* @return void
*/
public function myMethod()
{
if (REQ == 'PAGE')
{
var_dump($data);
}
}
}Brian,
You need to add the hook’s parameters in your own function. The parameters available can be found in the extension hook’s documentation code example. It’s the variables that appear after the hook name in the code sample. With our example of sessions_end you current have:
public function myMethod() {}But you should have this:
public function myMethod($data) {}Where “$data” is the same thing as “$this” in the docs here: http://ellislab.com/expressionengine/user-guide/development/extension_hooks/global/session/index.html#sessions-end
With every hook (and with object oriented code in general) you need to include the parameter variables if you want to work with them in your own methods.
Make sense?
Erik
Yeah. Thanks Eric.
I guess my question then would be this. Is there a way to see, or preview the data being passed in those parameters? (Similar to a console log or even a var_dump) So I can better educate myself on the data being handled through the system? I know this is a quite general question so I apologize for that, I would just like to see the data contained in the object prior to “working” with it.
By the way, your help is greatly appreciated.
Brian
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.