0

In Drupal 7 I am using following code in Global: PHP.

$data = message_load($row->mid);

if (isset($data->field_related_flag)){
  $fid = $data->field_related_flag['und'][0]['target_id'];
  $ftype = $data->field_flagged_node_type['und'][0]['value'];
  $flag = db_query('SELECT f.entity_id content_id, f.fid flag_id, flag.title type FROM flagging f INNER JOIN flag ON flag.fid = f.fid WHERE flagging_id = :val_fid', array(':val_fid' => $fid))-> fetchAssoc();
  $flag['content_type'] = $ftype;
  return json_encode($flag);
}

And from this code, I get the following output:

'related_flag' => '{"content_id":"410","flag_id":"1","type":"like","content_type":"event"}',

Here "content_id":"410" is the node ID.

Now I need to get the node title, description, and other fields. Can anyone please tell me what code I should write to get the desired result?

1

You can easily load the node using $node = node_load($nid);

Hoever it can be a bit tricky to get the values you want, so I recommend Entity metadata wrappers. They are part of the Entity API module and they allow you to access node variables like this:

$node = node_load($nid);
$node_wrapper = entity_metadata_wrapper('node', $node);
$node_wrapper->title->value();
$node_wrapper->field_description->value(); 

At the bottom of the linked Entity metadata wrapper page is a section on Debugging or introspecting wrappers, you can use that to find all available fields and their names.

  • Do I need to create another PHP field in view and enter this $node = node_load($nid); to load node ? Can you please explain it bit more. If you can give me complete example on how I should get the Title in output ? – Malik Jun 4 '16 at 14:10
  • No need for a separate PHP block. You can append to what you have. Your question didn't mention output. Pleae update the question to express what you want to achieve. I get the feeling you can do this much easier with views relationships. – Neograph734 Jun 4 '16 at 14:13
  • The output I am getting from the above code is ` 'related_flag' => '{"content_id":"410","flag_id":"1","type":"like","content_type":"event"}',` as I am getting Content_id, I just want to get title of this content_id ? – Malik Jun 4 '16 at 14:18
  • Extracting the nid from that line is basic PHP knowledge. But as I said it can be done with views relationships. Just explain (edit your question) what you want to achieve in the end. – Neograph734 Jun 4 '16 at 14:36

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.