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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用