Wednesday
Jun172009

« Drupal: Easing Content Type Proliferation »

In this blog post we will show you how to allow content editors to edit similar but different "pages" in Drupal, all based on a shared content type without confusion, by hiding the unnecessary form elements.

There can be times when you wish to create several similar content types that only differ slightly from one another (perhaps by a field or two). Let’s take a website that posts environmental warnings on different types of pollution as an example. The site could report air pollution, water pollution and ground pollution warnings. One option is to create 3 separate content types, one for each type of pollution. Air pollution may have an air quality index field, while ground pollution may have a field that shows how it affects the property value of nearby buildings. The point here is that most of the fields for all 3 content types are the same. Discovery Date, Detail Review, Severity, and so on. An alternative implementation is to use one content type called Pollution and a field in that content type named ‘Pollution_type’. This field in the content type would allow us the flexibility to know what type of pollution we are dealing with.

The problem comes when an environmental investigator wishes to add a new air pollution warning. They will see a Drupal form with a ground pollution area field, land value field and all the other fields which are not related to the air pollution type. This can make the user interface crowded with unrelated fields, and unusable. So how can we have one content type and one form which adapts to the type of pollution a user has selected? 

Simple.

By overriding the node_form.tpl file in the themes directory for the ‘pollution’ node type, we can take full control of the appearance of our form. We can then add some JavaScript, or better yet JQuery, which will hide and show elements based on which review type the user has chosen.

Step 1: Set Up CCK Fields

To set this example up we need a simple CCK content type with the following fields (please see Drupal documentation about how to create CCK fields):

Review_ type : drop down with default fields: ( 1 – air, 2 – ground, 3 – water)
hazard_time (text box)
area_affected (text box)
warning_date (date)
warning_details (text area)

etc ...

Step 2: Override the Node Form for the Review Content Type

To do so you need to create a function in the template.php file (which will be found in your themes directory). The name of the function will need to take on the following form:

phptemplate_[content_type]_node_form() and in our case the function will look like the following:

function phptemplate_pollution_node_form($form) {
    drupal_add_js('sites/all/modules/js/pollution.js');  

    // Add JQuery to act on the form
    // load the review_node_form.tpl.php file to render this 
    //form.   
    return _phptemplate_callback('pollution_node_form',
    array('form' => $form));  
}

The reason why this works is because when Drupal processes the form for a particular node that has been queued up to render, it will search through the template.php file for a function in the form of phtemplate_[content_type]_node_form: If it finds one it will then load and render the form returned by this function.

Step 3: Render a Custom Form

In the themes directory you need to create the form theme function that is returned by the function in the template.php function. In our example this would be pollution_node_form.tpl.php

Now in the pollution_node_form.tpl.php file we create the HTML and attributes we need to render the form. This includes any tags and classes that will help us when we use JQuery to hide and show the form.

<?php>
<legend> Type And Title </legend>

<?php echo drupal_render($form['field_pollution_type']); ?>
    <?php echo drupal_render($form['title']); ?>
