`
haohappy2
  • 浏览: 325848 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Drupal 6&7 Custom Template And Invoke Module

 
阅读更多
How to invoke a module block in the template?

<?php
$block
= module_invoke('module_name', 'block', 'view', 0);
print
$block['content'];
?>

How to create a module block?

 

Drupal has slightly changed its Theme API calls between Drupal 6 and Drupal 7. This small change can cause you (as it has me) some hours of grief at an apparently innocuous and undocumented feature.

the drupal 6 way

The old, Drupal 6 way of declaring a themable function was to do the following:

1
2
3
4
5
6
7
8
9
10
11
12
function mymodule_theme($existing, $type, $theme, $path) {
    return array(
        'mymodule_theme_function' => array(
            'template' => 'mymodule_theme_function',
            'arguments' => array(
                'argument_1' => NULL,
                'argument_2' => NULL,
                'argument_n' => NULL,
            )
        ),
    );
}

Throughout your code, when you wanted to call that theming function, you would call something like:

1
2
3
4
5
$themed_content = theme('mymodule_theme_function', 
        'Argument 1 Text Sample', 
        'Argument 2 Text', 
        'Argument 3 number or image'
    );

The declaration would go and look for the mymodule_theme_function.tpl.php in the same folder as your module and render the contents according to that template.

the drupal 7 way

Doing it in Drupal 7 has slightly changed the names and ordering of the parameters:

1
2
3
4
5
6
7
8
9
10
11
12
function my_d7_module_theme($existing, $type, $theme, $path) {
    return array(
        'my_d7_module_theme_function' => array(
            'template' => 'my_d7_module_theme_function',
            'variables' => array(
                'argument_1' => NULL,
                'argument_2' => NULL,
                'argument_n' => NULL,
            )
        ),
    );
}

The first note is that it's now called 'variables' in the hook_theme() override.  If you use the old name 'arguments', then you will likely see error messages come up in your Drupal log:

1
2
3
4
Notice: Undefined index: render element in theme() (line 811 of /var/www/includes/theme.inc)
Notice: Undefined variable: argument_1 in include() (line 10 of /var/www/sites/all/modules/my_d7_module/my_d7_module_theme_function.tpl.php)
Notice: Undefined variable: argument_2 in include() (line 13 of /var/www/sites/all/modules/my_d7_module/my_d7_module_theme_function.tpl.php)
Notice: Undefined variable: argument_n in include() (line 17 of /var/www/sites/all/modules/my_d7_module/my_d7_module_theme_function.tpl.php)

As such, they need to be referred to in an array key as variables in order to be passed to the final template.

You can attempt to then call this in a similar way to Drupal 6, such as:

1
2
3
4
5
$themed_content = theme('my_d7_module_theme_function', 
        'Argument 1 Text Sample', 
        'Argument 2 Text', 
        'Argument 3 number or image'
    );

The first thing you will notice is that you only get 'A' in your function for argument_1 and that the other arguments are null.

This is due to how the new, improved and Drupal 7 version of the theme() function needs to be called:

1
2
3
4
5
$themed_content = theme('my_d7_module_theme_function', array(
        "argument_1" => 'Argument 1 Text Sample',
        "argument_2" => 'Argument 2 Text',
        "argument_n" => 'Argument 3 number or image',
    ));

As you can see from the code above, the theme function takes an array as its second parameter. This array is a keyed array with the key being the name of the variable that was defined in the my_d7_module_theme(...) function and the value is the data that you want to assign to that variable and pass to the template.

分享到:
评论

相关推荐

    Drupal 8 Module Development 2nd Edition

    Write a Drupal 8 module with custom functionality and hook into various extension points Master numerous Drupal 8 sub-systems and APIs Model, store, and manipulate data in various ways and for various...

    Learning Drupal 6 Module Development

    Dive into Drupal module development as we create the Philosopher Biographies website, developing new modules and themes to meet practical goals. Create custom content types. Add AJAX functionality ...

    Drupal 6 Themes: Create new themes for your Drupal 6 site with clean layout and powerful CSS styling

    This book is a revised, updated and expanded edition of Drupal 5 Themes, written specifically for Drupal 6. The book will show you techniques and tools to help you improve the look and feel of any ...

    drupal 7 module development

    Drupal 7 模块开发是 Drupal 内容管理系统(CMS)中的一个重要环节,它允许开发者扩展 Drupal 的功能,以满足特定网站或应用的需求。Drupal 7 是 Drupal 的一个主要版本,发布于2011年,它引入了大量改进和新特性,...

    drupal6版本(这是drupal6)

    Drupal 6 是一个开源的内容管理系统(CMS),发布于2008年,是Drupal历史上的一个重要版本。这个版本引入了许多新功能和改进,为网站开发者提供了更强大的工具和更大的灵活性。在本文中,我们将深入探讨Drupal 6的...

    Drupal 6 JavaScript and JQuery

    ### Drupal 6 JavaScript and JQuery知识点总结 #### 一、书籍简介 《Drupal 6 JavaScript and JQuery》是由Matt Butcher编写的一本技术书籍,由Packt Publishing Limited出版社于2009年出版。本书旨在帮助读者了解...

    Learning Drupal 6 Module Development may 2008

    《Learning Drupal 6 Module Development may 2008》是一本专为 Drupal 6 开发者量身定制的指南,旨在帮助读者深入了解 Drupal 模块开发的各个环节。这本书的出版时间为2008年,那时 Drupal 6 是该开源CMS(内容管理...

    drupal8-links, Drupal 8链接&资源.zip

    drupal8-links, Drupal 8链接&资源 ##Drupal 8链接&资源对 fork &的贡献:)官方文档8更新以及如何帮助你。创建 Drupal 8.x 模块D7至D8升级教程: 裤子 MODULE写入 MODULE. info. yml文件( D

    Drupal 7 Module Development

    电子书 Drupal 7 Module Development

    drupal7与drupal6版本修改内容

    ### Drupal 7 与 Drupal 6 版本修改内容 #### 概述 Drupal 是一个开源的内容管理系统(CMS),被广泛用于构建各种类型的网站。从 Drupal 6 升级到 Drupal 7,Drupal 社区引入了大量的改进和新功能,以提高用户体验...

    Drupal.8.Development.Beginners.Guide.2nd.Edition.epub

    Whether you are new to Drupal or an experienced web developer, you will be able to master both basic configuration and advanced module development in Drupal through this book. What You Will Learn ...

    drupal 在线客服 module

    6. **自定义样式**:由于模块没有 CSS,可能需要使用 Drupal 的主题钩子或创建自定义 CSS 文件来调整客服小挂件的外观,使其符合网站的整体风格。 7. **测试和优化**:最后,一定要在实际环境中测试客服模块的功能...

    drupal6 mime 6.0

    7. **国际化支持**:Drupal 6 强调多语言支持,MimeMail模块也不例外。它可以轻松处理多语言邮件内容,适应全球化的站点需求。 8. **扩展和API**:MimeMail模块提供了一套API,允许开发者扩展其功能,如添加自定义...

    Drupal 6 Javascript And Jquery

    ### Drupal 6 JavaScript and jQuery:综合知识点解析 #### 核心概念与背景介绍 随着Web技术的不断发展,网站的功能越来越复杂。为了满足用户对交互性和动态效果的需求,开发者需要掌握更多的前端技术。Drupal 6...

    Drupal 6 JavaScript and jQuery.pdf

    尽管 Drupal 7 和 8 引入了更多的改进和现代技术,如Drupal 8中的React.js,但理解 Drupal 6 中的 JavaScript 和 jQuery 基础仍然是提升 Drupal 开发技能的重要步骤。对于那些仍在维护 Drupal 6 网站的开发者来说,...

    drupal6.X 系统最新的php系统适合大部分

    Drupal 6.x 版本是 Drupal 的一个重大版本,它在2008年发布,为用户提供了许多改进和新功能。然而,随着技术的不断进步,PHP 也在不断迭代更新,所以理解 Drupal 6.x 与最新 PHP 版本的兼容性至关重要。 标题中的...

    drupal7安装说明

    Drupal 7 是一个流行的开源内容管理系统,用于构建各种类型的网站。本教程将详细介绍如何在本地环境中安装 Drupal 7,特别是针对使用 Apache2、PHP5 和 MySQL5 的集成环境,如 AppServ。 首先,确保你已经安装了...

    Drupal 6 Panels Cookbook

    ### Drupal 6 Panels Cookbook #### 知识点详解 **一、Drupal 6 简介** Drupal 是一款开源的内容管理系统(CMS),以其强大的功能、灵活性以及社区支持而闻名。Drupal 6 是 Drupal 发展历史上的一个重要版本,在...

Global site tag (gtag.js) - Google Analytics