Drupal 8 - Entity API - Part 2

13 June 2017

Hey there! In part 1 of this article we сreated a simple content entity “bbd team member” using Entity API with a number of fields, the ability to create/edit/delete entities and a nice view. If you haven’t read it - you can do so here.

In this article I’m going to deal with permissions and add some “more complicated” fields. Less words - more coding.  Let’s start!

 

Permissions

Add the following file bbd_team_member.yml.permissions.yml in our module directory:

view bbd_team_members:

 title: 'View bbd team members'

create bbd_team_members:

 title: 'Create bbd team members'

edit bbd_team_members:

 title: 'Edit bbd team members'

delete bbd_team_members:

 title: 'Delete bbd team members'
 

Now please add src/Access directory and

BBDTeamMemberAccessControlHandler.php file in it with next code:

 

<?php


namespace Drupal\bbd_team_member\Access;


use Drupal\Core\Access\AccessResult;

use Drupal\Core\Entity\EntityAccessControlHandler;

use Drupal\Core\Entity\EntityInterface;

use Drupal\Core\Session\AccountInterface;

 

class BBDTeamMemberAccessControlHandler extends EntityAccessControlHandler {

 

 protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {

   $access_result = AccessResult::allowedIfHasPermission($account, 'create bbd_team_members');

   return $access_result->orIf(parent::checkCreateAccess($account, $context, $entity_bundle));

 }

 

 protected function checkAccess(EntityInterface $bbd_team_member, $operation, AccountInterface $account) {

   // The parent class grants access based on the administrative permission.

   $access_result = parent::checkAccess($bbd_team_member, $operation, $account);

   switch ($operation) {

     case "view":

       // Only allow administrators to view unpublished bbd_team_members.

       $permission = 'view bbd_team_members';

       $access_result->addCacheableDependency($operation);

       break;

 

     case "update":

       $permission = 'edit bbd_team_members';

       break;

 

     case "delete":

       $permission = 'delete bbd_team_members';

       break;

 

   }

   return $access_result->orIf(AccessResult::allowedIfHasPermission($account, $permission));

 }

 

}

 

Add the following to the handlers section of the annotation in src/Entity/BBDTeamMember.php:

"access" = "Drupal\bbd_team_member\Access\BBDTeamMemberAccessControlHandler",

Code snippit

 

Run drush cache-rebuild and enjoy testing.

My steps:

  • Create role 'HR director' ( Go to admin/people/roles/add)

 

  • Add a  Jayne Ludgrove user and assign the respective role.

Permissions view

-  Login as new user, ensure that we have access to admin/content/bbd-team-member/add but not to bbd-team-member/4

- Grant necessary access to HR director – she is a boss!

 

Lets add ‘more interesting’ fields to our entity!

 

Entity Reference fields.

Go to baseFieldDefinitions() method  BBDTeamMember.php and add following above:

 

$fields['manager'] = BaseFieldDefinition::create('entity_reference')
     ->setLabel(t('Manager'))
     ->setSetting('target_type', 'user')
     ->setDisplayOptions('form', ['weight' => 25])
     ->setDisplayOptions('view', ['weight' => 20]);

 

Don't forget to update entity running drush entity-updates .

  

Form view

We have got a nice autocomplete to choose the line manager. Now we can update members view to include manager field linked to user entity. If you want manager “to be” bbd_team_member entity – no problem, just change target_type to bbd_team_member.­

  

Team member entities