- Drupal 6 Theming Cookbook
- Karthik Kumar
- 534字
- 2021-04-13 16:56:43
Thus far in this chapter, we have looked at controlling block visibility based on the path of the page and the role of the user. In this recipe, we will look to configure a block to be displayed based on the node type of the content on the page.
We will be configuring the Recent comments block—which is provided by the Comment module—to only be visible for the story
and blog
node types. While the story
type is enabled by default, the blog
type is created only upon enabling the Blog module from the module administration page at admin/build/modules
(Home | Administer | Site building | Modules).
It is assumed that both the Blog and Comment modules have been enabled, and sample nodes and associated comments have been created.
Block visibility can be configured from the block's configuration page as per the following steps:
- Navigate to
admin/build/block
(Home | Administer | Site building | Blocks). - Look for the block which needs to be configured—Recent Comments—and click on the Configure link next to it.
- Navigate down to the Page specific visibility settings section.
- Select Show if the following PHP code returns TRUE and enter the following code in the Pages textarea below:
<?php // Array of allowed types. $types = array('blog', 'story'); // Check if the current page is a node. if ((arg(0) == 'node') && is_numeric(arg(1))) { $node = node_load(arg(1)); return in_array($node->type, $types); } ?>
- Click on the Save block button to save the changes.
- Back on the block administration page, ensure that the block has been enabled and added to one of the current theme's available regions such as the left or right sidebar.
Since we have chosen to control block visibility using PHP, Drupal evaluates the code within the Pages text area and only displays the block if it returns the value TRUE
.
The variable $types
contains the list of allowed node types. This list is populated with the internal names of each node type which can be accessed via the Content types page at admin/content/types
(Home | Administer | Content | Content types).

In the previous screenshot, the internal names of each type are listed under the column titled Type.
Since blocks are not restricted to only be visible on node pages, we need to ascertain if the page that is currently being viewed is a node page. We do this by checking the path to see if it matches the pattern node/node-id
. In other words, we only consider displaying the block if the path involves the word node followed by the node ID, an integer.
Once we have ensured that we are viewing a node, we need to get Drupal to load the node using the node_load()
function so that we can check if its type matches one of the types that we have allowed.
If the type of the node being viewed is in this list of allowed types, then the in_array()
call returns TRUE
and the block is displayed. Else, it returns FALSE
which leads to the block not being displayed.
- vtiger CRM Beginner's Guide
- AI繪畫教程:Midjourney關鍵詞靈感手冊
- 中文版CorelDRAW X7基礎培訓教程(移動學習版)
- Pro/E Wildfire 5.0中文版入門、精通與實戰
- 新媒體美工一冊通(全彩)
- AutoCAD 2017從新手到高手
- 三維建模與3D打印從入門到精通
- 中文版Photoshop CC基礎培訓教程
- Photoshop CC2017圖像處理實例教程
- After Effects印象 影視高級特效光影篇
- Premiere Pro基礎與實戰教程
- Photoshop CC圖像處理案例教程(第2版)
- 動畫制作基礎(項目教學版)
- AutoCAD 2024室內設計從入門到精通(升級版)
- Creo快速入門教程(Creo 8.0中文版)