drupal全部都是节点_构建一个Drupal 7模块:显示最新节点

drupal全部都是节点

Drupal is one of the most popular open source content management systems. It has a lot of functionality built in and thousands of free and paid modules to choose from for your site. It has a very extensible and flexible architecture which lets you build on top of the Drupal core and extend its functionality as needed. Drupal lets you plug into its system using Drupal hooks. We are going to use some of the hooks in the module which we are going to build in this article – a Drupal 7 module to show the different types of nodes (all content on a Drupal website is stored and treated as nodes) on your Drupal site by creating a Drupal block. The types of nodes which will be shown can be selected in the configuration menu of the module.

Drupal是最受欢迎的开源内容管理系统之一。 它内置了许多功能,并为您的站点提供了数千个免费和付费模块供您选择。 它具有非常可扩展和灵活的体系结构,可让您在Drupal核心之上构建并根据需要扩展其功能。 Drupal允许您使用Drupal挂钩插入其系统。 我们将在本文中要使用的模块中使用一些挂钩– Drupal 7模块,以显示Drupal上不同类型的节点(Drupal网站上的所有内容都存储并视为节点 )。通过创建Drupal块创建站点。 可以在模块的配置菜单中选择将显示的节点类型。

Before we begin, if you don't have an active Drupal installation on your machine, please set one up by following the instructions in their installation guide.

开始之前,如果您的计算机上没有有效的Drupal安装,请按照其安装指南中的说明进行设置

构建模块的基本结构 (Building your module's basic structure)

Let's start by creating the basic folder structure and files for our module. A Drupal module needs to be placed in a specific directory in your Drupal installation and it needs to have some basic files so that it can be identified as a module by the Drupal system. The first thing we need to do is create a folder for our module called shownodes. That folder should be kept in the sites\all\modules\ folder of your Drupal installation. In this folder create two blank files called shownodes.info and shownodes.module.

让我们开始为模块创建基本的文件夹结构和文件。 Drupal模块需要放置在Drupal安装的特定目录中,并且需要具有一些基本文件,以便Drupal系统可以将其标识为模块。 我们需要做的第一件事是为我们的模块创建一个文件夹,名为shownodes 。 该文件夹应保存在Drupal安装的sites\all\modules\文件夹中。 在此文件夹中,创建两个空白文件,分别称为shownodes.infoshownodes.module

The file shownodes.info provides the general information about your module to Drupal and the file shownodes.module will contain the implementation for various hooks used by our module. The following is how the file structure should look for our module.

文件shownodes.info向Drupal提供了有关您的模块的常规信息,并且文件shownodes.module将包含模块使用的各种挂钩的实现。 以下是我们模块的文件结构外观。

image01

Now open the shownodes.info file and add the following content:

现在打开shownodes.info文件并添加以下内容:

name = shownodes
description = This module displays various nodes in a block on your Drupal site.
core = 7.x

This basically describes your module's basic properties like its name, description and on which Drupal core level it can be deployed. Once you have added these details to the shownodes.info file you should be able to see your module in the module list as shown below. You can enable your module now.

这基本上描述了模块的基本属性,例如模块的名称,描述以及可在其上部署的Drupal核心级别。 将这些详细信息添加到shownodes.info文件后,您应该能够在模块列表中看到您的模块,如下所示。 您可以立即启用模块。

alt

了解Drupal挂钩 (Understanding Drupal Hooks)

The system to extend Drupal is built completely on the concept of hooks. Hooks are based on naming. For a module to implement a hook in Drupal, it has to create a function called modulename_hookname depending on which hook it needs to implement in the .module file. Once there is an event to call the hooks, the Drupal system will call the hook implementation of all the enabled modules. To read more about the available hooks visit the hooks documentation page.

扩展Drupal的系统完全基于钩子的概念。 挂钩基于命名。 为了使模块在Drupal中实现钩子,它必须创建一个名为modulename_hookname的函数,具体取决于它需要在.module文件中实现哪个钩子。 一旦有事件调用该挂钩,Drupal系统将调用所有已启用模块的挂钩实现。 要了解有关可用钩子的更多信息,请访问钩子文档页面

The first hook we will implement in our module is the hook_help which lets you provide help text on your module to describe what your module does.

我们将在模块中实现的第一个钩子是hook_help,它使您可以在模块上提供帮助文本以描述模块的功能。

Add the following code to your shownodes.module file

将以下代码添加到您的shownodes.module文件中

<?php
/**
 * @file
 * This is the main module file.
 */

/**
 * Implements hook_help().
 */
function shownodes_help($path, $arg) {

  if ($path == 'admin/help#shownodes') {
    $output = '<h3>' . t('About') . '</h3>';
    $output .= '<p>' . t('The shownodes module allows the Administrator to display various types of nodes as a block on your Drupal site.') . '</p>';
    return $output;
  }
}

We are implementing the hook_help hook through a function called shownodes_help which returns the help text for our module whenever the help request is fired. Once you have implemented this hook the help option will be shown on the module and clicking it will show the help as follows.

我们正在通过一个名为shownodes_help的函数来实现hook_help挂钩,该函数在触发帮助请求时返回模块的帮助文本。 一旦实现了此挂钩,帮助选项将显示在模块上,单击该选项将显示帮助,如下所示。

alt

向模块添加配置选项 (Adding configuration options to your Module)

Now let's add some configuration options for the administrator of our site. We will create an options page for the Administrator so that he can select which node types he wants to display in the block. This can be done using the Drupal menu hooks. We will implement the hook_menu hook in our shownodes.module like so:

现在让我们为网站管理员添加一些配置选项。 我们将为管理员创建一个选项页面,以便管理员可以选择要在块中显示的节点类型。 这可以使用Drupal菜单挂钩来完成。 我们将实施hook_menu钩在我们shownodes.module像这样:

/**
* Implementation of hook_menu().
*/
function shownodes_menu() {

  $items['admin/config/shownodes'] = array(
  'title' => t('Choose the nodes to show'),
  'description' => t('You can select which node types you want to show'),
  'page callback' => 'system_admin_menu_block_page',
  'access arguments' => array('administer site configuration'),
  'file' => 'system.admin.inc',
  'file path' => drupal_get_path('module', 'system'),
  );
  
  $items['admin/config/shownodes/settings'] = array(
  'title' => t('Choose the nodes to show'),
  'description' => t('You can select which node types you want to show'),
  'page callback' => 'drupal_get_form',
  'page arguments' => array('shownodes_admin_settings'),
  'access arguments' => array('administer site configuration'),
  'type' => MENU_NORMAL_ITEM,
  );
  
  return $items;
}

function shownodes_admin_settings() {

  $types = node_type_get_types();
  foreach($types as $node_type) {
    $nodetypes[$node_type->type] = $node_type->name;
  }

  $form['shownodes_nodes_toshow'] = array(
  '#type' => 'checkboxes',
  '#title' => t('Select the nodes to show'),
  '#options' => $nodetypes,
  '#default_value' => variable_get('shownodes_nodes_toshow', array('')),
  '#description' => t('All the node types selected below will be shown'),
  );
  
  return system_settings_form($form);
}

You can read more about hook_menu at the aforementioned hooks docs page.

您可以在上述的hooks文档页面上阅读有关hook_menu更多信息。

In this case, we create two menu items at the path admin/config/shownodes and admin/config/shownodes/settings and then we build a form with the help of drupal_get_form which creates check boxes for all the installed node types which we get from the Drupal function node_type_get_types. Saving the form, Drupal will automatically store the selected values in a variable using variable_set with the name taken from the form element which in our case is shownodes_nodes_toshow. We can fetch the value of this using the function variable_get('shownodes_nodes_toshow', array('')).

在这种情况下,我们在路径admin/config/shownodesadmin/config/shownodes/settings处创建两个菜单项,然后在drupal_get_form的帮助下构建一个表单,该表单为drupal_get_form获取的所有已安装节点类型创建复选框Drupal函数node_type_get_types 。 保存表单后,Drupal将使用variable_set自动将所选值存储在variable_set ,该variable_set的名称取自表单元素,在我们的示例中为shownodes_nodes_toshow 。 我们可以使用函数variable_get('shownodes_nodes_toshow', array(''))来获取其值。

We will use this to set the default values in the form and then later in the block implementation below to show the selected node types. Once we have added the above functions we should be able to see our configuration option as shown below in the Administration->configuration option.

我们将使用它在表单中设置默认值,然后在下面的块实现中稍后显示所选的节点类型。 添加了以上功能后,我们应该能够看到我们的配置选项,如管理->配置选项中所示。

alt

Selecting this option will show us our form with all the node types in the Drupal system and we will be able to select and save the node types which we want to show on our block as shown below.

选择此选项将向我们显示包含Drupal系统中所有节点类型的表单,我们将能够选择并保存要显示在块上的节点类型,如下所示。

alt

创建最新的节点块 (Creating latest node Block)

Once our configuration is saved we will build our block to display the node types which were selected in the configuration. To create a block we will implement the hooks hook_block_info and hook_block_view as shown below.

保存配置后,我们将构建块以显示在配置中选择的节点类型。 为了创建一个块,我们将实现钩子hook_block_infohook_block_view ,如下所示。

/**
 * Implements hook_block_info().
 */
function shownodes_block_info() {
  $blocks = array();

  $blocks['shownodeblock'] = array(
    'info' => t('A block to show selected nodes'),
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function shownodes_block_view($block_name = '') {
  if ($block_name == 'shownodeblock') {

    //Get the selected node types and create a list of them
    $show_nodes_list = array();
    $show_nodes_array = variable_get('shownodes_nodes_toshow', array(''));
    foreach ($show_nodes_array as $key => $value) {
      if($value) {
          array_push($show_nodes_list,$value);
      }
    }
    
    //Based on the node types create a query and then load the node types
    $query = new EntityFieldQuery();
    $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', $show_nodes_list)
    ->propertyCondition('status', 1)
    ->propertyOrderBy('created', 'DESC');
    
    $result = $query->execute();
    $nodes = array();
    if (isset($result['node'])) {
      $nids = array_keys($result['node']);
      $nodes = node_load_multiple($nids);
    }
    
    //Loop through the loded nodes to create a list
    //Which we will pass to the theme
    $list = array();
    foreach ($nodes as $node) {
      $options = array('absolute' => TRUE);
      $url = url('node/' . $node->nid, $options);
      $list[] = ''.$node->title.'';
    }
    
    //Return the content of the block
    $theme_args = array('items' => $list, 'type' => 'ol');
    $content = theme('item_list', $theme_args);

    $block = array(
      'subject' => t('Show Nodes Block'),
      'content' => $content,
    );
    return $block;
  }
}

In the above code we define a new block called shownodeblock in the function shownodes_block_info. Then, in the function shownodes_block_view we provide the view for that block by first reading the values which were saved in the configuration using the function variable_get('shownodes_nodes_toshow', array(''));. Next we query Drupal to get the node IDs based on the node types using the EntityFieldQuery class. Finally, we load those nodes and then loop through them to show them on our block.

在上面的代码中,我们在函数shownodes_block_info定义了一个称为shownodeblock的新块。 然后,在shownodes_block_view函数中,我们首先通过使用function variable_get('shownodes_nodes_toshow', array(''));读取配置中保存的值来提供该块的视图variable_get('shownodes_nodes_toshow', array('')); 。 接下来,我们使用EntityFieldQuery类查询Drupal以基于节点类型获取节点ID。 最后,我们加载这些节点,然后循环遍历它们以在我们的块上显示它们。

Once we have added the above code we should see our block in the block list. You can place the block in one of the regions (regions are actual parts of your layout, essentially, locations for blocks)

添加上述代码后,我们应该在阻止列表中看到我们的阻止。 您可以将块放置在一个区域中(区域是布局的实际部分,本质上是块的位置)

alt

Once the block is placed in one of the regions it can be seen in the region displaying the node types as follows

一旦将块放置在区域之一中,就可以在显示节点类型的区域中看到以下内容

alt

结论 (Conclusion)

In this article we created a complete Drupal module from scratch. We used different Drupal hooks and Drupal functions to create a configuration page and to create a block which we can place on a region. Drupal provides a variety of hooks which can be used to customize and provide additional functionality over the core. Through this short and fun tutorial, we've proven the extensible nature of Drupal makes expanding on it an easy and pleasant task. If you'd like to check out the full source code of this module, please see the Github repo.

在本文中,我们从头创建了一个完整的Drupal模块。 我们使用了不同的Drupal钩子和Drupal函数来创建配置页并创建可以放置在区域上的块。 Drupal提供了各种钩子,可用于自定义内核并在核心上提供其他功能。 通过这个简短而有趣的教程,我们证明了Drupal的可扩展性使对其进行扩展变得轻松而愉快。 如果您想查看此模块的完整源代码,请参阅Github仓库

翻译自: https://www.sitepoint.com/building-drupal-7-module-show-latest-nodes/

drupal全部都是节点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值