1

The background of this question is that I'm trying to migrate data from Drupal 6 Imagefield Extended to Drupal 7 File Entity (with File Entity Inline). See also https://www.drupal.org/node/974888

First I tried to understand how entities work and how they can be extracted and manipulated using Entity Metadata Wrappers. That seems to work ok, for example, I can get the values of my imagefield like this:

$nid = 103025;
$node_wrapper = entity_metadata_wrapper('node', $nid);
$images = $node_wrapper->field_image->value();

And it seems that I could change the values of the images with something like this:

// Do something with $images
$node_wrapper->field_image->set($images);
$node_wrapper->revision->set(1);
$node_wrapper->save();

(For some reason it's not saving the values for the node in my case, but that's another question.)

My problem now is that I have nested fields, i.e. the imagefield has "subfields" for the source and license (that I had added in Drupal 6 using Imagefield Extended). I do get the values of those nested fields in $images - but only in the typical array with $images[und][0]['value'] and $images[und][0]['safe_value']. But there seems to be no way to get those values through the wrapper, for example like

$node_wrapper->field_image->field_source->value();
$node_wrapper->field_image->value()->field_source->value();

Now I have been trying for hours to figure out how I can change (or populate) those (sub) fields and then save the entire node, but to no avail.

I'm also wondering if it's necessary to do this on a node level as files are also entities, but it seems that there is nothing like entity_metadata_wrapper('file', $fid);.

I'd appreciate any hint that could point me in the right direction because the search has been kind of exhausting.

7
  • Have you tried $node_wrapper->field_image->file->field_source->value()?
    – Clive
    Commented Jan 16, 2015 at 22:18
  • Hi Clive, that one returns a PHP error: "Call to a member function value() on a non-object".
    – yotka
    Commented Jan 17, 2015 at 11:52
  • Have you managed to confirm that File Entity definitely supports EntityMetadataWrapper? It isn't necessarily something implicit, entity modules can need to explicitly declare their support for it
    – Clive
    Commented Jan 17, 2015 at 12:46
  • Actually ignore that, it does support it. Try inspecting the results of $node_wrapper->field_image->getPropertyInfo() and $node_wrapper->field_image->file->getPropertyInfo() to see what fields/properties the system things are available. That might help to shed some light. Feel free to edit them into the question and ping me if you want me to have a look
    – Clive
    Commented Jan 17, 2015 at 12:51
  • 1
    What about $node_wrapper->field_image[0]->getPropertyInfo()? And then perhaps $node_wrapper->field_image[0]->file->getPropertyInfo(). I don't have file entity installed on anything so I'm guessing a bit
    – Clive
    Commented Jan 17, 2015 at 15:00

2 Answers 2

1

Using what Clive mentioned and with the hint eikes provided, this seems to be a way to accomplish what I want:

$nid = 108501;
$node_wrapper = entity_metadata_wrapper('node', $nid);

for ($i = 0; $i < count($node_wrapper->field_image->value()); $i++) {
  $node_wrapper->field_image[$i]->file->field_source->set('NEW SOURCE');
  $node_wrapper->field_image[$i]->file->field_license->set('NEW LICENSE');
  $node_wrapper->field_image[$i]->file->save();
}

// Just for dev: Check if the values have been saved
$node_wrapper = entity_metadata_wrapper('node', $nid);
dsm($node_wrapper->field_bild->value());

Erroneously I used the save() call on the whole node wrapper, but as files are their own entities, the call needs to be on the file, as eikes suggested.

Additionally, I noticed that calling $node_wrapper = entity_metadata_wrapper('node', $nid); after the above code doesn't reflect the changes (for checking). It seems I need to reload the entity first.

Thanks a lot to both of you!

1
  • 1
    I assume that the file you get is a separate entity and you may need to save it as well: $node_wrapper->field_image[$i]->file
    – eikes
    Commented Jan 27, 2015 at 12:54
0

This is my solution to access and update 2 nested levels deep on unlimited values on field collections fields

  function set_descuentos_pagos_action($nid) {
  // $raw_node = node_load($nid);  AVOID THIS
  $node = entity_metadata_wrapper('node', $nid);
  // dpm($node->getPropertyInfo());
  // dpm($node);
  $raw_ana = $node->field_ana->value();
  // dpm($raw_ana);
  $num_cotizaciones = count($raw_ana);

  // $i ES EL ITEM DE  CADA COTIZACION
  for ($i=0 ; $i < $num_cotizaciones  ; $i++ ) {
    
    $ana[$i] = entity_metadata_wrapper('field_collection_item', $raw_ana[$i]);
    $fp = $ana[$i]->field_m_fc_pago_texto->value();
    if($fp == 'Trimestral'){
      // get values from parent field collection
      $prima_descuento[$i] = $ana[$i]->field_m_fc_prima_neta_descuento->value();
      $prima_original[$i] = $ana[$i]->field_m_fc_prima_neta->value();
      $factor[$i] = $prima_descuento[$i] / $prima_original[$i];

      // get child field collection
      $raw_recibos = $ana[$i]->field_m_fc_recibo->value();
      $num_recibos[$i] = count($raw_recibos);

      // $j SON LOS PAGOS DE LA ITERACION $i
      for ($j=0; $j < $num_recibos[$i]; $j++) {

        // get current child field collection object
        $value[$j] = $ana[$i]->field_m_fc_recibo[$j]->value();

        // $pago[$j] ES EL PAGO OBJETO
        $pago[$j] = entity_metadata_wrapper('field_collection_item', $value[$j]);
         // dpm($pago[$j]->value());
         // get field value, set value on other field
         $prima = $pago[$j]->field_m_fc_prima_total->value();
         $nuevo_valor = $prima * $factor[$i];
         $pago[$j]->field_m_fc_prima_total_descuento->set($nuevo_valor);
         $pago[$j]->save();
      }

    }
  }

} // end  set_descuento_pagos_cotizacion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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