You are here

public function RegistrantsElementUtility::peopleTypeOptions in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/RegistrantsElementUtility.php \Drupal\rng\RegistrantsElementUtility::peopleTypeOptions()
  2. 8 src/RegistrantsElementUtility.php \Drupal\rng\RegistrantsElementUtility::peopleTypeOptions()

Generate available people type options suitable for radios element.

Return value

array Options suitable for a radios element.

File

src/RegistrantsElementUtility.php, line 315

Class

RegistrantsElementUtility

Namespace

Drupal\rng

Code

public function peopleTypeOptions() {

  /** @var \Drupal\rng\EventManagerInterface $event_manager */
  $event_manager = \Drupal::service('rng.event_manager');

  /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
  $bundle_info = \Drupal::service('entity_type.bundle.info');
  $entity_type_manager = \Drupal::entityTypeManager();
  $current_user = \Drupal::currentUser();
  $event = $this->element['#event'];
  $event_meta = $event_manager
    ->getMeta($event);
  $event_type = $event_meta
    ->getEventType();
  $for_bundles = [];

  // Anonymous registrant.
  if ($event_type
    ->getAllowAnonRegistrants()) {
    $for_bundles['anon:'] = t('Registrant');
  }

  // Create.
  foreach ($this->element['#allow_creation'] as $entity_type_id => $bundles) {
    $info = $bundle_info
      ->getBundleInfo($entity_type_id);
    foreach ($bundles as $bundle) {
      if ($this
        ->entityCreateAccess($entity_type_id, $bundle)) {
        $for_bundle_key = $entity_type_id . ':' . $bundle;
        $for_bundles[$for_bundle_key] = $info[$bundle]['label'];
      }
    }
  }

  // Existing.
  foreach ($this->element['#allow_reference'] as $entity_type_id => $bundles) {
    $entity_type = $entity_type_manager
      ->getDefinition($entity_type_id);
    $info = $bundle_info
      ->getBundleInfo($entity_type_id);
    foreach ($bundles as $bundle) {
      $for_bundle_key = $entity_type_id . ':' . $bundle;
      if (isset($for_bundles[$for_bundle_key])) {

        // Skip if there an option already exists for 'create'.
        continue;
      }
      $counting_bundle = $entity_type
        ->getBundleEntityType() !== NULL ? [
        $bundle,
      ] : [];
      $existing_count = $this
        ->countReferenceableEntities($event, $entity_type_id, $counting_bundle);

      // Add myself special option.
      if ($entity_type_id === 'user' && $current_user
        ->isAuthenticated()) {
        $identity = User::load($current_user
          ->id());
        if (!$this
          ->identityExists($identity)) {
          if ($event_meta
            ->identitiesCanRegister('user', [
            $current_user
              ->id(),
          ])) {
            $existing_count--;
            $for_bundles['myself:'] = t('Myself');
          }
        }
      }
      if ($existing_count > 0) {
        $for_bundles[$for_bundle_key] = $info[$bundle]['label'];
      }
    }
  }
  return $for_bundles;
}