在其个人资料页面上显示数据库中的用户数据



在Drupal 7中,是否可以在每个用户各自的个人资料页面上显示数据库查询的结果?我需要在现有模块中以编程方式执行此操作。因此,查询的输入将是当前正在查看其配置文件的用户的 ID。

仅显示查询的数据 - 没有管理,没有编辑,没有其他内容。

类似的东西..(图片仅供参考(

此外,块或字段或任何可能使这成为可能的东西都需要通过 _permission(( 钩子来配置谁可以或不能查看它。

我认为由于这基本上只是一个没有额外自定义内容的查询,因此可以通过Drupal API找到一种简单的方法。

您可以为此创建自定义块并在当前用户配置文件中查看它

  /**
     * Implements hook_block_info().
     */
    function custom_block_block_info() {
      $blocks = array();
      $blocks['my_block'] = array(
        'info' => t('My Custom Block'),
        'status' => TRUE,
        'region' => 'Content',
        'visibility' => BLOCK_VISIBILITY_LISTED,
        'pages' => 'user/*',
      );
      return $blocks;
    }
 /**
 * Implements hook_block_view().
 */
    function custom_block_view($delta = '') 
    {
        // The $delta parameter tells us which block is being requested.
        switch ($delta) 
        {
            case 'my_block':
                // Create your block content here
                $block['subject'] = t('This is just a test block created programatically');
                  $block['content'] = _user_detail_list();
                break;
        }
        return $block;
    }
 /**
 * Implements costome code we want to print().
 */ 
    function _user_detail_list(){
        //enter your query and output in some variable
        $value = "<p>User Detail</p>"
        return $value;
    }

注意:- 这里的配置文件使用新块扩展

会有一些编码来获得你想要的东西,但如果你只是想要样式/显示"user"对象已经可用的数据,那么下面的#1就可以了。

简单的方法:1. 创建一个视图并选择您需要显示的"用户"信息并为其提供路径。然后在您的子主题中使用正确的模板 - 请参阅代码片段。 https://www.drupal.org/forum/support/post-installation/2011-04-04/modify-the-default-profile-pagelayout

其他方式:

  1. 使用 user-profile.tpl .php请参阅https://api.drupal.org/api/drupal/modules%21user%21user-profile.tpl.php/7.x

  2. 在您的模块中,您需要致电并联系hook_user_view。

    https://api.drupal.org/api/drupal/modules%21user%21user.api.php/function/hook_user_view/7.x

在这里,您从数据库中获取用户配置文件数据,然后跟踪它

function modulename_menu() {
    $items['user-data'] = array(
    'title' => 'User data',
    'page callback' => 'user_data',
    'access callback' => ('user_is_logged_in'),
    '#type' => MENU_NORMAL_ITEM, 
  ); 
  return $items;    
}
function user_data(){
   global $user;
   $user_fields = user_load($user->uid);
   $output = "" //return those $user_fields values into table using theme('table',header,rows) 
   return $output; 
}

https://www.drupal.org/node/156863(用于创建表视图(

global $user;
$user_fields = user_load($user->uid);
$firstname = $user_fields->field_firstname['und']['0']['value'];
$lastname = $user_fields->field_lastname['und']['0']['value'];

相关内容

最新更新