SlideShare ist ein Scribd-Unternehmen logo
1 von 70
Introduzione allo sviluppo in Drupal 7 
Theming 
Andrea Panisson 
@andypanix
Chi sono 
Lavoro in 
come frontend developer 
e Drupal themer
Voi? 
Sviluppatori? 
Themers? 
Altro?
Come funziona un 
tema in Drupal 7?
A simplified wiring guide to the Theme Layer 
http://john.albin.net/drupal/arrays-of-doom
www.drupal.org/theming
Il theme layer 
di Drupal è 
formato da 
una serie di 
overrides 
Evaluating Base Themes 
Emma Jane Hogbin bit.ly/dday14-tb1
Drupal’s theme layer
Il theme layer di Drupal 
THEME HOOKs 
Un “theme hook” è semplicemente il nome 
di uno specifico modo per generare il 
markup di un insieme di dati grezzi
THEME HOOKs 
theme_item_list 
theme_link 
theme_image 
theme_menu_link 
bit.ly/dday14-tb3
THEME HOOKs 
Per definire un novo theme hook, 
ovvero creare una nuova funzione 
di theming, è sufficiente usare la funzione 
hook_theme() 
bit.ly/dday14-tb4
Il theme layer di Drupal 
2 sono i modi 
di implementare un 
theme hook
Il theme layer di Drupal 
Theme functions: passare i dati ad una 
funzione PHP e restiture del markup 
Templates: passare i dati ad un file di 
template, un file PHP composto da un mix 
di markup e PHP
THEME HOOKs 
Possiamo ridefinire come 
“theme hooks” tutte quelle 
funzioni o file di template che 
sono state/i specificatamente 
registrate/i tramite un 
hook_theme()
THEME HOOKs (function) 
<?php 
/** 
* Implements hook_theme(). 
*/ 
function mymodule_theme() { 
return array( 
'my_theme_hook' => array( 
'variables' => array('parameter' => NULL), 
'function' => 'theme_my_theme_hook', 
), 
); 
}
THEME HOOKs (function) 
<?php 
function theme_my_theme_hook($variables) { 
return '<div>USELESS!!</div>'; 
}
THEME HOOKs (template) 
<?php 
/** 
* Implements hook_theme(). 
*/ 
function mymodule_theme() { 
return array( 
'my_theme_hook' => array( 
'variables' => array('parameter' => NULL), 
'template' => 'my-template-file', 
), 
); 
}
THEME HOOKs (template) 
my-template-file.tpl.php 
<div>I’M USELESS<div>
THEME HOOKs 
Come utilizzare una funzione di theming: 
theme("my_theme_hook", $vars) 
theme(“item_list”, $vars) 
theme_my_theme_hook($vars) 
theme_item_list($vars)
THEME HOOKs 
Chiamare la funzione di theming usando 
theme("my_theme_hook", $vars) 
Permette successivamente di fare l’override di quella 
funzione nel tema, ad esempio nel file template.php o 
nella cartella del tema (se trattasi di template)
THEME HOOK 
function mythemename_my_theme_hook($variables) { 
return '<div>LESS USELESS??</div>'; 
} 
Override di una funzione di theming 
ad esempio nel tema (template.php):
Il theme layer di Drupal 
Drupal 7 Module Development - bit.ly/dday14-tb5
La «magia» 
della funzione 
theme() 
Drupal 7 Module Development - bit.ly/dday14-tb5
A simplified wiring guide to the Theme Layer 
http://john.albin.net/drupal/arrays-of-doom
The Drupal Theme System
THEME ENGINE 
E’ un insieme di script e file che 
interagiscono con il core del CMS ed 
interpretano il linguaggio di 
programmazione utilizzato nel tema
THEME ENGINE 
A cosa serve un theme engine? 
E’ un modo comodo per stampare l’output 
temizzabile in vari file di template 
piuttosto che utilizzare del semplice PHP
THEME ENGINE 
Aiuta a separare 
la funzionalità 
dalla presentazione 
Business logic - Presentation logic 
(separazione dei contesti)
THEME ENGINE 
Drupal 7 => PHPtemplate 
Drupal 8 => Twig
TEMPLATE ENGINE 
PHPTemplate 
HTML + PHP 
PHP for Themers su Drupalize.me 
https://drupalize.me/videos/php-themers?p=1146
Il theme system di Drupal
Il theme system di Drupal 
Un tema è un insieme 
strutturato di codice (al pari di 
un modulo) che fornisce degli 
strumenti atti a trasformare dei 
dati grezzi in un output 
strutturato e formattato.
Il tema in PHPtemplate 
themename.info 
page.tpl.php 
*.tpl.php 
template.php (business logic) 
*.js 
*.css
.info file 
Theme Metadata 
• Il nome del tema 
• La descrizione 
• La versione e le informazioni sulla compatibilità 
• Il template engine richiesto 
• I file CSS globali 
• I file JS globali 
• Le regioni incluse 
• Le impostazioni di default del tema 
• etc…
.info file 
Theme Metadata 
• Nome macchina del tema deve essere unico, 
non lo stesso di qualche modulo 
• Preferibilmente solo lettere, quindi non 
mio_tema ma miotema (best practice)
.info file 
Theme Metadata 
Regioni obbligatorie: 
regions[page_top] = Page Top 
regions[content] = Content 
regions[page_bottom] = Page Bottom 
Page Top e Page bottom sono regioni nascoste 
regions_hidden[] = region_name
.info file 
Theme Metadata 
Writing theme .info files 
www.drupal.org/node/171205
Template files *.tpl.php 
HTML e PHP
Template files *.tpl.php 
<?php if(_CONDITION_): ?> 
<p>Stampa questo!</p> 
<?php endif; ?> 
<?php print $stringa; ?> 
<?php print t($stringa_trad) ?> 
<?php hide($dopo); ?> 
<?php render($content); ?> 
<?php render($dopo); ?>
Il tema in PHPtemplate 
themename.info 
page.tpl.php 
*.tpl.php 
template.php (business logic) 
*.js 
*.css
Template files *.tpl.php 
html.tpl.php -> modules/system 
page.tpl.php -> modules/system 
region.tpl.php -> modules/system 
block.tpl.php -> modules/block 
node.tpl.php -> modules/node 
comment.tpl.php -> modules/comment 
field.tpl.php-> modules/field/theme 
RENDERING ORDER
http://bit.ly/dday14-tb2 
Rendering 
degli 
elementi 
della 
pagina
Template files *.tpl.php 
field.tpl.php viene utilizzato solo se 
sovrascritto nel tema, in caso 
contrario è utilizzata la relativa 
funzione di theming
www.drupal.org/node/171194 
Riassumendo
Il tema in PHPtemplate 
EXAMPLES
La «magia» 
della funzione 
theme() 
Drupal 7 Module Development - bit.ly/dday14-tb5
Template files *.tpl.php 
Solo print, render e if condition 
statements, nessuna logica 
<?php if(_CONDITION_): ?> 
<p>Stampa questo!</p> 
<?php endif; ?> 
<?php print $stringa; ?> 
<?php print t($stringa_trad) ?> 
<?php hide($dopo); ?> 
<?php render($content); ?> 
<?php render($dopo); ?>
Le funzioni di 
preprocess e process 
Devono essere usate per tutte 
le operazioni di business logic 
necessarie a livello di 
presentation logic 
del nostro progetto Drupal
Le funzioni di 
preprocess e process 
Consentono ad un modulo / 
tema di alterare o aggiungere 
variabili utilizzate da un altro 
modulo quando quest’ultimo 
chiama un theme hook
Le funzioni di 
preprocess e process 
Ogni theme hook registrato tramite la 
funzione hook_theme() ha le sue 
funzioni di preprocess e process. 
Ogni file di template ha le sue 
funzione di preprocess / process
Le funzioni di 
preprocess e process 
Tutte le funzioni di preprocess e process 
hanno la seguente forma: 
[module]_preprocess_[theme hook name](&$variables)
Le funzioni di 
preprocess e process 
function mymodule_preprocess_item_list(&$variables) { 
// Add a class to the list wrapper. 
$variables['attributes']['class'][] = 'dday14'; 
}
Preprocess functions 
1. template_preprocess() 
2. template_preprocesss_HOOK() 
3. MODULE_preprocess() 
4. MODULE_preprocess_HOOK() 
5. THEME_preprocess() 
6. THEME_preprocess_HOOK()
Process functions 
7. template_process() 
8. template_processs_HOOK() 
9. MODULE_process() 
10. MODULE_process_HOOK() 
11. THEME_process() 
12. THEME_process_HOOK()
Tempalte files *.tpl.php 
PREPROCESS 
EXAMPLES
La «magia» 
della funzione 
theme() 
Drupal 7 Module Development - bit.ly/dday14-tb5
Theme Hook Suggestions 
Le theme hook suggestions 
permettono di effettuare un 
override selettivo sia 
dei file di template che delle 
funzioni di theming
Theme Hook Suggestions: BLOCK 
block--[region|[module|--delta]].tpl.php 
base template: block.tpl.php 
block--module--delta.tpl.php 
block--module.tpl.php 
block--region.tpl.php
Theme Hook Suggestions 
• www.drupal.org/node/1089656 
• bit.ly/dday14-tb6 (The Definitive Guide to Drupal 7 
• Theme Hooks and Theme Hook Suggestions) 
$vars['theme_hook_suggestions'] 
($mothership_poorthemers_helper)
Theme Hook Suggestions 
SUGGESTIONS 
EXAMPLES
Theming views 
EXAMPLES
Un po’ di codice 
Sporchiamoci le mani: 
• theme-settings.php 
• drush for themers
L’angolo dei moduli 
• Devel (debugging with dsm()) 
• Devel Themer 
• Fences 
• Semantic Fields 
• Display suite
Domande?
Drupal 7 : theming avanzato

