Drupal 7 drupal 7计算字段返回未定义的索引

Drupal 7 drupal 7计算字段返回未定义的索引,drupal-7,Drupal 7,好吧,两个小时后,我还在挣扎——但我还是无法理解 我正在尝试使用计算字段平均一个节点的所有fivestar评级。但我正在努力使用实体访问其他字段 在节点主体中,这可以正常工作: $test1 = $node->field_ae_stimclasswrk[und][0]['average']; 但在计算字段区域中,这不起作用: $entity_field[0]['value'] = $entity->field_ae_stimclasswrk[$entity->language

好吧,两个小时后,我还在挣扎——但我还是无法理解

我正在尝试使用计算字段平均一个节点的所有fivestar评级。但我正在努力使用实体访问其他字段

在节点主体中,这可以正常工作:

$test1 = $node->field_ae_stimclasswrk[und][0]['average'];
但在计算字段区域中,这不起作用:

$entity_field[0]['value'] = $entity->field_ae_stimclasswrk[$entity->language]    [und][0]['average'];
相反,在保存节点时,会出现以下索引错误:

注意:未定义的索引:und in eval()(第2行的

这一定是语法上的问题,但我完全没有主意了

以下是字段信息:

   [field_ae_stimclasswrk] => Array
        (
            [und] => Array
                (
                    [0] => Array
                            (
                            [user] => 80
                            [average] => 80
                            [count] => 1
                        )

                )

       )

只是代码中的一个小错误:

$entity->field_ae_stimclasswrk[$entity->language][und][0]['average'];
如果仔细观察,您实际上尝试访问字段的语言元素两次,一次使用
$entity->language
,一次使用
und

最好保持代码的上下文关系,以便删除代码中的
[und]
项:

$entity->field_ae_stimclasswrk[$entity->language][0]['average'];

我也遇到过同样的问题。它实际上是由
$entity->field\u ref[$entity->language]
中不存在的索引引起的

对我来说,
$entity->field\u ref[$entity->language]
对于所有节点都存在,但如果在其中添加索引,则会导致不使用该字段的任何节点出现问题

$entity->field\u ref[$entity->language][0]
导致了该问题(请注意添加了
[0]
索引)

要解决您的问题,您可以尝试:

$test1 = (isset($node->field_ae_stimclasswrk[$node->language][0]['average']))? $node->field_ae_stimclasswrk[$node->language][0]['average'] : NULL;
或者更容易阅读:

if (isset($node->field_ae_stimclasswrk[$node->language][0]['average'])){
  $test1 = $node->field_ae_stimclasswrk[$node->language][0]['average'];
} else {
  $test1 = NULL;
}

这样,它将绕过任何不使用该字段的节点。

保存节点后返回此错误:注意:未定义索引:und/home/greenabr/public\u html/sites/all/modules/computed\u field/computed\u field.module(439):eval()'d代码。您是否按照上面的答案进行了更改?是的,我进行了上述更改。Humm.try
print\r($entity->field\u ae\u stimclasswrk)
看看变量中的实际内容