2

My structure is as follows:

I've got a custom contenttype with a multivalue field containing a field_collection. In this field_collection there are an amount field, a entity_reference_field and a computed_field. The referenced entity also contains several fields.

What I need to do:

  • Step 1: In the computed field (inside the collection) I need to get the value of a certain field of the entity, referenced by the entity_reference field (also inside the field collection) and multiply this value with the value of the amount field.
  • Step 2: In another computed field (on the node itself) I need to sum up all the values of all the computed fields inside these collections.

My Problem is primarily Step 1

  • What r u using for calculating and when? Is it ur module or rule? – ar7max Aug 23 '15 at 17:36
  • @ar7max I think that he use for calculate computed_field project – Adrian Cid Almaguer Aug 23 '15 at 18:45
1

Based on what I read in the computed_field documentation, you have two options when creating the necessary code to compute the field. You could create the PHP into the text area of the field (I really dislike PHP in the database...but that's just me) or you can create a custom module with a specific function in it. Per the documentation:

Drupal 7

To use a function in a custom module instead of writing the code in text area create a function that looks like:

<?php
function computed_field_YOUR_FIELD_MACHINE_NAME_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
  $entity_field[0]['value'] = 'value';
}
?>
Parameters:

&$entity_field - The computed field. Used to store the computed value.
$entity_type - The entity type: node, user, comment, etc.
$entity - The actual entity (a node, user, comment, etc.)
$field - General field settings.
$instance - Field instance settings.
$items - The list of items.
Note:
Make sure $entity_field is passed by reference.

What is awesome about this method is we have the entity available to us via $entity (in your case, it would be an entity of type field_collection). From this approach, I would:

  1. Create a custom module with this function
  2. I would then create logic around calling the $entity variable, loading the entity (if required) and then getting it's entity reference, which will probably require another entity load.
  3. Do compute field logic that you'd like.

Once this is done, you can use the same custom module to write another computer_field_YOURFIELD_compute() function for your initial field (on the node) that computes the combination for all field_collection computed field values.

  • Thank you for these clarifications. they are indeed useful but didn't address my problem. – Roman Meissner Aug 26 '15 at 8:03
  • In the snippet: ' $entity_field[0]['value'] = 'value';' it is the second "value" i've got a problem with. At that position i need to get the value of a field of a referenced (by an entity_reference field) entitys field. – Roman Meissner Aug 26 '15 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.