1

There is a content type in my website called singer and every singer may have name and some other information. there is another content type called songs and every song has two fields: (1) File (2) name. I also created an Entity Reference field in songs which links the song to a singer. obviously every singer may have any number of linked songs.

I listed the singers in a page and it must display every singer information and songs when the user click on any of the singers. this is my template.tpl.php

function MyThere_preprocess_page(&$variables)
{
    if (isset($variables['node'])) 
   {
        switch ($variables['node']->type)
        {
        case "singer":
            $suggest = "page__node__singers";
            $variables['theme_hook_suggestions'][] = $suggest;
            $variables['name'] = t($variables['node']->field_singer_name['und'][0]['value']);
            break;           
        }
    }
}

I can get value of singer fields (using kpr()) but I don't know how to get the value of songs fields that are linked to a singer.

thanks

  • 1
    Hello. Could you make a title of your question a bit more precise? Design questions are often closed as "primarily opinion based", and you seem to ask pretty decent one, just with misleading title. – Mołot Dec 13 '13 at 9:20
5

You can try this thing

// $node represents the node object
// field_songs is the field used in content type to reference field. 
$wrapper = entity_metadata_wrapper('node', $node);
$songs = $wrapper->field_songs->value();

Now in $songs you will have each songs object and you can print what ever information you want to print from it.

| improve this answer | |
  • thanks for your reply, I searched a lot about this function , but I still don't know what $node should be. I tried $variables['node'] but this is the current node and since fields of the $songs content type don't belong to the $singer it reported error. I guess entity reference field acts as an external key, so what should I assign to the $node variable? or more precisely how do I can get the relevant node of songs that is linked to singer? – M a m a D Dec 13 '13 at 9:45
  • Go through this link: pixelite.co.nz/article/… – Aniruddhsinh Dec 13 '13 at 10:19
  • this page didn't represent any thing about getting the $node variable. – M a m a D Dec 13 '13 at 10:45
1

one approach is directly quering the referenced field table. e.g.:

function MyThere_preprocess_page(&$variables)
{
  if (isset($variables['node']))
  {
    switch ($variables['node']->type)
    {
      case "singer":
        $suggest = "page__node__singers";
        $variables['theme_hook_suggestions'][] = $suggest;
        $variables['name'] = t($variables['node']->field_singer_name['und'][0]['value']);

        // I don't know what is your table's structure, correct ref.field table col names. Because they are wrong in my example:)
        $query = db_select('node', 'n')''
        $query->join('field_data_field_MY_REF_FIELD_NAME', 'fdfr',
          'n.nid = fdfr.field_MY_REF_FIELD_NAME_NID');
        $query->fields('n', array('nid'));
        $query->condition('n.nid', $variables['node']->nid, '=');
        $query->execute()->fetchCol();

        break;
    }
  }
}
| improve this answer | |

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.