You are here

public static function LearningPathAccess::membershipPreSave in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/LearningPathAccess.php \Drupal\opigno_learning_path\LearningPathAccess::membershipPreSave()

Sets roles on membership presave.

Parameters

\Drupal\Core\Entity\EntityInterface $membership: Membership object.

1 call to LearningPathAccess::membershipPreSave()
opigno_learning_path_entity_presave in ./opigno_learning_path.module
Implements hook_entity_presave().

File

src/LearningPathAccess.php, line 181

Class

LearningPathAccess
Class LearningPathAccess.

Namespace

Drupal\opigno_learning_path

Code

public static function membershipPreSave(EntityInterface &$membership) {
  if ($membership
    ->isNew()) {

    /** @var \Drupal\group\Entity\GroupContentInterface $membership */
    $group = $membership
      ->getGroup();
    $group_is_semiprivate = $group
      ->hasField('field_learning_path_visibility') && $group
      ->get('field_learning_path_visibility')
      ->getValue() === 'semiprivate';
    $group_requires_validation = $group
      ->hasField('field_requires_validation') && $group
      ->get('field_requires_validation')
      ->getValue();
    $user_join = $membership
      ->getEntity()
      ->id() == $membership
      ->getOwnerId();
    if (!($group_is_semiprivate && $group_requires_validation) && $user_join) {

      // Add 'student' role if user is self-join group
      // and group is not semiprivate with validation.
      $roles = $membership
        ->get('group_roles')
        ->getValue();
      if (!in_array('learning_path-student', $roles)) {
        $roles[] = 'learning_path-student';
        $membership
          ->set('group_roles', $roles);
      }
    }
  }
  LearningPathAccess::setLearningPathCourseMember($membership, 'update');
}