-1

I keep getting this error:

Fatal error: Cannot use object of type EntityDrupalWrapper as array in /sites/all/modules/rules/modules/php.eval.inc(146) : eval()'d code on line 29

when I use the below PHP code in rule

$t_exists = 0;
$etid = 0;
$vid =10;
$vname = 'emails';

$wrapper = entity_metadata_wrapper('node', $node);
$email_term = $wrapper->field_primary_email->value();

function customs_create_taxonomy_term($email_term, $vid) {
  $term = new stdClass();
  $term->name = $email_term;
  $term->vid = $vid;
  taxonomy_term_save($term);
  return $term->tid;
 }

function et_exist($vname, $email_term) {
$tid = NULL;
$vocabulary = taxonomy_vocabulary_machine_name_load($vname);
$terms = taxonomy_get_tree($vocabulary->vid);
    foreach ($terms as $term) {
      if ($term->name == $email_term) {
        $tid = $term->tid;
      }
    }
return $tid;
}

if ($wrapper->field_email_term_reference[0]->value()) {
foreach ($wrapper->field_email_term_reference->getIterator() as $index => $taxWrapper) {
  $taxWrapper->value()->name;
  $taxWrapper->value()->tid;
  $taxWrapper->value()->vid;
  $taxWrapper->value()->vocabulary_machine_name; 
if ($taxWrapper->value()->name == $email_term) {
$t_exists=1;
};
}

if (!$t_exists) {
$etid = et_exist($vname, $email_term);
If ($etid) {
$wrapper->field_email_term_reference[] = $etid;
} else {
$etid = customs_create_taxonomy_term($email_term, $vid);
$wrapper->field_email_term_reference[] = $etid;
}
$wrapper->save();
}; 
}

The line that seems to be causing the problem is:

if ($wrapper->field_email_term_reference[0]->value()) {

Please advice.

  • Which line is causing the problem? – Clive Apr 20 '17 at 15:58
  • The error mentions Line 29 as the cause – Girish Cholayill Apr 20 '17 at 16:09
  • And what's on line 29? You must remember this is a Q+A site, not an excuse to forgo basic debugging! – Clive Apr 20 '17 at 16:30
  • I believe this is the line causing the error: if ($wrapper->field_email_term_reference[0]->value()) { Sorry for the confusion, I am not a developer, do not know much about coding, trying to learn it with help from sites like these. I appreciate you trying to help me out – Girish Cholayill Apr 20 '17 at 16:35
  • No apology necessary, we just need to be clear about the real source of the issue to a) make sure you get a valid answer without things devolving into a support session in the comments, and b) make sure people with a similar problem in future will be able to identify that this one is indeed similar to theirs :) – Clive Apr 20 '17 at 20:20
0

The code is trying to access an EntityDrupalWrapper as if it was an EntityValueWrapper, which might suggest the field simply doesn't have a value for the entity that's throwing the error.

Since you're explicitly getting the first field item anyway, changing the if statement to something like this would probably fix it:

if ($wrapper->field_email_term_reference->value()) {
  • when I replace the code, it gives me the same error, but now eval()'d code on line 43, which is: $wrapper->field_email_term_reference[] = $etid; I am truly lost. What does this error mean? – Girish Cholayill Apr 20 '17 at 20:38
  • Possibly that the field was added after nodes of the type it was added to already existed. That sometimes throws entity metadata wrappers out of whack. I can only guess though, you need someone with access to the code and database to debug this really, there aren't many general pointers to give – Clive Apr 21 '17 at 10:30

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.