ok so the block from the docs is
if (isset($EXT->extensions['weblog_entries_tagdata']))
{
$tagdata = $EXT->call_extension('weblog_entries_tagdata', $tagdata, $row);
echo 'tagdata: '.$tagdata;
if ($EXT->end_script === TRUE) return $edata;
}
I am using it like
function set_content($tagdata, $row, $callback){
global $EXT;
if (isset($EXT->extensions['weblog_entries_tagdata']))
{
$tagdata = $EXT->call_extension('weblog_entries_tagdata', $tagdata, $row);
echo 'tagdata: '.$tagdata;
if ($EXT->end_script === TRUE) return $edata;
}
}
Few questions, the docs have it passing “$this” is that required (its not showing in the syntax example, where do I access it? Clearly you cant use that in your own class so is there a standard var that should be used in its content? Where the heck is “$edata” coming from or being set?
I am assuming my code should simply pass the data thru the method unmolested yet when I use it my the content is blank so I assume I am not passing something correctly.
I also cant find much info on the correct use of call_extension(). The data is getting passed into my method set_content() fine, how do I return it? Is that what call_extension does? If not how do I pass the data back? Should $EXT->call_extension return something? I check it and its either a Boolean or blank.
back to pulling out my hair…
Moved to Extensions: Technical Assistance by Moderator
Have you had a look at the Extension Development docs? They do not contain a specific example with this hook, but it might give you a good handle on the overview.
This might make it click for you:
function set_content($tagdata, $row)
{
return 'Hello world! - {title}<hr >';
}
Keep in mind that if other extensions are registered to this hook, you’ll want to make sure they work together nicely (this is covered in the docs linked above, too):
function set_content($tagdata, $row)
{
global $EXT;
$tagdata = ($EXT->last_call !== FALSE) ? $EXT->last_call : $tagdata;
$tagdata .= 'Modified!';
return $tagdata;
}
Sorry for the confusion, I assume you are meaning the code shown here. That is a reference to the extension hook in the application code, so that you can see at a glance what arguments are sent, and what the return data will impact. You will not need to use any of the Extension class methods in your code; the $last_call and $end_script properties are the only possible things you may need to access from that class, the rest is handled internally.
Thanks for your help Derek,
I finally have a working solution. However it does not seem as dynmatic as it could be. Thoughts on a better way to handle the set_content() are welcome and I hope the code can help anyone else in my shoes on day.
<?php if ( ! defined('EXT')) exit('No direct script access allowed');
/**
* Multi Language for Bayshore content
*
* This class swaps {zone1_en} for the correct language as defined in $_SESSION['multi_lang']
*/
class Multilang {
var $settings = array();
var $name = 'Multi Language Ext';
var $version = '1.0';
var $description = 'Controls the custom field output.';
var $settings_exist = 'n';
var $docs_url = 'http://ryanmills.net/';
/**
* Constructor
*/
function Multilang($settings = '')
{
}
/**
* Register hooks by adding them to the database
*/
function activate_extension()
{
global $DB;
// default settings
$settings = array();
$hook = array(
'extension_id' => '',
'class' => __CLASS__,
'method' => 'set_content',
'hook' => 'weblog_entries_tagdata',
'settings' => serialize($settings),
'priority' => 1,
'version' => $this->version,
'enabled' => 'y'
);
$DB->query($DB->insert_string('exp_extensions', $hook));
}
// --------------------------------------------------------------------
/**
* No updates yet.
* Manual says this function is required.
* @param string $current currently installed version
*/
function update_extension($current = '')
{
global $DB, $EXT;
if ($current < '1.0')
{
$query = $DB->query("SELECT settings FROM exp_extensions WHERE class = '".$DB->escape_str(__CLASS__)."'");
$this->settings = unserialize($query->row['settings']);
$DB->query($DB->update_string('exp_extensions', array('settings' => serialize($this->settings), 'version' => $this->version), array('class' => __CLASS__)));
}
return TRUE;
}
// --------------------------------------------------------------------
/**
* Uninstalls extension
*/
function disable_extension()
{
global $DB;
$DB->query("DELETE FROM exp_extensions WHERE class = '".__CLASS__."'");
}
// --------------------------------------------------------------------
/**
* EE extension settings
* @return array
*/
function settings()
{
$settings = array();
return $settings;
}
// --------------------------------------------------------------------
/**
* Set Content
* @return string
*/
function set_content($tagdata, $row, $callback){
global $EXT;
# init sessions if its not started
if (session_id() == "")
{
session_start();
}
# Define the default lang
if( !isset($_GET['set_lang']) ){
$_GET['multi_lang'] = 'en';
}
# Make sure no one tries to set a bad lang type.
if( isset( $_GET['set_lang'] ) ){
if($_GET['set_lang'] == 'en'){
$_SESSION['multi_lang'] = 'en';
}
if($_GET['set_lang'] == 'ru'){
$_SESSION['multi_lang'] = 'ru';
}
if($_GET['set_lang'] == 'es'){
$_SESSION['multi_lang'] = 'es';
}
}
if (isset($EXT->extensions['weblog_entries_tagdata']))
{
if( $tagdata == '{zone1_en}' ){
switch($_SESSION['multi_lang']){
case 'en':
$edata = $row['field_id_4']; // english content
break;
case 'ru':
$edata = $row['field_id_5']; // russian content
break;
case 'es':
$edata = $row['field_id_6']; // spanish content
break;
default:
$edata = $row['field_id_4']; // default english content
}
return $edata;
}
}
}
}
?>
if (isset($EXT->extensions['weblog_entries_tagdata']))
{
if( $tagdata == '{zone1_en}' ){
switch($_SESSION['multi_lang']){
case 'en':
$edata = $row['field_id_4']; // english content
break;
case 'ru':
$edata = $row['field_id_5']; // russian content
break;
case 'es':
$edata = $row['field_id_6']; // spanish content
break;
default:
$edata = $row['field_id_4']; // default english content
}
return $edata;
}
}
You needn’t worry if that array key is set, if it’s not, your code will not be executed. I was thinking something more like this:
switch($_SESSION['multi_lang'])
{
case 'ru':
$tagdata = str_replace(LD.'zone1_en'.RD, LD.'zone1_ru'.RD, $tagdata);
break;
case 'es':
$tagdata = str_replace(LD.'zone1_en'.RD, LD.'zone1_es'.RD, $tagdata);
break;
case 'en':
default:
break;
}
return $tagdata;
Basically, just switching out the English template variable with the appropriate custom field variable from that weblog based on the language setting. The rest of the weblog module’s entry parsing routine will handle the rest.
Also, you could move the _SESSION language setting logic to an earlier extension hook (like those in the EE Session class), so that it’s available to this and all other methods you may need to use it for.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.