Last updated March 25, 2011. Created by Drupal_is_amazing on March 6, 2009.
Edited by silverwing. Log in to edit this page.
Introduction
I thought it appropriate, just in case it's ever helpful, to try to document my experience learning drupal, so that others following the same path may find the journey easier.
Install and initial customizing
I initially installed drupal 6 on a linux/mysql/php server. Installation went smoothly. Using the Modules panel, I disabled the features I didn't want-- forums and comments, for example, weren't going to have a place on my site yet. I also disabled the right sidebar (and moved the user panel over to the left sidebar).
Initial Theme
Since I was adapted a pre-existing static site, I wanted a theme that would let me keep preserve some of the formatting of the old site's content. I therefore selected as a theme and went with Minnelli, a fork of the famous Garland theme. It seemed nice, uncluttered, and it was fixed width. I chose a color scheme I liked, and I was off!
However, my old site's content width and the Minnelli themes width weren't the same, so I had to alter the theme files to account to change the width.
A custom content type
The first thing I wanted to do with Drupal was set up one of the content types from my old site-- text interviews. The format of interviews is very simple-- a few simple fields like "interviewee's name" and "interviewee's email address" and "interviewee's website". Lastly, there is one large textarea field where the interview takes place.
To do this, I installed the Content Construction Kit, created a new content type, and added all the appropriate fields. I also added the Email module to add an email field to the CCK, and the Link module to add a link field.
Within a short amount of time, I was able to insert pre-existing interview into the site, and everything worked like a charm. The process was substantially easier than I had expected-- for example, on my old static site, I had to convert plain text to into html paragraphs by adding paragraph tags by hand-- but, ta-da, drupal now does this for me, automatically.
A Custom Template
At this point, the data for a single interview WAS in drupal-- but when viewing the interview, it was just a straightforward dump of all the field data-- not very pretty. Now, CSS alone could have gone along way towards fixing this, but CSS wizardry isn't really my bag.
What I wanted was a very simple way to customize the output of my interviews, without having to go through and learn all about the drupal code. Fortunately, there was an easy tool that was just what the doctor ordered-- the Content Templates (ConTemplates) module. I installed it, and then quickly and easily created a template that displayed interviews the way I wanted them.
Housekeeping
Now fully convinced that Drupal is the solution for me, I turned to doing some housekeeping that I had neglected thus far.
My webserver doesn't support cron, so I installed the PoorMansCron module instead.
The default drupal icon didn't really fit, so I replaced it with a nice icon of my own. I also took the opportunity to add a custom favicon that was used on my old static site.
Adding interviews menu page
Next, I wanted to create a menu page that would show a list of all the interviews. For this I installed the Views module and enabled it. That allowed me to create a custom view page. I added in the fields I wanted to display, ordering records by date. I selected grid style, and then added some css to my theme to customize the look of the menu page. I assigned the menu page a path and added a link to it to the "primary links" menu on the sidebar.
A Custom Module: Hello World
At this point, I started to dive in to actual development. I created a new module named "hello_world". I made the relevant "modules/hello_world" directory, and uploaded to short text files.
The first, hello_world.info, essentially just tells Drupal about the existence of the module.
The second, hello_world.module, is a php file with two functions.
hello_world_menu() is a function that essentially says to drupal: "When the user goes to http://example.com/?q=hello , call the function hello_world_page()."
hello_world_page() is a function that actually creates the content on the page. In this case, it just returns a string that says "Hello World"-- pretty simple.
<?php
function hello_world_menu() {
$items['hello'] = array(
'title' => 'Hello world page...', // page title
'page callback' => 'hello_world_page', // callback function name
'access callback' => TRUE, // every user can look at generated page
'type' => MENU_CALLBACK // define type of menu item as callback
);
return $items;
}
function hello_world_page() {
return '<p>Hello world!</p>';
}
?>After uploading, I enable the module in the modules admin panel, clear the site's cache, and point a browser to http://www.example.com/?q=hello . Like magic, the site response with a page saying "Hello World".
Now, I know how to get Drupal to execute an arbitrary piece of php code whenever a given url is request, and how to use that code to return a page to the user. Huzzahs are in order.
My Very Own Form!
Now, the whole point of being able to execute php code is to be able to receive input from the user and then return output based on that input. So, it's time to dive into forms.
First, I create a function that will "create" the form-- that is, when the function is called, it will construct a $form object that correctly describes the structure of the form.
<?php
function test_form($form_state) {
// define a text field named 'favfood'
$form['favfood'] = array(
'#type' => 'textfield',
'#title' => t('My Favorite Food is:'),
'#default_value' => $object['favfood'],
'#size' => 60,
'#maxlength' => 64,
'#description' => t('Enter your all-time favorite food.'),
);
// define a submit button
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit this form right now, Computer!!!'),
);
return $form; // return the newly-created form object
}
?>And then, we re-write the hello world function to, instead of just print out "hello world", instead tell drupal to get the form created by the above function.
<?php
function hello_world_page() {
return drupal_get_form('test_form');
}
?>Form output
Based on instruction at http://drupal.org/node/336447 , I next altered the form so that it can act on the input from the user. We rewrite the form creation function
<?php
function test_form($form_values = null) {
$form = array(
'#redirect' => false, // stops the form redirecting to itself after validation
'#multistep' => true, // Passes the form values back through as $form_values
);
if (!isset($form_values)) {
// There wasn't a form submitted, so show the blank form
$form['favfood'] = array(
'#type' => 'textfield',
'#title' => t('My Favorite Food is'),
'#default_value' => $object['favfood'],
'#size' => 60,
'#maxlength' => 64,
'#description' => t('Enter your all-time favorite food.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit this form right now, Computer!!!'),
);
}
else {
// Form has been submitted - so display the results
$markup = 'I also love '.$form_values['post']['favfood'].'!!!!';
$form['result'] = array( '#value' => $markup, '#type' => 'markup' );
}
return $form;
}
?>Using Form Input to create a new node
At some point, I don't want to simply have a user type in information only to receive a response. Eventually, I want to be able to actually use that information to help the user create a new content node.
The Steps Go Like this:
1. User comes to a given url,
2. User is presented with a form asking some basic information.
3. User enters information and clicks submit.
4. User is presented with the "add node" form (on the "add node page"), where the relevant fields have been pre-populated with data based on what was provided by the user in Step 2.
For me, the trick to learning how to do this was to examine the Prepopulate module and play around with how it is able to do something very similar. (Although it uses GET data encoded in the url, rather than POST data submitted by a form.)
After some fiddling, the trick is this:
* Have the initial form submit its data to the node/add/NameOfMyContentType
* Implement a hook_form_alter() function. Drupal will call this function in order to give it a chance to modify forms. When it is called, it will look at the data stored in $_POST and then alter the "add node" form so as to prepopulate the relevant fields with the appropriate data.
The precise part of $form that in which to store the data will vary based on how your "add node" form is setup. But the code that worked for my field was this:
<?php
function MyModule_form_alter(&$form, $form_state, $form_id) {
if (($form_id=='NameOfMyContentType_node_form') && (isset($_POST['NameOfMyInitialFormElement']))) {
$form['field_NameOfMyCCKField'][0]['#default_value']['value']=$_POST['NameOfMyInitialFormElement'];
}
}
?>Getting rid of the "Menu Settings" field on the "add content node"
Another issue presents itself-- when I add new content , it always has a pesky "Menu Settings" group that I don't want to have to ever look at again. Strictly speaking, only admins will see the "Menu Settings" field-- but if I know ahead of time that a particular content type is never going to be in a menu, why should I ever have to look at it again?
Unfortunately, CCK can't remove "Menu Settings", and it can't seem to move it around either. Fortunately, there is a module called "ctm" / "Menu Settings per Content Type" that gets the trick done-- it lets you decide what menus a particular content type can be put in-- and if there aren't any menus to pick from, it will hide the "Menu Settings" fields entirely.
When I first enabled the "Menu Settings per Content Type" module, it didn't work. Turns out, the problem was it was trying to remove the "Menu Settings" fields before they had finished being added.
So I installed the "Module Weights" module, which allows for easy editing of module weights. I then assigned the "Menu Settings per Content Type" module an arbitrarily large positive weight (20 in my case, chosen for no particularly good reason). At this point, everything worked like a charm-- "Menu Settings" disappeared and stayed disappeared.
Still to discuss
* Adding images
path
custom page type and adding nodetype to tpl for css
flexifilter
Still to do
* pretty paths
* auto pretty paths
* taxonomy
* hide login box from most pages
* copy static content into pages
Derived field values
* Custom "add node" page with Javascript or a custom PHP form that autopopulates a an add-node page upon submission?
Comments
Thank you very much. Good
Thank you very much.
Good Presentation.
Werbeagentur Berlin