简体   繁体   English

Drupal 7.如何通过template.php更改自定义块中的内容?

[英]Drupal 7. How I change content in custom block via template.php?

I created simple block in admin page with HTML content and now I need to modify this content via template.php. 我在管理页面中使用HTML内容创建了一个简单的块,现在我需要通过template.php修改此内容。 What need for this? 这有什么需要?

You should name your .tpl file based on your block ID. 您应该根据块ID命名.tpl文件。

For example: 例如:

If you've created block with an ID 如果您使用ID创建了代码块

block-block-1

then the template name should be 则模板名称应为

block--block--1.tpl.php

Main steps: 主要步骤:

  • Identify your block ID 识别您的区块ID
  • Go on your theme folder and find folder with template files 转到主题文件夹,然后找到包含模板文件的文件夹
  • Copy block.tpl.php in your theme's templates folder and rename it into name based on block's ID (block--block--1.tpl.php in this case) 复制主题模板文件夹中的block.tpl.php并根据模块的ID将其重命名为名称(在这种情况下为block-block--1.tpl.php)
  • Flush all caches 刷新所有缓存

NOTE : When you're editing your custom block you can see in URL the path with block's delta. 注意 :编辑自定义块时,您可以在URL中看到带有块增量的路径。 It will be something like 它会像

/admin/structure/block/manage/block/5/configure

For this URL, you should use block--block--5.tpl.php. 对于此URL,应使用block--block--5.tpl.php。 The number 5 is block's delta in this case. 在这种情况下,数字5是块的增量。 You can also view the source of the page where your block appears (or use Firebug to inspect) and look for the ID given to the block. 您还可以查看出现块的页面的源(或使用Firebug进行检查),并查找赋予该块的ID。

You can use hook_block_view_MODULE_DELTA_alter() to alter the content of a block inside template.php . 您可以使用hook_block_view_MODULE_DELTA_alter()来更改template.php中一个块的内容。 This way, you can add logic to decide the content. 这样,您可以添加逻辑来确定内容。

/**
 * Implements hook_block_view_MODULE_DELTA_alter().
 *
 * It is better to get the module and delta for block using dpm($block) by
 * implementing hook_block_view_alter() before.
 */
function mytheme_block_view_block_1_alter(&$data, $block) {
  if (some_criteria_met_on_which_you_wanted_to_change_content) {
    $data['content'] = t('This is how I altered the content of the block-1 inside template.php.');
  }
}

在此处输入图片说明

In the above picture, you can see module = block and delta = 1 . 在上图中,您可以看到module = blockdelta = 1 Therefore, the hook is mytheme_block_view_block_1_alter() . 因此,该钩子是mytheme_block_view_block_1_alter()

Note: 注意:

  • MODULE - The name of the module that defined the block. MODULE-定义块的模块的名称。

  • DELTA - The unique identifier for the block within that module, as defined in hook_block_info(). DELTA-该模块内块的唯一标识符,如hook_block_info()中所定义。

for queries, use comments. 对于查询,请使用注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM