1

I have an entity called CV which is used to store uploaded CVs. The entity is created and referenced on the user profile using inline entity form. I need to get the fid for the cv so I can do this:

$uri = file_load($fid)->uri;
$file_name = file_load($fid)->filename;
$path = file_create_url($uri);

I've tried to use entity_metadata_wrapper, but I'm not sure how to actually access the correct field. This is an example of what I've been trying:

$wrapper = entity_metadata_wrapper('candidate_cv', $cv);
$cv_file = $wrapper->field_candidate_cv->value();   

Which returns this error:

EntityMetadataWrapperException: Unknown data property field_candidate_cv.

Update: I have managed to get the title of the entity using this:

$cv_wrapper = entity_metadata_wrapper('user', $user);
$cv_upload = array();
foreach ($cv_wrapper->field_cv->value() as $index => $feature) {
    $cv_upload[] = $feature->title;
}
echo title[0];

But I can't get to the fid of the file field which is field_candidate_cv.

$cv_wrapper = entity_metadata_wrapper('user', $user);
   $cv_upload = array();
   foreach ($cv_wrapper->field_cv->value() as $index => $feature) {
        $cv_upload[] = $feature->field_candidate_cv;
    }
    $fid = $cv_upload[0]['fid'];

$fid prints nothing.

However, in this $fid prints Array:

 $fid = $cv_upload[0];
4
  • It seems that your entity is not properly defined or that entiy has no field called field_candidate_cv
    – sanzante
    Feb 4, 2015 at 15:25
  • It definitely has that field, so how do I properly define the entity?
    – Tasha
    Feb 4, 2015 at 15:33
  • try to access the property without metadata wrapper en check if that approach works
    – Robin
    Feb 4, 2015 at 15:38
  • I tried accessing the field with metadata wrapper but it returned this: 'Warning: array_flip(): Can only flip STRING and INTEGER values! in EntityCacheControllerHelper::entityCacheLoad()'
    – Tasha
    Feb 4, 2015 at 16:01

1 Answer 1

0

Solution:

$cv_wrapper = entity_metadata_wrapper('user', $user);
$cv_upload = array();
foreach ($cv_wrapper->field_cv->value() as $feature) {
    $cv_upload[] = $feature->field_candidate_cv;
}
$fid = $cv_upload[0]['und'][0]['fid'];

Printed the array of $cv_upload using:

drupal_set_message('<pre>'. print_r($cv_upload, TRUE) .'</pre>');

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.