Drupal 8:如何移除内容类型视图中的字段标签与值
Hey there, let's tackle those annoying unwanted field labels (like the Date field) and hidden field values (like the "20" in your sort order) on your Drupal 8 site. I've got a couple of solid methods to fix this, depending on whether you prefer no-code UI tweaks or more custom solutions.
1. Remove Field Labels (e.g., the Date field label)
Method 1: Use the Content Type Display UI (No Code Needed)
This is the simplest way for most cases:
- Head to your Drupal admin panel, go to Structure > Content Types > [Your Target Content Type] > Manage Display
- Find the field you want to hide the label for (like Date), click the gear icon in its Operations column to open the field settings popup
- In the "Label" dropdown, select
, then click "Update" to save - Refresh your front-end page, and the field label will be gone
Method 2: Twig Template Override (For Custom Scenarios)
If you need to target specific pages/views or want more control, use a template override:
- First, enable Twig debugging to get the exact template name for your field (you can turn this on in
sites/default/services.ymlby settingtwig.config.debug: true) - For a Date field, the template will likely be something like
field--node--field-date.html.twig - Create this file in your active theme's
templatesdirectory, then modify it to remove the label section:
{# Remove field label, only render the field value #} <div{{ attributes }}> {% for item in items %} <div{{ item.attributes }}>{{ item.content }}</div> {% endfor %} </div>
- Clear Drupal's cache, and the change will take effect
2. Hide Field Content (e.g., the "20" in Sort Order)
Method 1: Use View/Content Display Settings (UI Approach)
If the field is in a View:
- Go to Structure > Views > [Your Target View] > Edit
- Locate the field you want to hide (like Sort Order), click the gear icon next to the field name
- Check the Exclude from display box, save the view, and the field content won't show up anymore
If it's part of the content type's default display:
- Go to Structure > Content Types > [Your Content Type] > Manage Display
- In the "Display" dropdown for the target field, select
, save your changes, and you're done
Method 2: Field Permissions Module (For Granular Control)
If you need to hide fields based on user roles or want more flexibility, use the field_permissions module:
- Install and enable the module via your admin panel or Drush (
drush en field_permissions -y) - Go to Structure > Content Types > [Your Content Type] > Manage Fields
- Click Edit on the field you want to control, switch to the Permissions tab
- Check Custom field permissions, then uncheck the "View field value" permission for roles that shouldn't see the field
- Save, and the field content will be hidden for those roles
Method 3: Quick CSS Hide (Temporary Solution)
If you just need a quick fix and don't mind the value still being in the page source:
- Right-click the field value in your browser, select "Inspect" to get its CSS selector (e.g.,
.field--name-field-sort-order .field__item) - Add this CSS to your theme's
style.cssortheme.cssfile:
.field--name-field-sort-order .field__item { display: none; }
- Clear your site cache, and the value will be hidden from view
内容的提问来源于stack exchange,提问作者helloworld123
相关产品推荐
相关产品推荐

