You are here

class AvailableUserRolesService in Multiple Registration 8

Same name and namespace in other branches
  1. 8.2 src/AvailableUserRolesService.php \Drupal\multiple_registration\AvailableUserRolesService
  2. 3.x src/AvailableUserRolesService.php \Drupal\multiple_registration\AvailableUserRolesService

Class AvailableUserRolesService.

@package Drupal\multiple_registration

Hierarchy

Expanded class hierarchy of AvailableUserRolesService

2 files declare their use of AvailableUserRolesService
MultipleRegistrationController.php in src/Controller/MultipleRegistrationController.php
MultipleRegistrationLocalTasks.php in src/Plugin/Derivative/MultipleRegistrationLocalTasks.php
1 string reference to 'AvailableUserRolesService'
multiple_registration.services.yml in ./multiple_registration.services.yml
multiple_registration.services.yml
1 service uses AvailableUserRolesService
multiple_registration.service in ./multiple_registration.services.yml
Drupal\multiple_registration\AvailableUserRolesService

File

src/AvailableUserRolesService.php, line 14

Namespace

Drupal\multiple_registration
View source
class AvailableUserRolesService {

  /**
   * The role storage used when changing the admin role.
   *
   * @var \Drupal\user\RoleStorageInterface
   */
  protected $entityTypeManager;
  protected $configFactory;

  /**
   * AvailableUserRolesService constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
   *   EntityTypeManager Service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   Config factory.
   */
  public function __construct(EntityTypeManager $entityTypeManager, ConfigFactoryInterface $configFactory) {
    $this->entityTypeManager = $entityTypeManager;
    $this->configFactory = $configFactory;
  }

  /**
   * Get all roles with ability to create registration page.
   *
   * @return array
   *   Returns avaliable roles array.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getAvailableRoles() {
    $roles = user_role_names();
    $role_storage = $this->entityTypeManager
      ->getStorage('user_role');
    $admin_role = $role_storage
      ->getQuery()
      ->condition('is_admin', TRUE)
      ->execute();
    $admin_role = reset($admin_role);
    $notAvalible = [
      AccountInterface::ANONYMOUS_ROLE => $roles[AccountInterface::ANONYMOUS_ROLE],
      AccountInterface::AUTHENTICATED_ROLE => $roles[AccountInterface::AUTHENTICATED_ROLE],
      $admin_role => $roles[$admin_role],
    ];
    return array_diff_assoc($roles, $notAvalible);
  }

  /**
   * Get all role ids for whom registration forms was created.
   *
   * @return mixed
   *   If registration forms exists, array of paths.
   *   In other situation - FALSE.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function getRegistrationPages() {
    $roles = $this
      ->getAvailableRoles();
    if (!empty($roles)) {
      $pages_config = $this->configFactory
        ->getEditable('multiple_registration.create_registration_page_form_config');
      $reg_pages = [];
      foreach ($roles as $rid => $role_name) {
        if ($url = $pages_config
          ->get('multiple_registration_url_' . $rid)) {
          $isHidden = $pages_config
            ->get('multiple_registration_hidden_' . $rid);
          $reg_pages[$rid]['url'] = $url;
          $reg_pages[$rid]['role_name'] = $role_name;
          $reg_pages[$rid]['hidden'] = $isHidden;
        }
      }
      return $reg_pages;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvailableUserRolesService::$configFactory protected property
AvailableUserRolesService::$entityTypeManager protected property The role storage used when changing the admin role.
AvailableUserRolesService::getAvailableRoles public function Get all roles with ability to create registration page.
AvailableUserRolesService::getRegistrationPages public function Get all role ids for whom registration forms was created.
AvailableUserRolesService::__construct public function AvailableUserRolesService constructor.