
Drupal 8 - Entity Reference View to Display User Profile Values
Published: 3/24/2018
Use case: I have a custom entity, Department. I have another custom entity, Unit. These entities are related to one another such that Units belong to Departments, and the Unit src/Entity/Unit.php file contains an entity reference field to Department. So, I may have a Department named 'Information Technology,' and within that Department are Units named 'Integration' and 'Application Support.' It is a parent-child, one-to-many relationship.
Department is also a field that is assigned to User accounts. In the User account, this is field is named 'Manage Departments' (field_manage_department). When I add a User, I select one or more Departments that this User manages. To visually demonstrate the relationship, I have created the below table:
User A
User B
Department
- Unit
Information Technology
- Integration
- App Support
Finance
- Accounts Payable
- Accounts Receivable
Department
- Unit
Human Resources
- Recruitment
- Benefits
Let's say that Users have the ability to create new Units, but we don't want them to be able to select a Department that they don't have access to (one that is not assigned to them in their profile). If User A needs to add a new Unit to the Information Technology department, we don't want the Add Unit form to allow User A to select Finance or Human Resources.
There may be many ways to accomplish this level of security, but one way is through an Entity Reference view that is then used as the Handler for the Department field in src/Entity/Unit.php.
I created an Entity Reference view of content Department. The only field I added was the Department name, and I sorted ASC on the name. I knew I needed some type of Relationship to the User, but the key was choosing the correct Relationship. Rather than choosing the relationship User, I chose User using field_manage_department and required the relationship:
I then added Filter criteria for Current User from the relationship:
Here is a full screenshot of the View. This returned a list of only the Departments that the current user has access to.
NOTE: The Departments in the next two screenshots are different from the examples we used above, but you get the idea...
Here is the User profile, which got a little help from the Multiselect Module (opens in new tab):
Then the entity file was adjusted as such:
$fields['departmentid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Department'))
->setDescription(t('Specify a department'))
->setSetting('target_type', 'timeoff_department')
->setSetting('handler', 'views')
->setSetting('handler_settings', ['view' =>
['view_name' =>'filtered_departments',
'display_name' =>'entity_reference_1',
'arguments' => []
]
])
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -3,
))
->setDisplayOptions('form', array(
'type' => 'options_select',
'weight' => -3
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
