1

I have a Content Type Courses & a User Role Student

In the student I used EntityReference to refer to a number of courses.

Is it possible to get a list of all the students that have a particular course referenced to them, in PHP?

Right now I'm only accustomed in getting a value from a field in a content type like this:

$user_surname = $profile_info['student']->field_surname['und'][0]['value'];

PS I think this has to do with back-reference,which is a module I already installed.

  • Yes, you can achieve that using the Views module. – Krishna Mohan Oct 31 '14 at 7:44
  • You really should not be manually accessing field_surname, you should be using field_get_items – Letharion Oct 31 '14 at 8:11
4

You could do this with views (without PHP), but if you want to use PHP you should check out Entity Field Query

You will have to do something like this:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
  ->propertyCondition('status', NODE_PUBLISHED)
  ->fieldCondition('your_field_course', 'target_id', $nid, '=')
  ->addMetaData('account', user_load(1)); // Run the query as user 1.
$result = $query->execute();
if (isset($result['user'])) {
  $uids = array_keys($result['user']);
  $users = entity_load('user', $uids);
}

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.