News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

CPTTA: Change Profile Text To Avatar &/ Displayname (Hook Version!)

Started by Hj Ahmad Rasyid Hj Ismail, September 04, 2014, 03:19:56 PM

Previous topic - Next topic

Hj Ahmad Rasyid Hj Ismail

Link to Mod

CPTTA: Change Profile Text To Avatar &/ Displayname

- Version 1.0.0 For SMF 2.0.x.
- Fully hook, no file modification)

1. Please do your own backup though every installation is backed up automatically.
2. This mod will change your profile text in menu to user avatar, if any.
3. Upon its successful installation, you will be brought to the mod settings page where you can enable it.
4. Though you can also float it to the far right of the menu, there is nothing fancy about this mod.
5. With new addition, you can now add/replace profile text with user/displayname with or without user avatar.
6. You can test it in lower version too as IMO it should work just fine. ;)


Thank you for using/testing it.


Yours friendly,
Abu Fahim Ismail.

BSD License. Feel free to modify accordingly but keep author's link if it is in there somewhere. ;)
Note: This mod uses Arantor's code instead of mine in the relevant tip/trick topic.

 


#Change Logs

@Version 1.0.3
- Fix displayname not displaying if there is no avatar selected.

@Version 1.0.2
- Enhance to add/replace profile text with user/displayname with or without user avatar.
- Fix styling issues.

@Version 1.0.1
- Allow float profile menu button to float to the right.
- Fully hook, no file modification.

@Version 1.0.0
- Initial Release.

Hj Ahmad Rasyid Hj Ismail

Tutorial - How this basic hook mod is written?

Basically to write a basic hook mod like this, you will need certain basic files namely:
1. Package file for installation purposes (package-info.xml).
2. Source file for your mod (MOD.subs.php).
3. Hook file for hooking code to SMF default files (MOD.hook.php).
4. Language file for adding or defining text strings (MOD.english.php).
5. Readme file for information about the mod (MOD.readme.php).

The way the files is named will depends on SMF guides but so far this way is approved.

I will now focus to the source file and the hook file as the others is IMO not that difficult

In this mod I created two functions in the source file. Since I have limited functions, I just use one file. As you can see, other mods may use more.

Here is the code for the source file:

<?php
/**
 *
 * @author  ahrasis http://smf.ahrasis.com
 * @license BSD http://opensource.org/licenses/BSD-3-Clause
 * @mod     CPTTA: Change Profile Text To Avatar
 *
 */
 
if (!defined('SMF')) 
die('Hacking attempt...');

function 
CPTTA_Menu(&$areas)
{
global $txt$modSettings$settings$context;

// Add CSS
if (!empty($modSettings['cptta_enable']) && !empty($modSettings['cptta_float_right'])) {
$context['html_headers'] .= "\n" '
<style type="text/css">
ul li#button_profile { float:right; }
</style>
'
;
}

// Change Profile Text To Avatar
if (!empty($modSettings['cptta_enable'])) {
$areas['profile']['title'] = empty($context['user']['avatar']['href']) ? $txt['profile'] : '<img src="' $context['user']['avatar']['href'] . '" title="' $txt['profile'] . '" alt="' $txt['profile'] . '" class="altmenu_blur" />';
}
}

function 
CPTTA_Settings(&$config_vars)
{
global $txt;

// Load the language
loadLanguage('CPTTA');

$cptta = array(
// Add Settings
array('check''cptta_enable'),
array('check''cptta_float_right'),
);
// Insert after all available slice.
$first array_slice($config_vars0);
$config_vars array_merge($first$cptta);
}
?>


The one on the top is the description of you, your license and your mod. Then there are the said two functions, one is for adding / changing menu i.e. the profile text to avatar and the other is for adding its settings for admin in the Mod Settings page.

The source file

The first function

SMF menu is governed by Subs.php and it hook as mentioned in that file itself should use (&$areas). So that is why, the first function have that ending. The function can be named anything as long as it follows SMF convention in coding like "_" or capitalize first letter (am I using the correct words - SMF convention? :P).

