You are here

5 创建自己的预处理函数

admin 的头像
Submitted by admin on 星期五, 2015-05-29 02:06

作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com

  对于我们的这个模块来说,现在已经到了万事俱备只欠东风的阶段了。我们已经准备好了数据,现在我们需要在区块中将其显示出来。对于区块,我们首先想到的是区块的模板文件,下面是Drupal核心中自带的区块模板文件:

 

<div id="<?php print $block_html_id; ?>" class="<?php print $classes; ?>"<?php print $attributes; ?>>

 

  <?php print render($title_prefix); ?>

<?php if ($block->subject): ?>

  <h2<?php print $title_attributes; ?>><?php print $block->subject ?></h2>

<?php endif;?>

  <?php print render($title_suffix); ?>

 

  <div class="content"<?php print $content_attributes; ?>>

    <?php print $content ?>

  </div>

</div>

 

    在这里面,html代码片段中间,嵌套了一些PHP变量,这些变量是在哪里定义的呢?Drupal中模板中的变量通常都是定义在预处理函数中的。对于block.tpl.php,其变量来源于template_preprocesstemplate_preprocess_blocktemplate_process。其中template_preprocess_block是专门针对区块的预处理函数,其代码如下:

 

function template_preprocess_block(&$variables) {

  $block_counter = &drupal_static(__FUNCTION__, array());

  $variables['block'] = $variables['elements']['#block'];

  // All blocks get an independent counter for each region.

  if (!isset($block_counter[$variables['block']->region])) {

    $block_counter[$variables['block']->region] = 1;

  }

  // Same with zebra striping.

  $variables['block_zebra'] = ($block_counter[$variables['block']->region] % 2) ? 'odd' : 'even';

  $variables['block_id'] = $block_counter[$variables['block']->region]++;

 

  // Create the $content variable that templates expect.

  $variables['content'] = $variables['elements']['#children'];

 

  $variables['classes_array'][] = drupal_html_class('block-' . $variables['block']->module);

 

  $variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->region;

  $variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module;

  $variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module . '__' . $variables['block']->delta;

 

  // Create a valid HTML ID and make sure it is unique.

  $variables['block_html_id'] = drupal_html_id('block-' . $variables['block']->module . '-' . $variables['block']->delta);

}

    在这个函数中定义了变量blockblock_zebrablock_idcontentclasses_arrayblock_html_id,同时还定义了区块的模板建议theme_hook_suggestions。但是这里面并没有为我们定义“更多链接”。可以在我们的模块中实现区块的预处理函数,为block变量追加一个morelink属性:

 

/**

 * 为block变量添加morelink属性

 * @see block.tpl.php

 */

function block_morelink_preprocess_block(&$variables) {

  $result = db_query("SELECT url, title FROM {block_morelink} WHERE module = :module AND delta = :delta", array(

    ':module' => $variables['block']->module,

    ':delta' => $variables['block']->delta,

  ))->fetch();

  $morelink_url = empty($result)?'':$result->url;

  $morelink_title = empty($result)?'':$result->title;

  $variables['block']->morelink = '<span class="block-more-link">' . l(t('More'), $morelink_url, array('attributes' => array('title' => $morelink_title))). '</span>';

}

    在这段代码中,我们首先取出来了“更多链接”的url,title。然后使用l()函数构建了一个更多链接。将block.tpl.php模板文件复制到了themes\bartik\templates目录下面,并编辑里面的代码,以输出更多链接:

 

<div id="<?php print $block_html_id; ?>" class="<?php print $classes; ?>"<?php print $attributes; ?>>

 

  <?php print render($title_prefix); ?>

<?php if ($block->subject): ?>

  <h2<?php print $title_attributes; ?>><?php print $block->subject ?></h2>

<?php endif;?>

  <?php print render($title_suffix); ?>

 

  <div class="content"<?php print $content_attributes; ?>>

    <?php print $content ?>

  </div>

<?php if ($block->morelink): ?>

    <?php print $block->morelink ?>

  <?php endif;?>

</div>

 

    这样,我们配置一下搜索表单区块,输入一个测试用的更多链接urltitle。保存区块,回到区块的显示页面,我们就会看到一个更多链接。

 

                          1.png

                           图2-3搜索表单多了一个更多链接

 

    模块写到这里,功能基本上就完成了,如果是一个实际的项目,现在就可以将其应用于在线站点了。但是作为模块开发中的一个练习来讲,我们还需要进一步的来完善这个模块,使其具有较强的可配置性和可定制性。

 


Drupal版本: