6

I am trying to grab all of the fields of a node (which I am doing by using array_keys). At this point, I want to feed them all to entity_metadata_wrapper(), and grab their values.

I will then, if they are strings, print them to a PDF document using TCPDF's Text() function.

Everything looks like it will work correctly; however, certain fields give an error as such:

EntityMetadataWrapperException: Unknown data property field_grade. in EntityStructureWrapper->getPropertyInfo()

The field exists in my node, though.

'field_grade' => 
    array (size=13)
      'default_value' => null
      'description' => string '' (length=0)
      'display' => 
        array (size=4)

This is the code I am using.

$wrapper = entity_metadata_wrapper('node', $node);
$trip_fields = field_info_instances('node', 'trip');
$trip_fieldskeys = array_keys($trip_fields);
$margintop = 0;

foreach($trip_fieldskeys as $item) {
  $value = $wrapper->$item->value();
  if (gettype($value) == "string") {
    $pdf->Text(0, $margintop, $value, false, false, true, 0, 0,
       '', false, '', 0, false, 'T', 'M', false);
    $margintop = $margintop + 25;
  }
}
  • Did you solve this? – artfulrobot Oct 3 '14 at 11:35
  • This was a while ago, but I believe so. I'll look up what was done and reply back here in a bit. – Jack Ryan Oct 6 '14 at 14:31
  • Is it possible that there are some nodes that you are running through entity_metadata_wrapper() that are missing a value for field_grade? That happened to me recently. – rocket Oct 24 '14 at 4:55
3

If it is a custom field that you have defined in your module (using hook_field_info()) then you need to add a property_type to your field info. See https://www.drupal.org/docs/7/api/entity-api/property-info-for-fields for an example and https://www.drupal.org/node/905580 for a full list of possible types.

If your field is a complex (multiple value) data structure you may also additionally need to include property_callbacks as well. See http://d7.thecarneyeffect.co.uk/describe-field-properties-make-custom-fields-work-entity-metadata-wrappers for a good example of this.

1

I have used FPDI Library for PDF Creation for Particular Content Type Node

    /* PDf Creationg Start */
    $pdf=new PDF_HTML_Table();  
    $pdf->AddPage('L');
    $pdf->SetXY(10.0,5.0);
    $pdf->SetDisplayMode('fullwidth');
    $pdf->SetLeftMargin(4.0);
    $pdf->SetTitle('Your Title',false);
    $pdf->SetFont('Arial','',7);
    $pdf->SetFontSize(12);
    $title_page = 'Dated : '.date('jS F Y');    
    $pdf->Text(10,28,$title_page);          
    $pdf->SetFontSize(7);
    $header=array('your_field_no1','your_field_no2','your_field_no_n..');
    $rows = array();    

  /* Get Node Content */
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'your_content_type_name')               
  ->propertyCondition('status', 1, '=')
  ->propertyOrderBy('created','DESC')
  ->fieldCondition('field_name', 'value', $_GET['mname'],'=');
  $results = $query->execute();     

    if (isset($results['node'])) {
      $nodes = node_load_multiple(array_keys($results['node']));
       $counter = 0;          
      foreach($nodes as $row) {
          $data = array();  
        if(sizeof($row->your_field_no1) > 0)
            {
                $your_data_array[] = $row->your_field_no1['und'][0]['value'];
            }

       if(sizeof($row->your_field_no2) > 0)
            {
                $your_data_array[] = $row->your_field_no2['und'][0]['value'];
            }
         // and so on..
         $rows[] = $data;
      }

    }  
   $pdf->Ln(35);
        $pdf->SetFont('Arial','',9);
        $pdf->SetWidths(array(40,40,40,40,40,40,50));
        $pdf->SetAligns('C');           
        $pdf->FilterRow($data_header);
        $pdf->SetWidths(array(40,40,40,40,40,40,50)); 
        $pdf->FilterRow($data); 
   $filename = "your_desired_name_plus_".date('jS-F-Y-h-i-s').'.pdf';
   $pdf->Output($filename,"D");

and it's working on my site... hope this helps

1

PHP fatal errors when calling $wrapper->field_foo->value() on an empty $wrapper. You need check that node have this field before call $wrapper->field_foo->value(). That Exception handling, be careful with EntityMetadataWrapper. And you can try with:

$wrapper = entity_metadata_wrapper('node', $node);// you need sure with varibale $node
foreach ($wrapper->getPropertyInfo() as $key => $val) {
    $value = $wrapper->$key->value();
    if (gettype($value) == "string") {
   $pdf->Text(0, $margintop, $value, false, false, true, 0, 0,
       '', false, '', 0, false, 'T', 'M', false);
   $margintop = $margintop + 25;
   }
  }

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.