欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal 设置隐藏字段

shiping1 的头像

来自 http://addoa.com/blog/how-make-optionally-hide-node-titles-drupal-using-cck

 

log

How to Make Optionally Hidden Node Titles in Drupal (Using CCK)

How to Make Optionally Hidden Node Titles in Drupal (Using CCK)

 

In the popular open-source content management system (CMS) Drupal, titles display on every page as part of the Content Management System's functionality. But what if you don't want to have the titles display on some pages? For example, if you want to use a banner instead of the plain title. There's an easy way to make Drupal's node titles optional using the Content Construction Kit (CCK) module.6.x

Presumably you already have Drupal installed if you're reading this. If not, check out Drupal. It's a powerful CMS that is has a great community of developers. Many sites from well-known companies are powered by Drupal. Even the White House's website is powered by Drupal!

What you'll need to get started:

  • Drupal 6.x (something similar should work with 5.x, but I haven't tested it)
  • Content Construction Kit (CCK) for Drupal 6.
  • NOTE: Most of CCK's functionality has been moved to core in Drupal 7, so downloading the CCK module won't be necessary for Drupal 7 once it's released.

If you don't already have CCK installed, download and install it to your Drupal site now.

Step 1 — Finding content types settings
Once you've installed CCK, go to the "content types" section of Drupal admin (found at /admin/content/types).

Admin Panel
Select "content types" in the Drupal admin panel.

Step 2 — Managing a content type's fields
Select "manage fields" for the content type you wish to edit.

Admin Panel
Select "manage fields" for the content type you wish to edit.

Step 3 — Adding a new field
Add a new field to the content type. To do this drag the "new field" row to the position you want and fill in the field information. You'll probably want to drag your new field to right below the title field. You can use whatever label and field name you want, but we use "Title hide" and "titlehide" and we'll refer back to this later. The type should be set to "text" and the form element should be set to "Single on/off checkbox." This field will be used to specify if you want the node's title to be displayed or hidden. Once you're done, click "save."

Admin Panel
Add the field that will be used to specify whether or not to hide the title.

Step 4 — Defining field's allowed values
After you press save, you'll be brought to a new page where you can specify more detailed settings for the field. Here you need only change one setting: the "allowed values list." Enter "display|Display Title" on the first line and "hide|Hide Title" on the second line. These values will be referred to later. Once you've entered this, scroll down to the bottom of the page and save the field settings.

Admin Panel
Complete the allowed values list as it is above.

Once you save, you'll see your new field along with the others.

Admin Panel
Your content type's fields, including the new field you just created

Step 5 — Hiding the new field
Because this field will be used internally to hide or show the title of a page, you don't want this field to display in the node view. To hide the display of this field in the node view, click on the "display fields" tab. Once the display fields settings have loaded, change the label, teaser, and full node display to "" and then save.

Admin Panel
The display fields settings.

Step 5 — Grab a cup of coffee (tea?)... you're almost done

Awesome — you're "title hide" field is all set up (but you're not done yet)! If you go to add a new node of that content type, you'll see you can now tick the box to hide the title.

Admin Panel
You'll now see the title hide option when you add or edit nodes of that content type.

Step 6 — Bring the field to life
In order to make your new title hide field useful, you need to build it into your theme. To do this, I added some PHP code that will insert the value of the field as a CSS class of the title.

Step 7 — Identify the theme file that displays the node's title on the page
Identify the theme file that displays the node's title on the page. For most themes the theme file that is responsible for displaying the title is called page.tpl.php.

Step 8 — Find the PHP code that displays the page's title
Locate the PHP code that displays the title:

<?php print $title; ?>

It may be surrounded by additional related HTML and PHP that define layout, styles, or in which cases the title should load:

<?php if ($title): ?>
<div id="title"><h1><?php print $title; ?></h1></div>
<?php endif; ?>

Step 9 — Insert your title hide field as a class of the title's H1 tag
Insert your title hide field as a class of the H1 tag. This will look something like this:

<?php if ($title): ?>
<div id="title"><h1 class="title <?php if(!empty($node->field_titlehide[0]['value'])) {$titlehide = $node->field_titlehide[0]['value']; print $titlehide;} ?>"><?php print $title; ?></h1></div>
<?php endif; ?>

Step 10 — Add the CSS class that hides the title
Hiding the h1 tag is based on CSS style rules. Add a CSS rule like the following anywhere in your theme's stylesheets:

h1.hide{
display: none;
}

Step 11 — Finito
Congratulations! You now have optional Drupal node titles.

Troubleshooting

Nothing seems to be happening...
Hiding the h1 tag is based on CSS style rules. Did you remember to add a CSS style rule like the following in order to hide your title?

h1.hide{
display: none;
}

This style can be added anywhere to any of your site's CSS stylesheets.

The CSS class isn't being added to the title
Here are a few suggestions...

  • First try clearing your theme's cache! If Drupal is running on old, cached theme files, changes might not take effect.
  • The php code is based on calling the right CCK variable. You need to make sure you named your CCK field the same as I did or if you named it differently, you need to make those adjustments in my code. You also want to make sure you used the same CCK field type that I used, a checkbox, or you may not be calling the contents of the variable properly.
  • If you did everything right and the variable is still not being written as a CSS class to the H1 element, there's probably a issue with your theme accessing and displaying the CCK field using PHP variables. I'd recommend using the Devel contributed module in order to see what variables are being loaded and in order to help diagnose theme problems.

Comments

 

I read your post about "How to Make Optionally Hide Node Titles in Drupal (Using CCK)" and I evaluated it very interesting but unfortunately it don't work for me.

I have made all the change in drupal and i think that the problem is in my page.tpl.php.

Can you see my code (if i understand where is the problem i can share your metod with italian drupal community)?

Isaac's picture

My theme is a custom theme based off the Zen starter theme, although I don't see why it wouldn't work with your theme.

To make sure the proper CCK variable is available to the template, I recommend installing the Devel module. Once you install it, you can view all of the template variables that are loading (including CCK template variables). You'll be able to see the value of the CCK field you're using to switch the title hide on and off and then verify that the php code you're using is actually calling the variable to load.

Try this and let me know how it goes.

Simon's picture

Thanks, that's awesome!

I've been scratching my head on this small problem for a while!

You're tute works just perfect.

One question however: my Title Hide field offers a third (and default) radio box saying "N/A". How do i get rid of it?

Thanks again!

Simon

 

普通分类: