0

I need to create a site that is editable for the client. The site is pretty basic: home, about, contact, etc. On each page there are different blocks of text. Like on my homepage, I have different sections like a basic text, "3 reasons" boxes, etc.

I have a content type "pages" with the title and the basic text, which is fine. But how do I create the 3 boxes? Do I create a custom content type for them too? If so, how do I add them on the front page with the text as well?

I heard a bit about the Views module. Would it be useful in my case?

1 Answer 1

1

If you are using D6, you need to install the cck module to add custom fields to nodes.

But, if you are using D7, cck has been migrated into drupal core. You can easily create custom fields for each content type at the path admin/structure/types/manage/{content-type-name}/fields. For your page content type, go to admin/structure/types/manage/page/fields.

I think your case is not concerned with the views module. You can generally check what is views here.

AFAIK, there is no module to create different custom fields per node. We can only create custom fields per content type.
If you are sure you have static numbers of page ( such as page, about, etc. ) and if you will not allow your client to add a new page content, it is possible to create and display different fields upon NID.
You will need to write hook_preprocess_field in your theme template.php and hook_form_alter in your custom module to show/hide different field upon NID.
Here are some example code snippet.

In your theme template.php,

function yourtheme_preprocess_field(&$vars){
    $element = $vars['element'];
    // let's say nid=1 is for your home page
    if($element['#object']->nid == 1){ 
        // hide custom fields for the other node 2
        if( in_array($element['#field_name'], array('field_1_for_nid_2', 'field_2_for_nid_2')) ){
            $vars['classes_array'][] = 'invisible'; // make display:none in theme css file for the class .invisible
        }
    }
    // let's say nid=2 is for your about page
    elseif($element['#object']->nid == 2){ // do same things for nid=2
        // hide custom fields for the other node 1
        if( in_array($element['#field_name'], array('field_1_for_nid_1', 'field_2_for_nid_1')) ){
            $vars['classes_array'][] = 'invisible'; // make display:none in theme css file for the class .invisible
        }
    }
    // and so on.   
}

In your custom module yourmodule.module,

function yourmodule_form_alter(&$form, &$form_state, $form_id){

    if($form_id == 'page_node_form'){ // for the content type 'page'
        $nid = arg(1);
        if($nid == 1){ // when your client edit the page "home", let's say it is nid=1
            // hide custom fields for the other node 2
            $form['field_1_for_nid_2']['#required'] = FALSE;
            $form['field_1_for_nid_2']['#type'] = 'hidden';

            $form['field_2_for_nid_2']['#required'] = FALSE;
            $form['field_2_for_nid_2']['#type'] = 'hidden';         
        }
        elseif($nid == 2){ // when your client edit the page "about", let's say it is nid=2
            // hide custom fields for the other node 1
            $form['field_1_for_nid_1']['#required'] = FALSE;
            $form['field_1_for_nid_1']['#type'] = 'hidden';

            $form['field_2_for_nid_1']['#required'] = FALSE;
            $form['field_2_for_nid_1']['#type'] = 'hidden';         
        }
        // and so on
    }

}

If you don't know about how to write custom module, here is a good start point.
If you don't want custom coding, the best way is to create different content types with different fields for your pages.

6
  • Yes but I dont have all the same fields on each page. Can I create custom field specific to a node id instead of a whole node type?
    – allaire
    Aug 21, 2012 at 13:13
  • You need display only, right? I mean to show different custom fields on each page.
    – Sithu
    Aug 21, 2012 at 15:31
  • The client should also be able to edit these specific fields per page too
    – allaire
    Aug 21, 2012 at 21:24
  • It is ok if you are sure you have static number of pages such as home ( nid=1 ), about ( nid=2 ), etc and you will not let your client to add new pages. You will need to write hook in your custom module. I will update my answer.
    – Sithu
    Aug 22, 2012 at 4:01
  • Alright, I'll give this a try. Weird there is no better option available. Anyway, thanks for your time!
    – allaire
    Aug 22, 2012 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.