Weitere ähnliche Inhalte

Ähnlich wie Drupal 7 : theming avanzato

Creare un tema personalizzato per wordpress
Creare un tema personalizzato per wordpressCreare un tema personalizzato per wordpress
Creare un tema personalizzato per wordpressGGDBologna
 
Come portare il profiler di symfony2 in drupal8
Come portare il profiler di symfony2 in drupal8Come portare il profiler di symfony2 in drupal8
Come portare il profiler di symfony2 in drupal8Luca Lusso
 
DNM19 Sessione2 Orchard Temi e Layout (Ita)
DNM19 Sessione2 Orchard Temi e Layout (Ita)DNM19 Sessione2 Orchard Temi e Layout (Ita)
DNM19 Sessione2 Orchard Temi e Layout (Ita)Alessandro Giorgetti
 
Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...
Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...
Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...DrupalDay
 
Drupal Day 2012 - DRUPAL 8: I CAMBIAMENTI CHE CI ASPETTANO
Drupal Day 2012 - DRUPAL 8:  I CAMBIAMENTI CHE CI ASPETTANODrupal Day 2012 - DRUPAL 8:  I CAMBIAMENTI CHE CI ASPETTANO
Drupal Day 2012 - DRUPAL 8: I CAMBIAMENTI CHE CI ASPETTANODrupalDay
 
