使用规则处理字段集合项

使用规则处理字段集合项很有趣,真的! 看一下这条规则(在规则导出格式中):

{ "rules_calculate_sum_of_prices_in_all_field_collection_items" : {
    "LABEL" : "Calculate sum of prices in all field collection items",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_view--article" : { "bundle" : "article" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_article_details" } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "\u003Cstrong\u003EDrupal calculator\u003C\/strong\u003E started ..." } },
      { "variable_add" : {
          "USING" : { "type" : "decimal", "value" : "0" },
          "PROVIDE" : { "variable_added" : { "total_price" : "Price total" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-article-details" ] },
          "ITEM" : { "article_details_item" : "Article details item" },
          "DO" : [
            { "data_calc" : {
                "USING" : {
                  "input_1" : [ "total-price" ],
                  "op" : "+",
                  "input_2" : [ "article-details-item:field-price" ]
                },
                "PROVIDE" : { "result" : { "calculation_result" : "Calculation result" } }
              }
            },
            { "data_set" : { "data" : [ "total-price" ], "value" : [ "calculation-result" ] } },
            { "drupal_message" : { "message" : "After adding a price of \u003Cstrong\u003E[article-details-item:field-price]\u003C\/strong\u003E for field collection item with id \u003Cstrong\u003E[article-details-item:item-id]\u003C\/strong\u003E, subtotal is \u003Cstrong\u003E[calculation-result:value]\u003C\/strong\u003E." } }
          ]
        }
      },
      { "drupal_message" : { "message" : "The \u003Cstrong\u003ETotal price\u003C\/strong\u003E for all prices included as field collection items is \u003Cstrong\u003E[total-price:value]\u003C\/strong\u003E." } },
      { "drupal_message" : { "message" : "\u003Cstrong\u003EDrupal calculator\u003C\/strong\u003E ended ..." } }
    ]
  }
}

关于这条规则的更多细节如下……

规则事件:

查看内容(类型文章),使内容类型 article 的机器名称适应任何适合(或使用任何其他适合的规则事件)。

规则条件:

实体具有字段,而实体是节点,我的字段集合字段的机器名称是 field_article_details(使此机器名称适合任何适合,但请确保使用字段集合字段本身)。

规则行动:

醒来,这里是魔术(好玩吗?)将要发生的地方……这些是涉及的规则行动:

  1. Show a message on the site,有这样的消息:

Drupal 计算器开始……

  1. Add a variable,而它是一个名为 total_price 的变量,十进制(2 位数),初始值 0。

  2. Add a loop,迭代我的字段集合字段的每个项目(机器名称为 field_article_details),并为每次迭代执行以下规则操作:

  • Calculate a value,计算 total_price(在上面的规则行动 2 中定义)和 article-details-item:field-price(这是包含价格的字段集合中的字段的机器名称,带有 2 位数的小数)的总和,并将结果(总和)存储在变量中 calculation_result

  • Set a data value,它只是复制存储在我的 total_price 中的变量 calculation_result 中的值(在上面的规则操作 2 中定义)。备注:不确定(未经测试),但也许这个 calculation_result 变量可以直接替换为 total_price(在上一个操作中),因此你不需要此操作。

  • Show a message on the site,有这样的消息:

    为 ID 为 3 的字段集合项添加 3.40 的价格后,小计为 15.00。

  1. Show a message on the site,有这样的消息:

包含在字段收集项目中的所有价格的总价格为 26.23。

  1. Show a message on the site,有这样的消息:

Drupal 计算器结束了……

显然,这个规则是一个原型。在确信它能够正常工作之后,只需删除所有规则操作即可 Show a message on the site。因此,只有第 2 项和第 3 项(没有最后一个子项)留作规则操作。

开演时间 …

以下是我的测试结果示例,即显示的 Drupal 消息:

Drupal calculator started ...
After adding a price of 2.45 for field collection item with id 1, subtotal is 2.45.
After adding a price of 9.15 for field collection item with id 2, subtotal is 11.60.
After adding a price of 3.40 for field collection item with id 3, subtotal is 15.00.
After adding a price of 1.23 for field collection item with id 4, subtotal is 16.23.
The Total price for all prices included as field collection items is 26.23.
Drupal calculator ended ...

更多信息

如果你不熟悉字段集合,请尝试首先消化“ 此问题 ” 的答案。