Академический Документы
Профессиональный Документы
Культура Документы
Peter Martin Joomla World Conference 2012 Website: www.db8.nl Twitter: @pe7er Linkedin: http://linkedin.com/in/pe7er
Sunday November 18th, 2012
Overview Presentation
Introduction Core Hack Alternatives 1. Template Override 2. Alternative Layouts 3. Language Override 4. Use of Plugins 5. Clone Module 6. Component with own controller 7. Extra Fields 8. Overriding Core classes
Core Hack
Core Hack
Joomla
= Open Source license GNU GPL GPL protects freedom & rights of users Source code = public You can and are allowed to change all Joomla code Core Hack = modification in source code Joomla 3rd party extension
4
Core Hack
Disadvantage of changing core code: Stability
Might give problems with 3rd party extensions Your code changes can be overwritten when you upgrade
Maintainability
/components/com_contact/controllers/contact.php private function _sendEmail($data, $contact), just under // Prepare email body $mail->setBody($body); change into: $mail->setBody("IP address: ". $_SERVER['REMOTE_ADDR']."\n\n".$body);
7
Date: Sat, 17 Aug 2012 15:30:00 +0200 From: Visitor name <info@example.com> Reply-To: [Name visitor] <[email address visitor]> To: [email address SuperAdmin of Website] IP adres: 127.0.0.1 This is an enquiry email via http://www.example.com/ from: [Name visitor] <[email address visitor]> [Message of visitor]
8
1. Template override
10
1. Template override
Template
Components
& Modules
Template
11
Change
12
13
</ul>
14
15
16
17
Latest News
Saturday 01 January 2011, 01:00:01 - Beginners Saturday 01 January 2011, 01:00:01 - Getting Help Saturday 01 January 2011, 01:00:01 - Getting Started Saturday 01 January 2011, 01:00:01 - Joomla! Saturday 01 January 2011, 01:00:01 - Options
18
2. Alternative Layouts
19
2. Alternative Layouts
Alternative
Override
= Template Override XTD Extra Control Options regarding display Extra HTML output files in /templates/html/
Four
20
2. Alternative Layouts
Alternative layout: Add other tmpl HTML output files to Template Override folder: /templates/your_template/html/mod_articles_latest/
21
2. Alternative Layouts
Example: Rename override (#1from this presentation) default.php to non-existing tmpl file: layout-with-date.php:
22
3. Language Overrides
23
3. Language Overrides
Since Joomla 2.5 Before 2.5: Core hack language files Extensions > Language Manager > Overrides
24
3. Language Overrides
New,
25
3. Language Overrides
Read
26
3. Language Overrides
Save
& Close:
27
3. Language Overrides
Result:
28
3. Language Overrides
Important:
File
Note:
29
4. Use of Plugins
30
4. Use of Plugins
Joomla
Water
is H2O
Menu item H<sub>2</sub>O Water is H2O Article title H<sub>2</sub>O Water is H2O Text in article H<sub>2</sub>O Water is H O
2
31
4. Use of Plugins
ReReplacer
32
4. Use of Plugins
Start
subscript tag
End
33
subscript tag
4. Use of Plugins
Water
is H2O
Check the Menu/Article Alias! Menu item, Browser Page Title! Water is H2O
34
5. Clone a Module
35
5. Clone a Module
36
5. Clone a Module
Add
$html = JHtml::_('icons.buttons', $buttons); ?> <?php if (!empty($html)): ?> <div class="cpanel"><?php echo $html;?></div> <?php endif;?> Not possible to use template override...
37
5. Clone a Module
5a. Copy Module
/administrator/modules/mod_quickicon/ to /administrator/modules/mod_quickicon2/
38
5. Clone a Module
5c. Edit mod_quickicon references mod_quickicon2.php
$buttons = modQuickIcon2Helper::getButtons($params); require JModuleHelper::getLayoutPath('mod_quickicon2', $params->get('layout', 'default'));
mod_quickicon2.xml
<name>mod_quickicon2</name> <filename module="mod_quickicon2">mod_quickicon2.php</filename>
39
5. Clone a Module
5d. Add to Joomla: Extensions > Extension Manager > Discover
40
5. Clone a Module
5e. Add Module mod_quickicon2:
Extensions > Module Manager > Filter: administrator [New] > mod_quickicon2 Title: My own Quick Icons Position: icon
41
5. Clone a Module
Oops: Fatal error: Cannot redeclare class modQuickIconHelper in /administrator/modules/mod_quickicon2/helper.php on line 18
42
5. Clone a Module
5g. Add your own array
/administrator/modules/mod_quickicon2/helper.php array( 'link' => Jroute::_('index.php?option=com_search'), 'image' => 'header/icon-48-search.png', 'text' => Jtext::_('Search'), 'access' => array('core.manage', 'com_search') ),
43
5. Clone a Module
5h. Result:
44
45
contact component:
Displays contact details Displays contact form Retrieves input contact form (check input, send to specified email address)
However,
46
Put own controller in existing /controllers/ folder of component Template override: change hidden variables in form to trigger your own controller
Example:
change task:
<input type="hidden" name="task" value="my_own_controller.submit" />
48
becomes:
class ContactControllerMy_own_controller extends JControllerForm
49
6c2. Ask for model (Contact) with explicit prefix to prevent error:
Fatal error: Call to a member function getItem() on a non-object in /components/com_contact/controllers/my_own_controller.php on line 38
becomes:
$model = $this->getModel('Contact','ContactModel'); 50
6c3. Add your own code in method private function _sendEmail($data, $contact), just below // Prepare email body
$mail->setBody($body);
becomes:
$mail->setBody("IP address: "._SERVER['REMOTE_ADDR']. "\n\n".$body);
51
[Message of visitor]
53
runs extensions:
Components: URL index.php?com_content Modules: Menu item &menuItem=x Plugin listen to events (hooks) Components have hooks
54
default)
User Plugin:
Functionality
55
User Profile
User Plugin:
Events:
Documentation:
56
fields in articles? Joomla's core component com_content 3 places to display extra fields:
1.Front-end - Display extra fields in article 2.Front-end - Extra fields in article editor form 3.Back-end - Extra fields in article editor form
Content Plugin:
Functionality
57
Article Fields
onContentPrepareData onContentPrepareForm onUserAfterSave onUserAfterDelete onContentPrepare
Content Plugin:
Events:
Documentation:
Adding
58
59
Core
classes can
What
if you want to add something to a core class so that all inheritances will have that code?
60
Documentation:
61
short:
System Plugins are loaded before 1st event (onBeforeInitialise) Plugins can load Classes / plain code Create System Plugin that loads your modified core class (with include_once) Classes that have already been loaded before the System Plugins are imported...
Exception:
62
Override
63
65
66
9. Other Thoughts
.htaccess
301 Redirect /old-file.php http://example.com/new-file.php Does *not* work for files retrieved via filesystem (e.g. via include_once in .php files)
Symbolic Link? http://en.wikipedia.org/wiki/Symbolic_link Linux: ln -s target_path link_path Copy a Joomla Core File to /template/override/ folder & overwrite original with non-writable symlink to copy?
67
68
Core Hacks are NOT documented Analyse code to find Core Hacks:
Joomla 2.5.8 without 3rd party extensions: 5,586 items, totalling 19.0 MB Test site with some 3rd party extensions: 7,618 items, totalling 42.2 MB
Good luck!
69
Preparations
1/2:
Back-up of website (Use Akeeba backup!) Local LAMP stack (LAMP/XAMPP/MAMP/WAMP) Restore back-up to two websites using 2 databases:
/my-site-with-hack/ /my-site-without-hack/
70
2/2:
Download same version of Joomla & unzip Download same versions of 3rd party extensions Overwrite all files with Joomla files (except /installation/ folder) to
/my-site-without-hack/
Result:
71
/my-site-with-hack/ /my-site-without-hack/
72
73
74
Conclusion
75
Conclusion
Core Hack = Modification of core files (Joomla or 3rd party) Eight Alternatives to avoid Core Hacks
1. Template Override (copy extensions HTML output to your templates /html/ directory and change that) 2. Alternative Layouts 3. Language Overrides 4. Use Plugins 5. Clone Module (copy complete module & change that) 6. Own controller (add own controller & use template override to trigger your controller) 7. Extra fields 8. Overriding Core Classes
76
Conclusion
Core
Hack don't!
Most
of the 8 alternatives:
Disadvantage:
Questions?
78
Questions?
Used Photos
Axe - Peter Huys, http://www.sxc.hu/photo/808871 Photo Frame 9 Billy Alexander, http://www.sxc.hu/photo/1367198 Bengali Keyborad - Mohammad Jobaed Adnan http://www.sxc.hu/photo/676844 usb - Vangelis Thomaidis, http://www.sxc.hu/photo/913590 HiSpeed copier 1 - Marcin Barowski, http://www.sxc.hu/photo/537037 Game pad - Michal Zacharzewski, http://www.sxc.hu/photo/957040 EXTRA Warmth - Nicolas Raymond http://www.sxc.hu/photo/971125 blueprint - Kerem Yucel, http://www.sxc.hu/photo/282237 Red Plaster - Paul Barker, http://www.sxc.hu/photo/1114174 signs signs - Jason Antony, http://www.sxc.hu/photo/751034 Face - Questions - Bob Smith, http://www.sxc.hu/photo/418215
80