如何在bean模板中使用变量

时间:2015-10-06 09:36:08

标签: php drupal drupal-7 hook drupal-modules

我的drupal _preprocess_block(&$variables)与bean模板有问题。 我有模块:

    function kcc_related_releases_preprocess_block(&$variables) {
  // select Bean Blocks and ignore other Blocks
  if (!empty($variables['block']->module) && isset($variables['elements']['bean'])) {
    $bean_array      = $variables['elements']['bean'];
    $child_key_array = element_children($bean_array);
    if(isset($child_key_array)&&!empty($child_key_array)) {
      $bean = $bean_array[$child_key_array[0]];
      if(!empty($bean['#bundle'])&&$bean['#bundle']=='related_press_releases_block') {
        $bean_entity = $bean['#entity'];
        $bean = bean_load($bean_entity->bid);
        $tax_terms = $bean->field_tax_term;
        $related_array = array();
        foreach ($tax_terms as $tax_term) {
          $name = $tax_term[0]['taxonomy_term']->name;
          $tid = $tax_term[0]['tid'];
          $tax_term_links[] = theme('tax_term_link',
            array(
              'name' => $name,
              'tid' => $tid,
            ));
          // Get related content.
          $query = db_select('node', 'n');
          $query->join('field_data_field_tax_term', 'fc', 'n.nid = fc.entity_id');
          $query->fields('n', array('nid', 'title', 'uid'))
                ->condition('fc.field_tax_term_tid', $tid)
                ->condition('n.status', 1)
                ->range(0,5);
          $result = $query->execute()->fetchAllAssoc('nid');
          if ($result) {
            $related_array = array_merge($related_array, $result);
          }
        }
        // Tags rendered array.
        $variables['tax_term_links'] = implode(', ', $tax_term_links);
        // Related rendered array.
        foreach ($related_array as $key => $related) {
          $related_links_array[$key]['title'] = l($related->title, url('node/' . $related->nid));
        }
        if (isset($related_links_array) && !empty($related_links_array)) {
          $variables['related_terms'] = $related_links_array;
        }
        else {
          $variables['related_terms'] = NULL;
        }
      }
    }
  }

}

模板:

if (!empty($related_terms)): ?>
  <h4 class="related-title"><?php print render($title); ?></h4>
  <div class="related">
    <?php foreach($related_terms as $rel_item): ?>
      <div class="item">
        <div class="title"><?php print render($rel_item['title']); ?></div>
      </div>
    <?php endforeach; ?>
  </div>
<?php endif; ?>

但它不起作用,在我使用print_r($variables);的模板中我可以找到$ related_terms 请帮帮我

1 个答案:

答案 0 :(得分:1)

经过几次检查和搜索后,我发现了这个钩子函数HOOK_process_entity(&$variables) {,这解决了我的问题,而不是在我的模板变量中添加了corectly。希望它会帮助某人