Referencing roles in Drupal 7/8 entities

This is the post about roles, references, bicycles and pain fun.

Drupal 7 solution

On Drupal 7 we have a pretty easy solution - Rolereference module. It’s installation and configuration is pretty straightforward. This module provides a nodereference-like field with a bunch of standard formatters. Also, this module has common views integration. Although, this module is not designed to save a humanity it’s still pretty useful for some range of cases. Especially when you need to maintain catalogues with filtering by role.

Drupal 8 solution

Not long time ago our team decided to port the module to Drupal 8. I volunteered to do the port. I was full of energy and immediately jumped into the coding process. In few days the Drupal 8 version was ready. Despite the fact that Drupal 8 does not require any module to maintain a reference to the role. Yes, yes, it has such possibility in its damn core. Anyway, it was a really exciting experience. Inventing a bicycle is always fun.

Short how-to for those who want to save a bit of time.

1. First of all go to structure/types/manage/[CONTENT_TYPE]/fields.

2. Add a Reference field with “Other” option as a target.

Role reference Drupal 8 (field)

Third and the last step is to select a Role as a “Type of item to reference”

Role reference Drupal 8
That’s it. I want to send special greetings to my teammates who knew the truth and did not tell me.