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

时间:2017-01-14 04:04:27

标签: php drupal drupal-7

我创建了一个自定义模块,以编程方式创建一个块并显示国家/地区列表。它使用自定义模板文件。我将国家/地区列表数组传递给模板文件。

问题是如何在模板文件中使用foreach显示每个国家/地区。

我的代码如下。

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;
}

模板/块 - my_web_service.tpl

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

非常感谢任何帮助。请找到下面输出的屏幕截图。

Screen shot

参考

Create a custom template file for a custom block in drupal

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

2 个答案:

答案 0 :(得分:0)

我注意到您的代码中存在一些问题:

  1. 您的hook_theme实施不完整。最终代码:

    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. 按照以下方式调用您的主题:

     ...
     $result = my_web_service_consume_data();
     $variables = array(
         'results' => $result
     );
     $block['content'] = theme('block__my_web_service', $variables);
    
  3. 在模板中使用$results变量:

     <?php
      foreach ($results as $result) {
         echo $result . '<br/>';
      }
     ?>
    
  4. 这就是它。祝你好运。

答案 1 :(得分:0)

/etc/hosts

请注意,它应该是$ results而不是$ result

相关问题