The document is notes from a Drupal training course. It introduces some core Drupal concepts like nodes, users, and modules. It then provides an example of a birthday module that allows users to save their birthday, displays it on their profile, and shows a happy birthday message on their birthday. The module utilizes hooks, forms, blocks, and other Drupal APIs.
24. function mymodule_menu() {
$items['about'] = array(
'title' => 'About us',
'description' => 'A description of us.',
'page callback' => 'mymodule_about',
'access arguments' => array('access content'),
);
return $items;
}
Here are all my URLsâŠ
25. function mymodule_menu() {
$items['about'] = array(
'title' => 'About us',
'description' => 'A description of us.',
'page callback' => 'mymodule_about',
'access arguments' => array('access content'),
);
return $items;
}
Here are all my URLsâŠ
29. function mymodule_about($subpage = âlegalâ) {
if ($subpage == âlegalâ) {
return âWe donât have lawyers yet.â;
}
if ($subpage == âwelcomeâ) {
return âWelcome to our web site!â;
}
}
Iâll build the contents.
30. function mymodule_about($subpage = âlegalâ) {
if ($subpage == âlegalâ) {
return âWe donât have lawyers yet.â;
}
if ($subpage == âwelcomeâ) {
return âWelcome to our web site!â;
}
}
Iâll build the contents.
36. donât use a
hack. hook!
http://www.ïŹickr.com/photos/lanier67/185311136/
37.
38. A list of content types
A user is is being built
registering
A node is being Content is being
displayed searched
The menu is Links are being
being built displayed
The database is
being queried A comment is
being posted
A form is being
processed
54. /**
* Allows users to save their birthday,
* and congratulates them on the big day.
*/
function birthday_menu() {
$items['admin/settings/birthdays'] = array(
'title' => 'Birthday settings',
'description' => 'Settings for birthdays.',
'page callback' => 'birthday_page',
'access arguments' => array('administer users'),
);
return $items;
}
function birthday_page() {
return drupal_get_form('birthday_form');
}
55. /**
* Allows users to save their birthday,
* and congratulates them on the big day.
*/
function birthday_menu() {
$items['admin/settings/birthdays'] = array(
'title' => 'Birthday settings',
'description' => 'Settings for birthdays.',
'page callback' => 'birthday_page',
'access arguments' => array('administer users'),
);
return $items;
}
function birthday_page() {
return drupal_get_form('birthday_form');
}
56. function birthday_form() {
$form['birthday_age'] = array(
'#type' => 'checkbox',
'#title' => t(quot;Show users how old they are.quot;),
'#default_value' => variable_get('age', TRUE),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
return $form;
}
function birthday_form_submit($form, $form_state) {
$values = $form_state['values'];
variable_set('age', $values['birthday_age']);
drupal_set_message(t('Birthday settings saved.'));
}
57. function birthday_form() {
$form['birthday_age'] = array(
'#type' => 'checkbox',
'#title' => t(quot;Show users how old they are.quot;),
'#default_value' => variable_get('age', TRUE),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
return $form;
}
function birthday_form_submit($form, $form_state) {
$values = $form_state['values'];
variable_set('age', $values['birthday_age']);
drupal_set_message(t('Birthday settings saved.'));
}
67. drupal manages the page
http://www.ïŹickr.com/photos/thomashawk/2681744739
68. drupal manages the page
modules build the content
http://www.ïŹickr.com/photos/thomashawk/2681744739
69. drupal manages the page
modules build the content
modules also respond to events
http://www.ïŹickr.com/photos/thomashawk/2681744739
70. drupal manages the page
modules build the content
modules also respond to events
hack with hooks
http://www.ïŹickr.com/photos/thomashawk/2681744739
Core - bootstrap, route incoming page requests to modules
Modules - Build page content and respond to events
Many third-party modules, some build on top of other modules
Theme - Turn data from modules into HTML.
We’ll be concentrating on the module side of things today.
Core - bootstrap, route incoming page requests to modules
Modules - Build page content and respond to events
Many third-party modules, some build on top of other modules
Theme - Turn data from modules into HTML.
We’ll be concentrating on the module side of things today.
Core - bootstrap, route incoming page requests to modules
Modules - Build page content and respond to events
Many third-party modules, some build on top of other modules
Theme - Turn data from modules into HTML.
We’ll be concentrating on the module side of things today.
Core - bootstrap, route incoming page requests to modules
Modules - Build page content and respond to events
Many third-party modules, some build on top of other modules
Theme - Turn data from modules into HTML.
We’ll be concentrating on the module side of things today.
Core - bootstrap, route incoming page requests to modules
Modules - Build page content and respond to events
Many third-party modules, some build on top of other modules
Theme - Turn data from modules into HTML.
We’ll be concentrating on the module side of things today.
Core - bootstrap, route incoming page requests to modules
Modules - Build page content and respond to events
Many third-party modules, some build on top of other modules
Theme - Turn data from modules into HTML.
We’ll be concentrating on the module side of things today.
Core - bootstrap, route incoming page requests to modules
Modules - Build page content and respond to events
Many third-party modules, some build on top of other modules
Theme - Turn data from modules into HTML.
We’ll be concentrating on the module side of things today.
Core - bootstrap, route incoming page requests to modules
Modules - Build page content and respond to events
Many third-party modules, some build on top of other modules
Theme - Turn data from modules into HTML.
We’ll be concentrating on the module side of things today.
Core - bootstrap, route incoming page requests to modules
Modules - Build page content and respond to events
Many third-party modules, some build on top of other modules
Theme - Turn data from modules into HTML.
We’ll be concentrating on the module side of things today.
This is a module. Two files, A few lines of code.
In Drupal, that’s all you need to customize the behavior of the system and add new functionality.
A little more complicated. Shorter function, has an .install file.
This is a module, too. Views, one of Drupal’s most popular, contains over 400 files, over a meg of code and inline docs.
Don’t worry, there’s a middle ground.
We’re going to cover what they have in common.
This is a module, too. Views, one of Drupal’s most popular, contains over 400 files, over a meg of code and inline docs.
Don’t worry, there’s a middle ground.
We’re going to cover what they have in common.
Part 1: user with a web browser makes a request.
sends a URL, optionally a cookie.
http://www.mysite.com/articles/welcome.html
Apache intercepts the request.
Possibly routes it to other servers
What folder should I look in for foo.com?
Normally, it would then check /articles for a welcome.html file and send it back.
.htaccess turns foo.com/blah into foo.com/index.php?q=blah
...And PHP takes over.
Apache intercepts the request.
Possibly routes it to other servers
What folder should I look in for foo.com?
Normally, it would then check /articles for a welcome.html file and send it back.
.htaccess turns foo.com/blah into foo.com/index.php?q=blah
...And PHP takes over.
Apache intercepts the request.
Possibly routes it to other servers
What folder should I look in for foo.com?
Normally, it would then check /articles for a welcome.html file and send it back.
.htaccess turns foo.com/blah into foo.com/index.php?q=blah
...And PHP takes over.
Apache intercepts the request.
Possibly routes it to other servers
What folder should I look in for foo.com?
Normally, it would then check /articles for a welcome.html file and send it back.
.htaccess turns foo.com/blah into foo.com/index.php?q=blah
...And PHP takes over.
Bootstrap
All page requests go through index.php (front-side controller)
Bootstrap Drupal core, load essential includes
What site am I running? connect to database
Is there a cookie? Load the user.
Now, ask for menu items...
Bootstrap
All page requests go through index.php (front-side controller)
Bootstrap Drupal core, load essential includes
What site am I running? connect to database
Is there a cookie? Load the user.
Now, ask for menu items...
Bootstrap
All page requests go through index.php (front-side controller)
Bootstrap Drupal core, load essential includes
What site am I running? connect to database
Is there a cookie? Load the user.
Now, ask for menu items...
Bootstrap
All page requests go through index.php (front-side controller)
Bootstrap Drupal core, load essential includes
What site am I running? connect to database
Is there a cookie? Load the user.
Now, ask for menu items...
Bootstrap
All page requests go through index.php (front-side controller)
Bootstrap Drupal core, load essential includes
What site am I running? connect to database
Is there a cookie? Load the user.
Now, ask for menu items...
The module
Why, yes! Here’s a list of all the URLs I can handle.
Explain each line. See api.drupal.org
This is called a hook. We’ll get to this later.
The module
Why, yes! Here’s a list of all the URLs I can handle.
Explain each line. See api.drupal.org
This is called a hook. We’ll get to this later.
Traffic Cop
Is welcome.html an SEO-friendly alias? (about/welcome vs. welcome.html)
Find the best match. (about)
Run access checks.
Call the function, passing along unused pieces of the path
Grunt work
Module’s function gets called. Extra params passed along.
That’s how node module works -- ‘node’ plus a wildcard
Can do anything now: print out JSON and exit(), etc.
To build a normal page, build HTML and return it.
Grunt work
Module’s function gets called. Extra params passed along.
That’s how node module works -- ‘node’ plus a wildcard
Can do anything now: print out JSON and exit(), etc.
To build a normal page, build HTML and return it.
Presentation
Checks for 404, 403, and empty page.
index.php now has the contents of the page.
theme(‘page’, $content)
Sidebar blocks get loaded, the theme system is given a chance to add CSS and JS, etc.
Presentation
Checks for 404, 403, and empty page.
index.php now has the contents of the page.
theme(‘page’, $content)
Sidebar blocks get loaded, the theme system is given a chance to add CSS and JS, etc.
Presentation
Checks for 404, 403, and empty page.
index.php now has the contents of the page.
theme(‘page’, $content)
Sidebar blocks get loaded, the theme system is given a chance to add CSS and JS, etc.
Oh, look. Welcome.html!
Apache gets back the fully rendered HTML. Sweet.
Document gets fired back to the user.
The user’s browser renders it -- and they click on the next link and the process starts all over again.
Important detail: when drupal asked about menus and modules responded, we saw a hook.
hooks are everywhere: when pieces of content load, when a db query is run, when a user logs in…
that’s a drupal event. module_invoke_all(‘hook’) => for modules()… <name>_menu()
Modules can create their own hooks just by calling module_invoke_all()
As a Drupal page is built, events are firing nonstop.
As a Drupal page is built, events are firing nonstop.
As a Drupal page is built, events are firing nonstop.
As a Drupal page is built, events are firing nonstop.
As a Drupal page is built, events are firing nonstop.
As a Drupal page is built, events are firing nonstop.
As a Drupal page is built, events are firing nonstop.
As a Drupal page is built, events are firing nonstop.
As a Drupal page is built, events are firing nonstop.
Hooks can announce what’s going on
Allow modules to answer questions
Allow modules to extend functionality
Allow modules to change existing workflows
Hooks can announce what’s going on
Allow modules to answer questions
Allow modules to extend functionality
Allow modules to change existing workflows
Hooks can announce what’s going on
Allow modules to answer questions
Allow modules to extend functionality
Allow modules to change existing workflows
Hooks can announce what’s going on
Allow modules to answer questions
Allow modules to extend functionality
Allow modules to change existing workflows
Hooks can announce what’s going on
Allow modules to answer questions
Allow modules to extend functionality
Allow modules to change existing workflows
What does “hacking core” mean?
Why is it bad?
What does “hacking core” mean?
Why is it bad?