【问题标题】:Drupal 7: custom module: display custom contentTypy in custom blockDrupal 7:自定义模块:在自定义块中显示自定义 contentTypy
【发布时间】:2014-08-06 20:25:01
【问题描述】:

我是 Drupal 7 的新手,所以我有一个问题。

我有自己的内容类型 Writers,其中包括标题、生命年限、照片、描述等字段。

我的任务是在页面上显示 3 个随机 Writer。实际上我是在 Views 模块的帮助下完成的,但我想自己做。

所以我像这样创建了自己的模块 random_content:

<?php

function random_content_help($path, $arg) {
  switch ($path) {
    case "admin/help#random_content":
    return '<p>'. t("Displays random content") .'</p>';
   break;
  }  
}

function random_content_block_info() {
  $blocks['random_content'] = array(
    'info' => t('Random content'), 
    'cache' => DRUPAL_CACHE_PER_ROLE, 
  );
  return $blocks;
}

function random_content_contents() {
    $query = db_select('node', 'n')
    ->fields('n', array('nid', 'title'))
    ->condition('type', 'writers')
    ->orderBy('rand()') 
    ->range(0,3)
    ->execute();
    return $query; 
}

function random_content_block_view($delta = '') {
  switch($delta){
    case 'random_content':
    $block['subject'] = t('Random content');
    if(user_access('access content')) {
      $result = random_content_contents();       
      $items = array();           
      foreach ($result as $node){
                $items[] = array(
                'data' => l($node->title, 'node/' . $node->nid) . '</br>',
              );
      }
      if (empty($items)) { 
        $block['content'] = t('No data availible.'); 
      } else {
        $block['content'] = theme('item_list', array(
          'items' => $items));
      }
    }
  }
  return $block;
 }

如您所见,我只学会了添加指向特定内容的链接。但是如何显示完整的信息,包括标题、寿命、照片和描述?

【问题讨论】:

    标签: drupal drupal-7 drupal-modules drupal-hooks


    【解决方案1】:

    要显示完整节点或其中的一部分,您需要加载节点。例如

    $my_node = node_load($nid);
    
    $render_array = array();
    $render_array['title'] = array(
       '#type' => 'markup',
       '#markup' => $my_node->title
    );
    $author = field_get_items('node', $my_node, 'field_author','und');  
    $render_array['author'] = array(
       '#type' => 'markup',
       '#markup' => $author[0]['safe_value']
    );
    
    // or as some like to do it
    
    $render_array['author'] = array(
       '#type' => 'markup',
       '#markup' => $my_node->field_author['und'][0]['value']
    );
    echo drupal_render($render_array);
    

    注意“und”常量表示语言未定义。如果您启用了翻译/语言并针对不同语言提供不同的内容,则您必须使用“en”、“de”等来表示相应的语言。

    您也可以让drupal 渲染节点,然后操作或检索单个项目。像这样

    $my_node = node_load($nid);
    $build = node_view($my_node,'full');
    $build['body'][0]['#markup'] = $build['body'][0]['#markup'].' some addition';
    $build['field_author'][0]['#markup'] = $build['field_author'][0]['#markup'].' my favorite';
    
    echo drupal_render($build);
    

    使用后一种方法的优点是,整个主题引擎以及所有设置为作用于内容等的钩子都会启动。当然,如果您只想检索值,则不需要它。

    另请注意,我假设您的作者字段名为 field_author。您应该在字段编辑窗口中检查内容类型。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    相关资源
    最近更新 更多