Documente Academic
Documente Profesional
Documente Cultură
1. /**
2. * Implements hook_menu().
3. */
4. function MYMODULE_menu() {
5.
$items = array (http://www.php.net/array)();
6.
7.
$items['admin/content/frontpage'] = array (http://www.php.net/array)(
8.
'title' => 'Front page content',
9.
'description' => 'Custom fields for front page content.',
10.
'page callback' => 'drupal_get_form',
11.
'page arguments' => array (http://www.php.net/array)('MYMODULE_front_page_co
12.
'access arguments' => array (http://www.php.net/array)('administer front pag
13.
);
14.
15.
return $items;
16. }
This creates a nice menu link that can be found under Content->Front page content. We should
also create a permission for editing the front page content.
1. /**
2. * Implements hook_permission().
3. */
4. function MYMODULE_permission() {
5.
return array (http://www.php.net/array)(
6.
'administer front page content' => array (http://www.php.net/array)(
7.
'title' => t('Administer front page content'),
8.
'description' => t('Modify front page content.'),
9.
),
1 of 2
10.
);
11. }
Now we need to create our actual form using Drupals lovely form API.
1. /**
2. * Callback function for editing front page content.
3. */
4. function MYMODULE_front_page_content($form_state) {
5.
6.
$form['front_page_title'] = array (http://www.php.net/array)(
7.
'#title' => t('Front page title'),
8.
'#type' => 'textfield',
9.
'#default_value' => variable_get('front_page_title', ''),
10.
);
11.
12.
$form['front_page_subtitle'] = array (http://www.php.net/array)(
13.
'#title' => t('Front page sub-title'),
14.
'#type' => 'textfield',
15.
'#default_value' => variable_get('front_page_subtitle', ''),
16.
);
17.
18.
return system_settings_form($form);
19. }
This sets up our simple form and by using system_settings_form it will save all of our fields to the
variable table in the database.
Now that we have all of our data stored we need to setup a way to display it on the front page. We
could create blocks with the content or we could print them out directly in our theme template file.
I normally create blocks but for this example I will just be showing you how to print it out in a
template file. First we need to pass the data into the template file and we will do that by adding
some code to the template.php file in our theme.
1. function MYTHEME_preprocess_page(&$variables) {
2.
3.
if (drupal_is_front_page()) {
4.
$variables['front_page_title'] = variable_get('front_page_title', '');
5.
$variables['front_page_subtitle'] = variable_get('front_page_subtitle',
6.
}
7.
8. }
We can now print out the data in our page--front.tpl.php file wherever we like.
1. <?php print $front_page_title; ?>
2. <?php print $front_page_subtitle; ?>
This might seem like a lot of work to do what Drupals block system already does, but I find the
client has an easier time understanding this and once you do it a couple of times it doesnt take
very long to write.
2 of 2