0

i've a custom filefield in a custom content type and i use to display it in a node.tpl .But here my filefield has no limit number and under D6 i use this code :

    foreach($node->field_objectif_bts as $file) {
  if ($file['view']) {
      $header = array('data' => 'Les objectifs des formations BTS');
      $rows[] = array($file['view']);
      $attributes = array('id'=> 'objectif-bts','sticky' => FALSE);
    }
}

$output = theme('table', $header,$rows, $attributes);
print $output;

In Drupal 7 it doesn't work, so I'm looking for modifications to do on this code or new code for Drupal 7 theming.

Actually, I tried the following.

foreach($node->field_objectif_bts as $file) {
  if ($file['und']['value']) {
      $header = array('data' => 'Les objectifs des formations BTS');
      $rows = array($file['und'][0]['value']);
      $attributes = array('id'=> 'objectif-bts','sticky' => FALSE);
    }
}

$output = theme('table', array('header' => $header, 'rows'=> $rows), $attributes);
print $output;

I also tried this code, but I get an error.

$w = entity_metadata_wrapper('node', $node);
foreach ($w->field_objectif_bts->getIterator() as $file_w) {
  // $file_w->file returns the wrapper of the File entity. You can
  // then address it's values as:
  $name = $file_w->file->name->value();
  $mime = $file_w->file->mime->value();
  $custom_field = $file_w->file->field_objectif_bts->value();
  // etc. You can see all available fields by inspecting the
  // return value of $file_w->file->getPropertyInfo()
  if ($custom_field) {
      $header = array('data' => 'Les objectifs des formations BTS');
      $rows = array($custom_field);
      $attributes = array('id'=> 'objectif-bts','sticky' => FALSE);
    }
    $output = theme('table', array('header' => $header, 'rows'=> $rows), $attributes);
print $output;
}

The error is the following:

EntityMetadataWrapperException: Unknown data property field_objectif_bts. in EntityStructureWrapper->getPropertyInfo() (line 339 of /var/www/drupal7/sites/all/modules/entity/includes/entity.wrapper.inc).

1

There are two parts in your problem: one is getting the values, and the second is themeing them.

As far as getting the values is concerned, if you don't mind using the Entity and File Entity modules, a simpler way to access field content is to use entity metadata wrappers. This way you don't need to worry about deltas and languages.

You can write code as follows:

$w = entity_metadata_wrapper('node', $node);
foreach ($w->field_objectif_bts->getIterator() as $file_w) {
  // $file_w->file returns the wrapper of the File entity. You can
  // then address it's values as:
  $name = $file_w->file->name->value();
  $mime = $file_w->file->mime->value();
  $custom_field = $file_w->file->field_custom_field->value();
  // etc. You can see all available fields by inspecting the
  // return value of $file_w->file->getPropertyInfo()
}

As far as themeing the table goes, I think your call to theme is correct (If unsure, you can check the documentation for theme_table)

| improve this answer | |
  • it's very different as D6..so i learn about this actually and your links are useful. i am not sure about your code, is it only for theming ? or is it for getting value and theming a field ? – webmaster pf Mar 15 '13 at 11:00
  • 1
    D7 uses Entities, which is a new concept not present in D6. Your code was accessing field values on the "$node" object. I'm providing an alternative D7 way of doing this, which I consider cleaner than directly using the values on the $node object - however you can still do that in D7 if you wish. – Alice Heaton Mar 15 '13 at 11:33
  • (So the code I provided is only for accessing values on a node object. You can use/theme those as you wish) – Alice Heaton Mar 15 '13 at 11:34
  • i think i do add the file_entity module too ? – webmaster pf Mar 19 '13 at 9:20
  • Ah yes you're right - I have the File Entity module enabled too, sorry I forgot about it. I've edited the answer to add that into it. – Alice Heaton Mar 19 '13 at 9:40

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.