I Temi in WordPress
I Temi in WordPress I Temi in WordPress
I Temi in WordPress Thomas Vitale
 
Presentazione django reminiscence
Presentazione django reminiscencePresentazione django reminiscence
Presentazione django reminiscenceAndrea Gottardi
 
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen..."Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...bmeme
 
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen..."Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...DrupalDay
 
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019Matteo Enna
 
Sviluppare un plugin WordPress da zero - WordCamp Bologna 2018
Sviluppare un plugin WordPress da zero - WordCamp Bologna 2018Sviluppare un plugin WordPress da zero - WordCamp Bologna 2018
Sviluppare un plugin WordPress da zero - WordCamp Bologna 2018Marco Chiesi
 
Wordpress Template hierarchy
Wordpress Template hierarchyWordpress Template hierarchy
Wordpress Template hierarchyGloria Liuni
 
DNM19 Sessione1 Orchard Primo Impatto (ita)
DNM19 Sessione1 Orchard Primo Impatto (ita)DNM19 Sessione1 Orchard Primo Impatto (ita)
DNM19 Sessione1 Orchard Primo Impatto (ita)Alessandro Giorgetti
 
Drupal come framework di sviluppo
Drupal come framework di sviluppoDrupal come framework di sviluppo
Drupal come framework di sviluppoGrUSP
 
Progetto di Basi di Dati
Progetto di Basi di DatiProgetto di Basi di Dati
Progetto di Basi di Datisegarva
 