I started with globalizing the variables, then the code that I need to inject to the header. This could be any code of yours that need to be inserted in the header including but not limited to css, javascript, html or php code.

I then call for the menu, if this mod is enabled. As you can see, with this mod, I redefined the current profile title by $areas['profile']['title']. And that was it for the first part. Easy right?

The second function

The second one is not necessary if you don't to give the admin the ability to turn the mod on or off, or any other settings. But if you need one or more, then you can add it in via default mod settings page or create your own page. In this mod, I just used the default one.

I adapted the slice method just in to the last part of the available mod(s). It can be used to insert your own setting on top or in the middle or on the last part.

Done with the source file.


The source file
Now let's go the hook file which is very basic. Here is the code:

<?php
/**
 *
 * @author  ahrasis http://smf.ahrasis.com
 * @license BSD http://opensource.org/licenses/BSD-3-Clause
 * @mod     CPTTA: Change Profile Text To Avatar
 *
 */

// If we have found SSI.php and we are outside of SMF, then we are running standalone.
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('SMF'))
require_once(dirname(__FILE__) . '/SSI.php');

// If we are outside SMF and can't find SSI.php, then throw an error
elseif (!defined('SMF')) 
die('<b>Error:</b> Cannot install - please verify you put this file in the same place as SMF\'s SSI.php.');

if (
SMF == 'SSI')
db_extend('packages');

// Define the hooks
$hook_functions = array(
'integrate_pre_include' => '$sourcedir/CPTTA.subs.php',
'integrate_menu_buttons' => 'CPTTA_Menu',
'integrate_general_mod_settings' => 'CPTTA_Settings',
);

// Adding or removing them?
if (!empty($context['uninstalling']))
$call 'remove_integration_function';
else
$call 'add_integration_function';

// Do the deed
foreach ($hook_functions as $hook => $function)
$call($hook$function);

if (
SMF == 'SSI')
   echo 
'Congratulations! You have successfully installed this mod!';

?>


To tell you the truth I don't know much part of this code except the one in the middle. Ask somebody who has better knowledge to understand that will ya?

The main part is this one:
// Define the hooks
$hook_functions = array(
'integrate_pre_include' => '$sourcedir/CPTTA.subs.php',
'integrate_menu_buttons' => 'CPTTA_Menu',
'integrate_general_mod_settings' => 'CPTTA_Settings',
);


First, the source file is being called via integrate_pre_include hook function.

Then the menu function in the said file is being called via integrate_menu_buttons. This will run the said function which is defined in the source file.

The last one is adding the settings in the general Mod Settings page.

And that's all about writing hook in this mod. There is nothing fancy in this mod code. But by writing this mod tutorial, I hope I can share my limited knowledge and experience.

You are free to ask question that relates to this mod and its coding.

Cheers.

Hj Ahmad Rasyid Hj Ismail

This mod is now updated and can also add/replace profile text with displayname, with or without username. Styling issues are also fixed in this version.

#Change Logs

@Version 1.0.2
- Enhance to add/replace profile text with user/displayname with or without user avatar.
- Fix styling issues.

@Version 1.0.1
- Allow float profile menu button to float to the right.
- Fully hook, no file modification.

@Version 1.0.0
- Initial Release.

Hj Ahmad Rasyid Hj Ismail

Fixed and updated.

#Change Logs

@Version 1.0.3
- Fix displayname not displaying if there is no avatar selected.

vkot

I didn't install the mod, I just want to say thank you for the tutorial on hook mod.
Maybe one day I will try to make my hard-coded hacks to mods...
For specialized SMF installation/customization, Web Development, Linux Server Administration, click here.
Για εξειδικευμένες υπηρεσίες στα παραπάνω, πατήστε εδώ.

Hj Ahmad Rasyid Hj Ismail

Feel free to do it vkot. It is not so difficult. I believe if I can do it, anybody else can do it too. Good luck!

Advertisement: