步骤8)修改查询

我们应该需要修改“reent_posts_contents()”函数。
从模块设置表单中检索数据“使用变量()”并将限制添加到数据库查询。
修改后的'reent_posts_contents()的功能看起来像下面。

/**
 * Custom content function. 
 * 
 * Set beginning and end dates, retrieve the recent posts from the database
 */
function recent_posts_contents(){
    //Get today's date.
    $today = getdate();
    //Calculate the date a week ago.
    $start_time = mktime(0, 0, 0,$today['mon'],($today['mday'] - 7), $today['year']);
    //Get all posts from one week ago to the present.
    $end_time = time();

    //NEW LINE 
    $limit = variable_get('latest_posts_limit', 3);

    //Use Database API to retrieve recent posts.
    $query = db_select('node', 'n')
      ->fields('n', array('nid', 'title', 'created'))
      ->condition('status', 1) //Published.
      ->condition('created', array($start_time, $end_time), 'BETWEEN')
      ->orderBy('created', 'DESC') //Most recent first.
      ->range(0, $limit) //NEW LINE
      ->execute();
    return $query;  
}

测试

转到配置»性能,然后单击“清除所有缓存”按钮。

转到“模块列表”页面=>选中复选框以启用模块=>单击“保存配置”。
我们将看到配置链接以及模块。

单击“配置”链接,我们将被重定向到模块设置页面。
我们可以设置要在块中显示的帖子的最大数量。

基于本教程,我们创建了一个名为lodel_posts的Drupal自定义模块。

步骤4)检索数据

现在我们将创建自定义函数来从数据库中检索新创建的帖子。
'reent_posts_contents()'函数检索了上周内创建的帖子。
'netword_posts_contents()'功能将是以下内容。

/**
 * Custom content function. 
 * 
 * Set beginning and end dates, retrieve the recent posts from the database
 */
function recent_posts_contents(){
    //Get today's date.
    $today = getdate();
    //Calculate the date a week ago.
    $start_time = mktime(0, 0, 0,$today['mon'],($today['mday'] - 7), $today['year']);
    //Get all posts from one week ago to the present.
    $end_time = time();

    //Use Database API to retrieve recent posts.
    $query = db_select('node', 'n')
      ->fields('n', array('nid', 'title', 'created'))
      ->condition('status', 1) //Published.
      ->condition('created', array($start_time, $end_time), 'BETWEEN')
      ->orderBy('created', 'DESC') //Most recent first.
      ->execute();
    return $query;  
}

高级功能:

现在我们将实现一些高级功能,使自定义模块更灵活。
我们将创建一个配置表单,其中管理员能够调整要显示的链接数量。

第3步)声明块

'hook_block_info()'用于定义块。
在“lodate_posts.module”文件中创建“lovely_posts_block_info()”函数并放置以下代码。

/**
 * Implements hook_block_info().
 */
function latest_posts_block_info() {
    $blocks['latest_posts'] = array(
        //The name that will appear in the block list.
        'info' => t('Latest posts'),
        //Default setting.
        'cache' => DRUPAL_CACHE_PER_ROLE,
    );
    return $blocks;
}

测试
转到“模块列表”页面=>选中复选框以启用模块=>单击“保存配置”。

导航到结构»块,向下滚动到列表的底部。
我们将在“已禁用”部分下会看到“最新帖子”块。

返回模块列表页面=>取消选中该复选框以禁用Modum =>单击“保存配置”。

步骤6)准备模块配置表单

“使用hook_menu()',我们将定义我们的表单。
在“lodatile_posts.module”文件中定义'natull_posts_menu()'函数,并添加以下代码。

/**
 * Implements hook_menu().
 */
function latest_posts_menu() {
    $items = array();

    $items['admin/config/content/latest_posts'] = array(
      'title' => 'Latest posts',
      'description' => 'Configuration for Latest posts module',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('latest_posts_form'),
      'access arguments' => array('access administration pages'),
      'type' => MENU_NORMAL_ITEM,
    );

    return $items;
}

标题:必填。
菜单项的标题。

描述:菜单项的描述。

页面回调:在用户访问路径时调用显示网页的函数。

页面参数:要传递给页面回调函数的参数数组。

访问参数:传递给Access回调函数的一组参数。

类型:类型常量是menu_normal_item,menu_callback,menu_suggested_item,menu_local_action,menu_local_task,menu_default_local_task。

通过在“lodatile_posts.info”文件中添加以下行来在模块列表中添加配置按钮。

; NEW LINE	
configure = admin/config/content/latest_posts

步骤1)创建模块文件夹和文件

为模块选择一个短名称。
模块名称必须以字母开头,并且它必须只包含小写字母和下划线。
对于此示例,我们将选择“nullate_posts”作为模块名称。

