drdawt9210 2015-11-19 12:32
浏览 20

Drupal 7,访问_preprocess_html中的复选框字段值

I am trying to access a custom node field using _preprocess_html but many of the approaches I've seen on StackOverflow don't seem to work correctly and I don't quite understand why.

I have a checkbox field called 'public' which is available on a node as 'field_public'. I want to find out if it is checked or unchecked in a preprocess_html template function.

If I am not logged into the site and do the following :

function MYTHEME_preprocess_html() {
    $node = menu_get_object();
}

Then my field value is $node->field_public[LANGUAGE_NONE][0]['value'] as I would expect. However if I am logged in it changes to $node->field_public[0]['value'].

Now I could just do

$public = ($node->field_public[LANGUAGE_NONE][0]['value'] || $node->field_public[0]['value'])

But I want to it correctly, and none of the options I have tried seem to give me a value in both cases. I have tried :

// node_build_content method
$node = node_load($node->nid);
node_build_content($node);
$public = $node->content['field_public'];

// entity api method
$wrapper = entity_metadata_wrapper('node', $node);
$public = $wrapper->field_public->value();

// field_get_item method
$public = field_get_item('node',$node,'field_public');

In all these cases the value is fetched when the langauage attribute is present (not logged in) and isn't fetched when I am logged in as admin (when the node object has no language attribute), so I have to fall back on checking two ways. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dsc6517 2015-11-19 12:47
    关注

    Best way to get field value is always:

    $node->field_public[LANGUAGE_NONE][0]['value']
    

    Language_None is equal to 'und', when you dont use multilanguage fields it will be always good way to get value. [0] is a index, when field has only one value, it will be at this index - always.

    There is no need to check it like that:

    $public = ($node->field_public[LANGUAGE_NONE][0]['value'] || $node->field_public[0]['value'])
    

    Fields always have language key first.

    So using it like that:

    $public = $node->field_public[LANGUAGE_NONE][0]['value'];
    

    Or to be safe if field is not always used, and you render different type of content:

    $public = isset($node->field_public[LANGUAGE_NONE][0]['value']) ? $node->field_public[LANGUAGE_NONE][0]['value'] : null;
    

    Its totally fine to get simple value. You can try to render fields or to use other function if you want to have other result, like themed field.

    Your code:

    $node = node_load($node->nid);
    

    here you just make query twice, dont know why, node is already fetched inside $node variable. So just use value, Keep It Simple

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?