doubu7425 2017-01-14 04:04
浏览 65
已采纳

在Drupal的模块开发中使用块中的模板文件中的foreach显示数组值

I created a custom module to create a block programmatically and show country list. It uses custom template file. I passed the country list array to the template file.

Issue is how do I show each country using foreach inside the template file.

My code is given below.

my_web_service.module

    <?php

    /*
     * @file
     * A sample web service module
     */

    /*
     * Implements hook_menu
     */

    function my_web_service_menu() {
      $items = array();

      $items['test-web-service'] = array(
        'title' => 'Test Web Service',
        'description' => 'Test web service',    
        'access arguments' => array('access administration pages'),
        'type' => MENU_NORMAL_ITEM,
      );

      return $items;
    }

    function my_web_service_consume_data() {
      $url = 'http://services.groupkt.com/country/get/all';
      $result = drupal_http_request($url);
      $country_response = drupal_json_decode($result->data);
      $country = array();
      $i = 0;
      if (!empty($country_response)) {
        foreach($country_response['RestResponse']['result'] as $country_arr) {
          $country[$i] = $country_arr['name'];
          $i++;
        }
      }

      return $country; 
    }

    function my_web_service_block_info() {
      $blocks['my_web_service'] = array(
        'info' => t('My Web Service'),
      );

      return $blocks;
    }

    function my_web_service_block_view($delta = '') {
      switch ($delta) {
        case 'my_web_service' :
          $block['subject'] = t('My Web Service');
          if (user_access('access content')) {
            $result = my_web_service_consume_data();
            $variables = $result;
            //$block['content'] = theme('item_list', array('items' => $result)); 
            $block['content'] = theme('block__my_web_service', $variables);
            //$block['content'] = theme('item_list', $variables);
          }
      } 

      return $block;

}
function my_web_service_theme($existing, $type, $theme, $path) {
  $theme = array();
  $theme['block__my_web_service'] = array(
    'variables' => array(),
    'template' => 'block--my_web_service',    
    'path' => drupal_get_path('module', 'my_web_service') . '/templates',
  );
  return $theme;
}

template/block--my_web_service.tpl

<?php
echo '<pre>' . print_r($variables, true) . '</pre>';
?>

Any help is highly appreciated. Please find the screen shot of out put below.

Screen shot

References

Create a custom template file for a custom block in drupal

https://www.jaypan.com/tutorial/custom-drupal-blocks-right-way

  • 写回答

2条回答 默认 最新

  • drbuowqe02101 2017-01-14 16:54
    关注

    I noticed a few issues in your code:

    1. Your hook_theme implementation is incomplete. Final code:

      function my_web_service_theme($existing, $type, $theme, $path) {
         $theme = array();
         $theme['block__my_web_service'] = array(
           'variables' => array(
              'results' => array()
            ),
           'template' => 'block--my_web_service',    
           'path' => drupal_get_path('module', 'my_web_service') . '/templates',
         );
         return $theme;
       }
      
    2. Call your theme like this:

       ...
       $result = my_web_service_consume_data();
       $variables = array(
           'results' => $result
       );
       $block['content'] = theme('block__my_web_service', $variables);
      
    3. Use $results variable in your template:

       <?php
        foreach ($results as $result) {
           echo $result . '<br/>';
        }
       ?>
      

    That's pretty much it. Good luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。