Posts in the “drupal” category

A Drupal form textarea example

Drupal textarea FAQ: How do I create an HTML textarea in a Drupal form (Drupal 6 or Drupal 7)?

Assuming that you're comfortable with creating a Drupal form, you can create a Drupal form textarea as easily as this:

# an html textarea for our drupal form
$form['life_story'] = array(
  '#title' => t('Your Life Story'),
  '#type' => 'textarea',
);

That code creates an HTML textarea with the label "Your Life Story". The textarea is specified with this option in your form declaration:

A Drupal form checkboxes example (HTML check boxes)

Drupal form FAQ: How do I create an HTML checkbox in a Drupal form?

Assuming that you're comfortable with creating a Drupal form, you can create a Drupal form checkbox (technically a Drupal checkboxes element) as easily as this:

A Drupal form radio buttons example (Drupal radios field)

Drupal radio buttons FAQ: How do I create an HTML radio button widget in a Drupal form?

Assuming that you're already comfortable with creating a Drupal form, you can create Drupal form radio buttons (technically a Drupal "radios" element) like this:

A Drupal form hidden field example

Drupal form FAQ: How do I create an HTML hidden field in a Drupal form (a Drupal 6 or Drupal 7 form)?

Assuming that you're comfortable with creating a Drupal form, you can create a Drupal form hidden field like this:

# an html hidden field for our drupal form
$form['wizard_page'] = array(
  '#type' => 'hidden',
  '#value' => 2,
);

The "type=hidden" declaration in this Drupal form array element is what tells Drupal that we want to create a hidden form field with the given property.

A Drupal form weight field example

Drupal form FAQ: Is there a special way I should create a Drupal weight field in a Drupal form (a Drupal 6 or Drupal 7 form)?

Drupal has a concept of "weight" that is used in various places, including the Taxonomy and Book modules, and to help support that concept, the Drupal Form API has a "weight" form element.

Assuming that you're comfortable with creating a Drupal form, you can create a Drupal form weight element like this:

A Drupal date field form example

Drupal form FAQ: How can I create a date field in a Drupal form (using Drupal 6 or Drupal 7)?

Assuming that you're already comfortable with creating a Drupal form, you can create a Drupal date field widget like this:

Drupal form password field examples

Drupal form FAQ: How do I create an HTML password field in a Drupal form (using Drupal 6 or Drupal 7)?

Assuming that you're comfortable with creating a Drupal form in general, you can create a Drupal form password field like this:

$form['password'] = array(
  '#type' => 'password',
  '#title' => t('Password'),
);

Here's what this simple, default Drupal form password field looks like:

A Drupal form submit button and submit handler function example

Drupal form submit button and handler FAQ: How do I create a Drupal form with a Drupal submit button? Also, how do I create a Drupal form submission handler function?

Assuming that you're comfortable with creating a Drupal form, you can add a submit button to your Drupal form very easily, like this:

A Drupal form custom HTML DIV CSS wrapper example

Drupal form custom HTML FAQ: How can I wrap some custom HTML around a Drupal form, like placing a wrapper DIV tag around my form so I can add some custom CSS to style my form/div?

You can wrap an HTML DIV tag around your Drupal form by using the form prefix and suffix properties, like this:

Drupal form textfield required syntax

Drupal form textfield FAQ: What is the syntax to make a Drupal textfield required?

Just use the Drupal form #required property, like this:

$form['first_name'] = array(
  '#title' => t('First Name'),
  '#type' => 'textfield',
  '#required' => TRUE,
);

In that example the Drupal form textfield is made required by setting the "#required" tag equal to TRUE.

 

Drupal form textfield default value syntax

Drupal form textfield FAQ: How do I set the default value for a textfield in a Drupal form?

Just use the Drupal form "#default_value" tag, like this:

$form['first_name'] = array(
  '#title' => t('First Name'),
  '#type' => 'textfield',
  '#default_value' => 'John',
);

Here's an example of setting the default value of a Drupal textfield to a variable instead of a static text string:

Drupal form textfield help text (description) syntax

Drupal form textfield FAQ: How do I set the "help" text for a Drupal form textfield?

The help text that you normally see with a Drupal textfield comes from the Drupal form "#description" attribute. Here's a quick example:

The Drupal form field required syntax

Drupal form fields FAQ: What is the syntax to make a Drupal form field (textfield, textarea, password field) required?

To make a Drupal form field required, just use the Drupal form #required attribute, as shown in this required textfield example:

$form['first_name'] = array(
  '#title' => t('First Name'),
  '#type' => 'textfield',
  '#required' => TRUE,
);

As you can see, to set a Drupal form field to be required, you just need to add the #required form attribute, and set it to TRUE.

 

The Drupal fieldset initially collapsed syntax

Drupal fieldset FAQ: How do I make a Drupal fieldset initially collapsed?

To make a Drupal fieldset collapsed initially, make the fieldset collapsible, then set the "collapsed" property TRUE, like this:

The Drupal fieldset collapsible syntax

Drupal fieldset FAQ: What is the syntax to make a Drupal fieldset (form fieldset) collapsible?

To make a Drupal form fieldset collapsible, just add the "#collapsible" attribute to your Drupal form definition, like this:

A Drupal fieldset example (Drupal form group)

Drupal form fieldset FAQ: How do I build a Drupal form using one or more fieldset areas to group my form elements into logical components (like 'shipping address', 'billing address', 'credit card information', and so on)?

As inferred by the question, you group Drupal form elements using the Drupal fieldset element. You define a fieldset, then add form elements to that fieldset using a simple sub-array syntax. For example, in the following Drupal source code example I define a fieldset using an array like this:

A Drupal Views tutorial

This is a tutorial on how to create a view using the Drupal Views module. In this example I'll be creating a simple view of the Drupal 'comments' database table. I'll demonstrate how to create this viewas a Drupal node with an associated URL and menu item, so when you're done you'll be able to access it at a URL like:

Drupal canonical meta tags for pager URLs

As a quick note, I was just about to write a Drupal 6 module to add "rel=canonical" meta tags to Drupal pager pages (pager URLs), so URLs that look like this:

http://www.devdaily.com/blog?page=47

or this:

Drupal CCK module - How to hide the Body field

Some times you just have to laugh at yourself ... I was just struggling to figure out how to hide the Body field on a new CCK form, and just as I was thinking I might have to resort to some jQuery magic, I finally read the help text under the Body field on the CCK "Submission Form Settings":

To omit the body field for this content type, remove any text and leave this field blank.