[Gian Mario Mereu] - Drupal & Patternlab: un nuovo approccio al theming
[Gian Mario Mereu] - Drupal & Patternlab: un nuovo approccio al theming[Gian Mario Mereu] - Drupal & Patternlab: un nuovo approccio al theming
[Gian Mario Mereu] - Drupal & Patternlab: un nuovo approccio al themingWellnet srl
 

Ähnlich wie Drupal 7 : theming avanzato (20)

Creare un tema personalizzato per wordpress
Creare un tema personalizzato per wordpressCreare un tema personalizzato per wordpress
Creare un tema personalizzato per wordpress
 
Come portare il profiler di symfony2 in drupal8
Come portare il profiler di symfony2 in drupal8Come portare il profiler di symfony2 in drupal8
Come portare il profiler di symfony2 in drupal8
 
DNM19 Sessione2 Orchard Temi e Layout (Ita)
DNM19 Sessione2 Orchard Temi e Layout (Ita)DNM19 Sessione2 Orchard Temi e Layout (Ita)
DNM19 Sessione2 Orchard Temi e Layout (Ita)
 
Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...
Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...
Drupal 8: dal download del Core alla pubblicazione in produzione. Cos'è cambi...
 
eZ publish - Extension
eZ publish - ExtensioneZ publish - Extension
eZ publish - Extension
 
Drupal Day 2012 - DRUPAL 8: I CAMBIAMENTI CHE CI ASPETTANO
Drupal Day 2012 - DRUPAL 8:  I CAMBIAMENTI CHE CI ASPETTANODrupal Day 2012 - DRUPAL 8:  I CAMBIAMENTI CHE CI ASPETTANO
Drupal Day 2012 - DRUPAL 8: I CAMBIAMENTI CHE CI ASPETTANO
 
Laravel Framework PHP
Laravel Framework PHPLaravel Framework PHP
Laravel Framework PHP
 
I Temi in WordPress
I Temi in WordPress I Temi in WordPress
I Temi in WordPress
 
Presentazione django reminiscence
Presentazione django reminiscencePresentazione django reminiscence
Presentazione django reminiscence
 
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen..."Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
 
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen..."Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
"Twig e i belli dentro": panoramica sui nuovi standard di frontend-developmen...
 
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
Drupal diventa un CMF e WordPress che fa? Slide WordCamp Milano 2019
 
Sviluppare un plugin WordPress da zero - WordCamp Bologna 2018
Sviluppare un plugin WordPress da zero - WordCamp Bologna 2018Sviluppare un plugin WordPress da zero - WordCamp Bologna 2018
Sviluppare un plugin WordPress da zero - WordCamp Bologna 2018
 
Wordpress Template hierarchy
Wordpress Template hierarchyWordpress Template hierarchy
Wordpress Template hierarchy
 
Perl Template Toolkit
Perl Template ToolkitPerl Template Toolkit
Perl Template Toolkit
 
Java lezione 17
Java lezione 17Java lezione 17
Java lezione 17
 
DNM19 Sessione1 Orchard Primo Impatto (ita)
DNM19 Sessione1 Orchard Primo Impatto (ita)DNM19 Sessione1 Orchard Primo Impatto (ita)
DNM19 Sessione1 Orchard Primo Impatto (ita)
 
Drupal come framework di sviluppo
Drupal come framework di sviluppoDrupal come framework di sviluppo
Drupal come framework di sviluppo
 
Progetto di Basi di Dati
Progetto di Basi di DatiProgetto di Basi di Dati
Progetto di Basi di Dati
 
[Gian Mario Mereu] - Drupal & Patternlab: un nuovo approccio al theming
[Gian Mario Mereu] - Drupal & Patternlab: un nuovo approccio al theming[Gian Mario Mereu] - Drupal & Patternlab: un nuovo approccio al theming
[Gian Mario Mereu] - Drupal & Patternlab: un nuovo approccio al theming
 

Mehr von Twinbit

Drupal Agile: DRUPAL ED IL MERCATO ENTERPRISE
Drupal Agile: DRUPAL ED IL MERCATO ENTERPRISEDrupal Agile: DRUPAL ED IL MERCATO ENTERPRISE
Drupal Agile: DRUPAL ED IL MERCATO ENTERPRISETwinbit
 
