dongyuduan1890 2015-03-26 15: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 03: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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部