dongyuduan1890 2015-03-26 23:04
浏览 49
已采纳

结合3个单独的工作脚本 - 用于drupal计算字段

I'm using computed field in drupal, which uses just straight up PHP, only problem is I don't know php well.

The 3 scripts below, work individually. When I try to combine them, using different syntax of course, they all fail. What I'm trying to do is add up 3 fields, from 3 different field sets or field collections.

1st

$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;

2nd

$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;

3rd

$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;

I've tried this to combine them, but it doesn't work:

$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;

I've aware the foreach loops through arrays. I don't need it to loop, so I can take that out. They are all multiple value fields, but I only need it to pull one value.

I've also tried this, and it didn't not work either:

$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();

Any help greatly appreciated! Been messing with this all day.

  • 写回答

1条回答 默认 最新

  • dongshen2903 2015-03-27 11:14
    关注

    Just combine the parts where the three examples differ…

    $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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题