Drupal performance and scalability
Drupal performance and scalabilityDrupal performance and scalability
Drupal performance and scalabilityTwinbit
 
Managing Spatial Information and Services with Drupal: the GEO-MOOD approach
Managing Spatial Information and Services with Drupal: the GEO-MOOD approachManaging Spatial Information and Services with Drupal: the GEO-MOOD approach
Managing Spatial Information and Services with Drupal: the GEO-MOOD approachTwinbit
 
Which base theme for your Drupal project
Which base theme for your Drupal projectWhich base theme for your Drupal project
Which base theme for your Drupal projectTwinbit
 
Mobile Drupal
Mobile DrupalMobile Drupal
Mobile DrupalTwinbit
 
Thinking spatially with your open data
Thinking spatially with your open dataThinking spatially with your open data
Thinking spatially with your open dataTwinbit
 
When Drupal meets OpenData
When Drupal meets OpenDataWhen Drupal meets OpenData
When Drupal meets OpenDataTwinbit
 
Drupal in the Cloud
Drupal in the CloudDrupal in the Cloud
Drupal in the CloudTwinbit
 
Drupal + Facebook @ DrupalSocialCulb
Drupal + Facebook @ DrupalSocialCulbDrupal + Facebook @ DrupalSocialCulb
Drupal + Facebook @ DrupalSocialCulbTwinbit
 
Sviluppare applicazioni Facebook utilizzando Drupal
Sviluppare applicazioni Facebook utilizzando DrupalSviluppare applicazioni Facebook utilizzando Drupal
Sviluppare applicazioni Facebook utilizzando DrupalTwinbit
 

Mehr von Twinbit (10)

Drupal Agile: DRUPAL ED IL MERCATO ENTERPRISE
Drupal Agile: DRUPAL ED IL MERCATO ENTERPRISEDrupal Agile: DRUPAL ED IL MERCATO ENTERPRISE
Drupal Agile: DRUPAL ED IL MERCATO ENTERPRISE
 
Drupal performance and scalability
Drupal performance and scalabilityDrupal performance and scalability
Drupal performance and scalability
 
Managing Spatial Information and Services with Drupal: the GEO-MOOD approach
Managing Spatial Information and Services with Drupal: the GEO-MOOD approachManaging Spatial Information and Services with Drupal: the GEO-MOOD approach
Managing Spatial Information and Services with Drupal: the GEO-MOOD approach
 
Which base theme for your Drupal project
Which base theme for your Drupal projectWhich base theme for your Drupal project
Which base theme for your Drupal project
 
Mobile Drupal
Mobile DrupalMobile Drupal
Mobile Drupal
 
Thinking spatially with your open data
Thinking spatially with your open dataThinking spatially with your open data
Thinking spatially with your open data
 
When Drupal meets OpenData
When Drupal meets OpenDataWhen Drupal meets OpenData
When Drupal meets OpenData
 
Drupal in the Cloud
Drupal in the CloudDrupal in the Cloud
Drupal in the Cloud
 
Drupal + Facebook @ DrupalSocialCulb
Drupal + Facebook @ DrupalSocialCulbDrupal + Facebook @ DrupalSocialCulb
Drupal + Facebook @ DrupalSocialCulb
 
Sviluppare applicazioni Facebook utilizzando Drupal
Sviluppare applicazioni Facebook utilizzando DrupalSviluppare applicazioni Facebook utilizzando Drupal
Sviluppare applicazioni Facebook utilizzando Drupal
 

