You are here

public function Post::setVisibility in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_post/src/Entity/Post.php \Drupal\social_post\Entity\Post::setVisibility()
  2. 8.4 modules/social_features/social_post/src/Entity/Post.php \Drupal\social_post\Entity\Post::setVisibility()
  3. 8.5 modules/social_features/social_post/src/Entity/Post.php \Drupal\social_post\Entity\Post::setVisibility()
  4. 8.6 modules/social_features/social_post/src/Entity/Post.php \Drupal\social_post\Entity\Post::setVisibility()
  5. 8.7 modules/social_features/social_post/src/Entity/Post.php \Drupal\social_post\Entity\Post::setVisibility()
  6. 8.8 modules/social_features/social_post/src/Entity/Post.php \Drupal\social_post\Entity\Post::setVisibility()
  7. 10.3.x modules/social_features/social_post/src/Entity/Post.php \Drupal\social_post\Entity\Post::setVisibility()
  8. 10.0.x modules/social_features/social_post/src/Entity/Post.php \Drupal\social_post\Entity\Post::setVisibility()
  9. 10.2.x modules/social_features/social_post/src/Entity/Post.php \Drupal\social_post\Entity\Post::setVisibility()

File

modules/social_features/social_post/src/Entity/Post.php, line 260

Class

Post
Defines the Post entity.

Namespace

Drupal\social_post\Entity

Code

public function setVisibility($visibility) {
  $allowed_values = $this
    ->getPostVisibilityAllowedValues();
  $visibility_label = $this
    ->getDefaultVisibilityByLabel($visibility, TRUE);
  if (!$visibility_label) {

    /** @var \Drupal\user\RoleInterface $role */
    $role = $this
      ->entityTypeManager()
      ->getStorage('user_role')
      ->load($visibility);
    if ($role instanceof RoleInterface) {
      foreach ($allowed_values as $key => $value) {
        if ($value['label'] === $role
          ->label()) {
          $this
            ->set('field_visibility', $key);
        }
      }
    }
  }
  else {
    foreach ($allowed_values as $key => $allowed_value) {
      if ($visibility_label == $allowed_value['label']) {
        $this
          ->set('field_visibility', (int) $allowed_value['value']);
      }
    }
  }
  return $this;
}