【问题标题】:Drupal render() - make printed content ucfirstDrupal render() - 使打印内容 ucfirst
【发布时间】:2012-09-11 20:27:40
【问题描述】:

我有一个 drupal 模板,可以呈现如下内容:

<?php $field_collection_state_info = reset($content['field_state_information']['0']['entity']['field_collection_item']);  ?>  
<?php print render($field_collection_state_info['field_state_name']); ?>

但我想运行 php ucfirst(),所以所有呈现的内容都在第一个字母上大写。

我试过这个:

<?php print ucfirst(render($field_collection_state_info['field_state_name'])); ?>

但这没有用。

我应该如何做到这一点?

非常感谢任何建议。

C

【问题讨论】:

    标签: php drupal drupal-7 drupal-render


    【解决方案1】:

    我认为您必须在调用 render() 之前更改渲染数组的内容:

    // the 'und' part is depending on the translation of the node
    $field_collection_state_info['field_state_name']['und'][0]['value'] =
    ucfirst($field_collection_state_info['field_state_name']['und'][0]['value']);
    // render
    print render($field_collection_state_info['field_state_name']);
    
    // or you could try to modify the safe_value part of the array depending on your
    // filter settings for the field
    $field_collection_state_info['field_state_name']['und'][0]['safe_value'] =
    ucfirst($field_collection_state_info['field_state_name']['und'][0]['safe_value']);
    // render
    print render($field_collection_state_info['field_state_name']); 
    

    编辑尝试将此添加到您的template.php,它会更改使用hook_preprocess_node() 的collection_field 的值:

    function YOURTHEME_preprocess_node(&$vars) {
      if($vars['type'] == 'YOUR-CONTENT-TYPE') {    
         // you might need to replace the field_content_block with the name of your field <field_state_information>
         $data = reset($vars['content']['field_content_block'][0]['entity']['field_collection_item']);
         // turn the first character to uppercase
         $data['field_text']['#object']->field_text['und'][0]['value'] = ucwords($data['field_text']['#object']->field_text['und'][0]['value']);
        // set the data in the array, like I said above your might need to change the `field_collection_block` to `field_state_information`
        $vars['content']['field_content_block'][0]['entity']['field_collection_item'] = $data;
      } 
    }
    

    【讨论】:

    • 谢谢@Krister。我收到此错误:Notice: Undefined index: und in include() 有什么想法吗?
    • 嗨@Krister ...我在上面的代码中添加了一行,这可能有助于解释为什么这不起作用。抱歉,我之前没有发过。
    • @cybercampbell - 你的模板文件的名称是什么?
    • @cybercampbell - 我更新了我的答案,改用hook_preprocess_node()。我在自己安装的内容类型上尝试了它,该内容类型具有包含图像和文本字段的 field_collection。
    • 谢谢@Krister ...我应该为YOUR-CONTENT-TYPE 放什么,因为这是通过视图显示的profile2 数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2011-01-14
    • 1970-01-01
    相关资源
    最近更新 更多