Drupal 7 : theming avanzato

  • 1.
  • 2. Introduzione allo sviluppo in Drupal 7 Theming Andrea Panisson @andypanix
  • 3. Chi sono Lavoro in come frontend developer e Drupal themer
  • 4.
  • 5.
  • 7. Come funziona un tema in Drupal 7?
  • 8. A simplified wiring guide to the Theme Layer http://john.albin.net/drupal/arrays-of-doom
  • 10. Il theme layer di Drupal è formato da una serie di overrides Evaluating Base Themes Emma Jane Hogbin bit.ly/dday14-tb1
  • 12. Il theme layer di Drupal THEME HOOKs Un “theme hook” è semplicemente il nome di uno specifico modo per generare il markup di un insieme di dati grezzi
  • 13. THEME HOOKs theme_item_list theme_link theme_image theme_menu_link bit.ly/dday14-tb3
  • 14. THEME HOOKs Per definire un novo theme hook, ovvero creare una nuova funzione di theming, è sufficiente usare la funzione hook_theme() bit.ly/dday14-tb4
  • 15. Il theme layer di Drupal 2 sono i modi di implementare un theme hook
  • 16. Il theme layer di Drupal Theme functions: passare i dati ad una funzione PHP e restiture del markup Templates: passare i dati ad un file di template, un file PHP composto da un mix di markup e PHP
  • 17. THEME HOOKs Possiamo ridefinire come “theme hooks” tutte quelle funzioni o file di template che sono state/i specificatamente registrate/i tramite un hook_theme()
  • 18. THEME HOOKs (function) <?php /** * Implements hook_theme(). */ function mymodule_theme() { return array( 'my_theme_hook' => array( 'variables' => array('parameter' => NULL), 'function' => 'theme_my_theme_hook', ), ); }
  • 19. THEME HOOKs (function) <?php function theme_my_theme_hook($variables) { return '<div>USELESS!!</div>'; }
  • 20. THEME HOOKs (template) <?php /** * Implements hook_theme(). */ function mymodule_theme() { return array( 'my_theme_hook' => array( 'variables' => array('parameter' => NULL), 'template' => 'my-template-file', ), ); }
  • 21. THEME HOOKs (template) my-template-file.tpl.php <div>I’M USELESS<div>
  • 22. THEME HOOKs Come utilizzare una funzione di theming: theme("my_theme_hook", $vars) theme(“item_list”, $vars) theme_my_theme_hook($vars) theme_item_list($vars)
  • 23. THEME HOOKs Chiamare la funzione di theming usando theme("my_theme_hook", $vars) Permette successivamente di fare l’override di quella funzione nel tema, ad esempio nel file template.php o nella cartella del tema (se trattasi di template)
  • 24. THEME HOOK function mythemename_my_theme_hook($variables) { return '<div>LESS USELESS??</div>'; } Override di una funzione di theming ad esempio nel tema (template.php):
  • 25. Il theme layer di Drupal Drupal 7 Module Development - bit.ly/dday14-tb5
  • 26. La «magia» della funzione theme() Drupal 7 Module Development - bit.ly/dday14-tb5
  • 27. A simplified wiring guide to the Theme Layer http://john.albin.net/drupal/arrays-of-doom
  • 29. THEME ENGINE E’ un insieme di script e file che interagiscono con il core del CMS ed interpretano il linguaggio di programmazione utilizzato nel tema
  • 30. THEME ENGINE A cosa serve un theme engine? E’ un modo comodo per stampare l’output temizzabile in vari file di template piuttosto che utilizzare del semplice PHP
  • 31. THEME ENGINE Aiuta a separare la funzionalità dalla presentazione Business logic - Presentation logic (separazione dei contesti)
  • 32. THEME ENGINE Drupal 7 => PHPtemplate Drupal 8 => Twig
  • 33. TEMPLATE ENGINE PHPTemplate HTML + PHP PHP for Themers su Drupalize.me https://drupalize.me/videos/php-themers?p=1146
  • 34. Il theme system di Drupal
  • 35. Il theme system di Drupal Un tema è un insieme strutturato di codice (al pari di un modulo) che fornisce degli strumenti atti a trasformare dei dati grezzi in un output strutturato e formattato.
  • 36. Il tema in PHPtemplate themename.info page.tpl.php *.tpl.php template.php (business logic) *.js *.css
  • 37. .info file Theme Metadata • Il nome del tema • La descrizione • La versione e le informazioni sulla compatibilità • Il template engine richiesto • I file CSS globali • I file JS globali • Le regioni incluse • Le impostazioni di default del tema • etc…
  • 38. .info file Theme Metadata • Nome macchina del tema deve essere unico, non lo stesso di qualche modulo • Preferibilmente solo lettere, quindi non mio_tema ma miotema (best practice)
  • 39. .info file Theme Metadata Regioni obbligatorie: regions[page_top] = Page Top regions[content] = Content regions[page_bottom] = Page Bottom Page Top e Page bottom sono regioni nascoste regions_hidden[] = region_name
  • 40. .info file Theme Metadata Writing theme .info files www.drupal.org/node/171205
  • 42. Template files *.tpl.php <?php if(_CONDITION_): ?> <p>Stampa questo!</p> <?php endif; ?> <?php print $stringa; ?> <?php print t($stringa_trad) ?> <?php hide($dopo); ?> <?php render($content); ?> <?php render($dopo); ?>
  • 43. Il tema in PHPtemplate themename.info page.tpl.php *.tpl.php template.php (business logic) *.js *.css
  • 44. Template files *.tpl.php html.tpl.php -> modules/system page.tpl.php -> modules/system region.tpl.php -> modules/system block.tpl.php -> modules/block node.tpl.php -> modules/node comment.tpl.php -> modules/comment field.tpl.php-> modules/field/theme RENDERING ORDER
  • 46. Template files *.tpl.php field.tpl.php viene utilizzato solo se sovrascritto nel tema, in caso contrario è utilizzata la relativa funzione di theming
  • 48. Il tema in PHPtemplate EXAMPLES
  • 49. La «magia» della funzione theme() Drupal 7 Module Development - bit.ly/dday14-tb5
  • 50.
  • 51. Template files *.tpl.php Solo print, render e if condition statements, nessuna logica <?php if(_CONDITION_): ?> <p>Stampa questo!</p> <?php endif; ?> <?php print $stringa; ?> <?php print t($stringa_trad) ?> <?php hide($dopo); ?> <?php render($content); ?> <?php render($dopo); ?>
  • 52. Le funzioni di preprocess e process Devono essere usate per tutte le operazioni di business logic necessarie a livello di presentation logic del nostro progetto Drupal
  • 53. Le funzioni di preprocess e process Consentono ad un modulo / tema di alterare o aggiungere variabili utilizzate da un altro modulo quando quest’ultimo chiama un theme hook
  • 54. Le funzioni di preprocess e process Ogni theme hook registrato tramite la funzione hook_theme() ha le sue funzioni di preprocess e process. Ogni file di template ha le sue funzione di preprocess / process
  • 55. Le funzioni di preprocess e process Tutte le funzioni di preprocess e process hanno la seguente forma: [module]_preprocess_[theme hook name](&$variables)
  • 56. Le funzioni di preprocess e process function mymodule_preprocess_item_list(&$variables) { // Add a class to the list wrapper. $variables['attributes']['class'][] = 'dday14'; }
  • 57. Preprocess functions 1. template_preprocess() 2. template_preprocesss_HOOK() 3. MODULE_preprocess() 4. MODULE_preprocess_HOOK() 5. THEME_preprocess() 6. THEME_preprocess_HOOK()
  • 58. Process functions 7. template_process() 8. template_processs_HOOK() 9. MODULE_process() 10. MODULE_process_HOOK() 11. THEME_process() 12. THEME_process_HOOK()
  • 59. Tempalte files *.tpl.php PREPROCESS EXAMPLES
  • 60. La «magia» della funzione theme() Drupal 7 Module Development - bit.ly/dday14-tb5
  • 61.
  • 62. Theme Hook Suggestions Le theme hook suggestions permettono di effettuare un override selettivo sia dei file di template che delle funzioni di theming
  • 63. Theme Hook Suggestions: BLOCK block--[region|[module|--delta]].tpl.php base template: block.tpl.php block--module--delta.tpl.php block--module.tpl.php block--region.tpl.php
  • 64. Theme Hook Suggestions • www.drupal.org/node/1089656 • bit.ly/dday14-tb6 (The Definitive Guide to Drupal 7 • Theme Hooks and Theme Hook Suggestions) $vars['theme_hook_suggestions'] ($mothership_poorthemers_helper)
  • 65. Theme Hook Suggestions SUGGESTIONS EXAMPLES
  • 67. Un po’ di codice Sporchiamoci le mani: • theme-settings.php • drush for themers
  • 68. L’angolo dei moduli • Devel (debugging with dsm()) • Devel Themer • Fences • Semantic Fields • Display suite

Hinweis der Redaktion

  1. COVER
  2. LAST SLIDE