Custom entity permission to allow editing of own entities

Note Statistics

Note Statistics

  • Viewed 244 times
  • Bookmarked 1 times
  • 1 Grateful readers
Thu, 12/19/2019 - 23:13

When creating a custom entity using

drupal generate:entity:content

there is no permission for editing own entity. Assuming your entity type is names message follow these steps to enable this permission

  1. Add the new permission to the permission file name message.permissions.yml. Add a permission
edit own message:
  title: 'Edit Own Message'
  1. Update the src/MessageAccessControlHandler::checkAccess as below

protected function checkAccess()
     switch($opreration) {
		   //...
			 case 'update':
			  if($entity -> getOwnerId() === $account ->id()) {
          return  AccessResult::allowedIfHasPermission($account, 'edit own messages');
        }
        return AccessResult::allowedIfHasPermission($account, 'edit  messages');
}
Authored by