<div id=’warning’>
    <?php echo drupal_render($form['field_warning]); ?>
</div>
<div id= 'land_pollute_field’>
    <?php echo drupal_render($form['field_land_area_affected
]); ?>
</div>
<div id= ‘air_pollute_field’>
    <?php echo drupal_render($form['field_direction_of_
movement]); ?>
</div>

// render other field from the CCK content type ..

<?php>

    // Unset form elements that you don’t want rendered
        unset($form['attachments']);
        unset($form['menu']);
    …
     // Render the rest of the form …
        echo drupal_render($form);

?>

Now to get the JQuery to connect with the HTML id’s of each separate div we add the following JQuery file to the JavaScipt file referred to in Step 2. In our example the file should be named warning.js and be in the /themes/yourtheme/js directory.

$(document).ready( function() {

    // Need to initialize on page load which fields are shown on the page.
    $("#land_pollute_field").css("display","block");
    $("#water_pollute_field").css("display","none");
    $("#air_pollute_field").css("display","none");

    var choice = $("#edit-field-pollution-type").val();  
// You will need to find the ‘id’ of the dropdown list 
    switch(choice) {

        case "land":
            $("#land_pollute_field").css("display","block");
            $("#water_pollute_field").css("display","none");
            $("#air_pollute_field").css("display","none");
        break;
        case "air": 
            
            $("#water_pollute_field").css("display","none");
            $("#land_pollute_field").css("display","none");
            $("#air_pollute_field").css("display","block");

        break;
        case "water":

            $("#land_pollute_field").css("display","none");
            $("#water_pollute_field").css("display","block");
            $("#air_pollute_field").css("display","none");

        break;
});

From here its easy to expand the form and manipulate the layout as needed.

Reader Comments (9)

-Breitling Avenger Seawolf watches uk sale In the themes directory you need to create the form theme function that is returned by

That's very nice, and I like it.spooks dvd series 9

November 14, 2011 | Unregistered Commenterspooks dvd series 9

Individual-level consortia relationships like this are usually strong. Bio-empathy can be a plat-form for long-term strategic planning; They prey on your fear that you’ll “miss out on something good” and cheap uggs for sale claim to have “inside information” that isn’t public knowledge! Their pre-eminence is refl? It may look like a pretty scary mass of numbers to someone who is not used to cheap uggs for sale things but let me explain, In the RIM frame-work. The cheap uggs Times of discount uggs September revealed that UFJ board memberswere undertaking a campaign to convince shareholders they should back thedeal; cult to cheap uggs with a reason why you do a certain thing!

November 22, 2011 | Unregistered Commenteruggs outlet

No matter what type of uggs on sale

you are looking for, there is no doubt one available for you. They have come a long way from

their days of being in World War I aircrafts. And, thanks to their manufacturer, ugg boots outlet Australia, you can

feel good while wearing these shoes while still being in style! You will simply love the

options in color, styles, and designs that you have. The whole family will be wearing ugg outlet because they just are so

darn cute and comfortable!The ugg boots

sale is one of the a lot of accepted types of cossack on the bazaar appropriate now.

Anyone who is not accustomed with them should accede bottomward one on! They are appreciably

bendable and luxurious. They action an accomplished akin of abundance and a admirable

appearance as well. They are actual balmy and cozy. Your anxiety will be in shoe heaven! Let's

yield a afterpiece attending at what is accessible in the ugg boots clearance and uggs for cheap see if we can't

acquisition something that you will enjoy!

November 26, 2011 | Unregistered Commenterugg boots sale

ugg bailey button triplet is really a legendary make of sheepskin boots. Apparently simple-minded cartoon popular in Asia and europe, however the shape of the world, and it is now a fad popular on the planet the wind.GG uses the greatest quality leathers and suedes, not to mention, the earth's finest Grade "A" Merino sheepskin. bailey button triplet ugg United kingdom uses just the greatest-grade sheepskin available. Twin-face sheepskin can be used in a number of our core items. A bit of twin-face sheepskin continues to be treated on the fleece side, and also the skin side, supplying the soft comfort ugg bailey button triplet boots Australia is known for.Boots adopted within world additionally to continents thwart I as aircraft pilots used these lower as high as lined air uggs generally referred to as Uggs Clearance. Uggs are applied consequently of slightly substantial understanding sheepskin referred to as 'twin faced' this specific energy solved on from both edges inner together with outer. This notable gear items trunk to exhale and wick falling in dried out ft.Comfortable altering design also enables variations of ugg bailey button triplet sale , inside a couple of years aboard the main worldwide magazines, so fur boots craze is sweeping the planet.

December 12, 2011 | Unregistered Commenterugg bailey button triplet

Refer the bcbg max azria dresses , tt is have lots styles,for example: Herve Leger bustier dress from your Herve Leger collections are inspired by previous designs and outdoors influences, because its designes without strap, using the bust top only,you don't need to make more adornments, the sexy already showing directly, that's why it's welcome by all women in each and every periods of favor.If you wish to show its fashion capabilities, to provide yourself any 72 alter, this Herve Leger Single Strap is a superb choice! Unique cloud styles, creating any noble sense of puzzle. Simple, good, Star Supporter, even when merely a idea drill side nail furnishings, Hevre leger is furthermore the popularity within the OL since easily setup the look within the increase regarding women.Women can't do with no beautiful dress, Herve Leger Dresses will assist you to show the wonder and have the time happiness. The sexy. This women at Herve Leger Sale looked very charming and sexy.

December 12, 2011 | Unregistered Commenterherve leger

En ting at huske er at v?lge en klassisk stil Aviator Canada Goose Jakke , sandsynligvis i sort eller m?rk brun, for at sikre, at denne canada goose trillium publish er lige s? alsidig som det kan v?re. Valg af et Aviator jakke, der er blevet-overdesigned eller v?lge en i en lys farve vil begr?nse, hvad du kan b?re det mediterranean, og vil klart se ud som du f?lger en trend, der begr?nser dets holdbarhed og g?re det mere af en impuls k?b finish en investering i en vigtig brik til din garderobe.Motorcykel jakken har sin egen stil af canada goose danmark opg?relse, der adskiller sig meget i forhold til andre Canada Goose Jakker . Motorcykel jakke er af to typer, living room ene som er almindeligt ses p? banen hedder stykke og living room canada goose trillium anden 2011 Canada Goose Kids Parka kombineret af l?der buks og sko. Det Canada Goose Banff Parka Biker foretr?kker l?der motorcykel Canada Goose Outlet generelt designet have metalplettering sammen mediterranean dens tilbeh?r for vagt din krop.Males du v?lger at b?re din Aviator jakke i denne s?boy, vil det tilf?je et strejf af catwalken til ethvert outfit og sikre, 2011 Canada Goose Kids Parka at du er forberedt p? det kolde vejr og husk at du ikke beh?ver at bruge en formue p? Canada Goose Danmark living room seneste trend, og det vil holde search langt ud i slutningen af ??denne vinter.

December 12, 2011 | Unregistered CommenterCanada Goose Jakker

<h1>discount designer bags</h1> cheap and fashion
<h1>designer inspired handbags</h1> Acclaimed
<h1>air max 2011</h1> high quality
<h1>nike shox tl3</h1> wholesale nike
<h1>women puma shoes</h1> Particular style
<h1>air max tn</h1> Different
<h1>puma shoes</h1> Very famous
<h1>puma shoes online</h1> Popular brands
<h1>women timberland boots</h1> A great feeling
<h1>wholesale gucci shoes</h1> Online Sales

December 19, 2011 | Unregistered Commenterdsadsa

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>