我在drupal中使用计算字段,它只使用直接PHP,唯一的问题是我不熟悉php。
下面的3个脚本,单独工作。当我尝试将它们组合起来时,当然使用不同的语法,它们都会失败。我想要做的是从3个不同的字段集或字段集合中添加3个字段。
第1次
$price = 0;
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach($wrapper->field_venue_sites_simple as $collection) {
$price += $collection->field_site_fee->value();
}
$entity_field[0]['value'] = $price;
第二
$price = 0;
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach($wrapper->field_venue_sites_w_catering as $collection) {
$price += $collection->field_site_fee->value();
}
$entity_field[0]['value'] = $price;
第三
$price = 0;
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach($wrapper->field_venue_site as $collection) {
$price += $collection->field_peak_pricing->field_peak_price_saturday->value();
}
$entity_field[0]['value'] = $price;
我已尝试将它们合并,但它不起作用:
$price = 0;
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$price =
$wrapper->field_venue_sites_simple->field_site_fee->value() +
$wrapper->field_venue_site->field_peak_pricing->field_peak_price_saturday->value() +
$wrapper->field_venue_sites_w_catering->field_site_fee->value();
$entity_field[0]['value'] = $price;
我已经意识到foreach循环遍历数组。我不需要它循环,所以我可以把它拿出来。它们都是多个值字段,但我只需要它来拉一个值。
我也试过这个,但它也没有用:
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$collection1 = $wrapper->field_venue_sites_simple;
$collection2 = $wrapper->field_venue_site;
$collection3 = $wrapper->field_venue_sites_w_catering;
$entity_field[0]['value'] =
$collection1->field_site_fee->value()+
$collection2->field_peak_pricing->field_peak_price_saturday->value()+
$collection3->field_site_fee->value();
任何帮助非常感谢!一整天都搞乱了。
答案 0 :(得分:0)
只需将三个示例不同的部分组合起来......
$price = 0;
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach($wrapper->field_venue_sites_simple as $collection) {
$price += $collection->field_site_fee->value();
}
foreach($wrapper->field_venue_sites_w_catering as $collection) {
$price += $collection->field_site_fee->value();
}
foreach($wrapper->field_venue_site as $collection) {
$price += $collection->field_peak_pricing->field_peak_price_saturday->value();
}
$entity_field[0]['value'] = $price;