0
我有一個塊,我想在某個區域激活它,並且爲他設置僅在給定節點中才能看到的條件。我怎樣才能在Drupal 7中以編程方式執行此操作?如何在Drupal 7中以編程方式激活和設置塊的條件?
我有一個塊,我想在某個區域激活它,並且爲他設置僅在給定節點中才能看到的條件。我怎樣才能在Drupal 7中以編程方式執行此操作?如何在Drupal 7中以編程方式激活和設置塊的條件?
我能夠通過使用下面的代碼來實現這一點。
$menu_block = array(
'module' => 'menu',
'delta' => 'IDBLOCK', // the id of the block
'theme' => 'MYTHEME', // the current theme
'visibility' => 1, // it is displayed only on those pages listed in $block->pages.
'region' => 'menu',
'status' => 1,
'pages' => '', // display the menu only for these pages
);
drupal_write_record('block', $menu_block);
drupal_write_record如果您想在更新掛鉤中使用,則不起作用。如果您正在更新或創建數據庫條目,您當然也可以使用db_update或db_insert。下面是一個示例更新:
<?php
// find your block id, for me $bid = 38
db_update('block')
->fields(array(
'module' => 'system',
'delta' => 'main-menu', // block delta, find in database or module that defines it
'theme' => 'mytheme', // theme to configure
'visibility' => BLOCK_VISIBILITY_NOTLISTED, // see drupal constants
'region' => 'main_menu', // region declared in theme
'status' => 1,
'pages' => '',
)
)
->condition('bid', $bid, '=')
->execute();
?>
有關參數的更多詳細信息,請參閱hook_block_info api。