2

I have a custom entity (Person) with these fields: ID, nom, prenom, telephoneportable, estactive, adherentid, userID, created, changed.
I created it with baseFieldDefinitions.

adherentid is a 'entity_reference' link to another custom content entity 'Adherent'.
userID are is a 'entity_reference' link to standard Drupal 'User' entity.

In the PersonListBuilder, I try to have these ids showed as links to the corresponding entities.

Here is the code for userID (and it's working fine):

$row['userid']['data'] = array(
'#theme' => 'username',
'#account' => $entity->getUser(),
);

What should be the code for adherentid (can't make it work so far):

$row['adherentid']['data'] = array(
'#theme' => '???',
'#account' => $entity->getAdherent(),
);

Thanks in advance

0

One possibility is to use the Link render element, and by using the canonical link for the Adherent entity. This assumes that the Adherent entity type definition has a canonical link, and that the route is defined in your module's routing YAML.

$row['adherentid']['data'] = array(
 '#type' => 'link',
 '#url' => Uri::fromRoute('entity.adherent.canonical', ['association_adherent' => $id]),
 '#title' => 'My link',
);

Another possibility is to render the field, and use the display mode that provides a link to the entity using the entity label. However this is less accessible in the documentation to look up within a reasonable time.

  • Thanks for your advice. I updated my code accordingly: $row['adherentid']['data'] = array( '#theme' => 'link', '#url' => Url::fromRoute('entity.association_adherent.canonical'), '#title' => 'My link', ); Unfortunately, I got 'Theme Hook link not found' in the log... And I don't know how to replace 'My link' by the correct adherent stuff (At least the ID) – gbmapo May 30 '16 at 14:46
  • Thanks for the reply. I fixed my answer. i forgot that link is now #type and not #theme. The link to the API page gives the correct info as well. – mradcliffe May 30 '16 at 15:45
  • with '#type' => 'link', I have now an error: Uncaught PHP Exception Symfony\Component\Routing\Exception\MissingMandatoryParametersException: "Some mandatory parameters are missing ("association_adherent") to generate a URL for route "entity.association_adherent.canonical"." – gbmapo May 31 '16 at 6:58
  • 1
    With these modifications, it works! Thanks for your help. $url = Url::fromRoute('entity.association_adherent.canonical', array('association_adherent' => $id)); $row['adherentid']['data'] = array( '#type' => 'link', '#url' => $url, '#title' => $title, ); – gbmapo May 31 '16 at 10:13
0

Finally, that worked for me:

$adherent = $entity->getAdherent();  
$id = (int) $adherent->id->value;  
$row['adherentid']['data'] = array(  
  '#type' => 'link',  
  '#url' => Url::fromRoute('entity.association_adherent.canonical', array('association_adherent' => $id)),  
  '#title' => $adherent->designation->value,  
 );  

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.