0

我有一个块,我想在某个区域激活它,并为他设置只能在给定节点中看到的条件。如何在 Drupal 7 中以编程方式执行此操作?

4

2 回答 2

3

如果您想在更新挂钩中使用, 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。

于 2013-03-07T23:56:29.470 回答
2

我能够通过使用以下代码来实现这一点。

$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);
于 2011-04-03T06:04:49.140 回答