You are here

function rng_update_8003 in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 rng.install \rng_update_8003()
  2. 3.x rng.install \rng_update_8003()

Update event types with new registrant and people type configuration.

File

./rng.install, line 139
Contains install and update functions for RNG.

Code

function rng_update_8003() {

  /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
  $bundle_info = \Drupal::service('entity_type.bundle.info');
  $config_factory = \Drupal::configFactory();
  $default_registrant_type_id = 'registrant';

  // Generate default people types allowing all enabled identity types.
  $people_types = [];
  $identity_types = $config_factory
    ->get('rng.settings')
    ->get('identity_types') ?: [];
  foreach ($identity_types as $entity_type) {
    $bundles = $bundle_info
      ->getBundleInfo($entity_type);
    foreach (array_keys($bundles) as $bundle) {
      $people_types[] = [
        'entity_type' => $entity_type,
        'bundle' => $bundle,
        'existing' => 1,
        'create' => 0,
        'entity_form_mode' => 'default',
      ];
    }
  }
  $config_names = $config_factory
    ->listAll('rng.event_type.');
  foreach ($config_names as $name) {
    $config = $config_factory
      ->getEditable($name);

    // Set default registrant for event types to the new bundle entity created
    // in 8002. 8002 + 8003 happen simultaneously.
    $config
      ->set('default_registrant', $default_registrant_type_id);

    // Permit referencing existing entities for all enabled identity types.
    $config
      ->set('people_types', $people_types);

    // Add a dependency on the registrant type used for 'default_registrant'.
    $id = 'rng.registrant_type.' . $default_registrant_type_id;
    $config_dependencies = $config
      ->get('dependencies.config') ?: [];
    if (!in_array($id, $config_dependencies)) {
      $config_dependencies[] = $id;
      $config
        ->set('dependencies.config', $config_dependencies);
    }
    $config
      ->save();
  }
}