在“站点/全部/模块/”目录中创建具有模块名称(namull_posts)的文件夹。

在模块目录中创建一个'namest_posts.info'文件('sites /全部/modules/nullum_posts /'),用于告诉Drupal关于模块。
'letply_posts.info'文件包含模块的元信息,模块需要它。
缺少此文件的模块将不会出现在模块列表中。

'/全部/模块/nullum_posts/nullat_posts.info'文件包含以下代码:

name = Latest Posts
description = A block module that lists links to newly created posts developed by onitroad.
core = 7.x

将模块文件('namull_posts.module')创建到模块目录('sites /全部/modules/nullum_posts /')中。

  • 模块文件具有模块目录名称的名称和.module的扩展名。
  • 模块文件是PHP文件,并以打开PHP标记开头。
  • 省略关闭(?>)标记,以避免某些服务器上的运行时问题。

目前,“/全部/模块/nullum_posts/nullum_posts.module'文件包含以下代码:

<?php

测试
转到模块列表页面并滚动列表,我们将在其他类别下看到最新的帖子模块。

步骤5)生成块内容

使用'hook_block_view'我们将拍摄自定义函数生成的数据('next_posts_contents()')并转换为块内容。
'reent_posts_block_view()'将是以下内容。

/**
 * Implements hook_block_view().
 * 
 * Prepares the contents of the block.
 */
function latest_posts_block_view($delta = '') {
    switch ($delta) {
        case 'latest_posts':
            $block['subject'] = t('Latest posts');
            if (user_access('access content')) {
                //Use our custom function to retrieve data.
                $result = recent_posts_contents();
                //Array to contain items for the block to render.
                $items = array();
                //Iterate over the resultset and format as links.
                foreach ($result as $node) {
                  $items[] = array(
                    'data' => l($node->title, 'node/' . $node->nid),
                  ); 
                }
                //No content in the last week.
                if (empty($items)) {
                    $block['content'] = t('No posts available.');  
                } 
                else {
                    //Pass data through theme function.
                    $block['content'] = theme('item_list', array(
                      'items' => $items));
                }
          }
        return $block;
    }
}

测试

转到“模块列表”页面=>选中复选框以启用模块=>单击“保存配置”。

导航到结构»块并向下滚动到列表的底部。
从“已禁用部分”将“最新帖子”块位置设置为页面区域,然后单击“保存块”。

如果选择“最新帖子”块的“侧列第一”区域,我们将看到左侧列杆的最新帖子列表。

祝贺!我们已成功创建基本的Drupal自定义模块。

第2步)声明帮助钩

在“lodate_posts.module”文件中实现一些钩子。
此外,对模块如何在注释中工作的文档是一个非常好的主意。

/**
 * @file
 * A block module that displays latest posts.
 */

实现帮助钩(Hook_help)。
此挂钩提供有关我们模块的帮助和其他信息。
在“lodate_posts.module”文件中创建一个名为'namult_posts_help()'的函数并放置以下代码。

/**
 * Implements hook_help().
 *
 * Displays help and module information.
 */
function latest_posts_help($path, $arg) {
    switch ($path) {
        case "admin/help#latest_posts":
            return '<p>' . t("Displays links to newly created posts developed by onitroad") . '</p>';
            break;
    }
}

测试
转到“模块列表”页面=>选中复选框以启用模块=>单击“保存配置”。

模块会出现帮助链接。
帮助链接将重定向到模块帮助页面。

返回模块列表页面=>取消选中该复选框以禁用Modum =>单击“保存配置”。

第7步)声明表单

根据“页面回调”,请在请求链接时调用“drupal_get_form”,并将“页面参数”值传递给该函数。
通过分配“最新_posts_form”,我们将其定义为表单ID和将创建配置表单的函数的名称。

创建一个名为'namuly_posts_form()'的函数,并通过将元素添加到'$form'数组来构建表单。
将以下代码添加到“lodate_posts.module”文件中。

/**
 * Page callback: Latest posts settings
 *
 * @see latest_posts_form()
 */
function latest_posts_form($form, &$form_state) {
    $form['latest_posts_limit'] = array(
      '#type' => 'textfield',
      '#title' => t('Maximum number of posts'),
      '#default_value' => variable_get('latest_posts_limit', 3),
      '#size' => 2,
      '#maxlength' => 2,
      '#description' => t('The maximum number of links to display in the block.'),
      '#required' => TRUE,
    );

    return system_settings_form($form);
}
Drupal自定义模块开发教程

该模块有助于扩展和自定义Drupal功能。
Drupal有三种模块,核心,贡献和定制。
在本教程中,我们可以在Drupal 7中学习如何在Drupal 7中创建自己的模块。

日期:2020-06-02 22:18:57 来源:oir作者:oir