You are here

function rng_update_8004 in RNG - Events and Registrations 8.2

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

Replace event_type config entity with rng_event_type entity.

File

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

Code

function rng_update_8004(&$sandbox) {
  $updateManager = \Drupal::entityDefinitionUpdateManager();
  $rng_event_type = $updateManager
    ->getEntityType('rng_event_type');
  if (empty($rng_event_type)) {
    $rng_event_type = new ConfigEntityType([
      'id' => 'rng_event_type',
      'label' => new TranslatableMarkup('Event type'),
      'config_prefix' => 'rng_event_type',
      'admin_permission' => 'administer event types',
      'entity_keys' => [
        'id' => 'id',
        'label' => 'label',
      ],
    ]);
    $updateManager
      ->installEntityType($rng_event_type);
  }
  $config_factory = \Drupal::configFactory();

  // Rename existing configs.
  foreach ($config_factory
    ->listAll('rng.event_type.') as $event_type) {
    $new_config = str_replace('rng.event_type.', 'rng.rng_event_type.', $event_type);
    $config_factory
      ->rename($event_type, $new_config);

    // Rule plugins depend on rng.event_type configs.
    foreach ($config_factory
      ->listAll('rng.rule.') as $rng_rule) {
      $config = $config_factory
        ->getEditable($rng_rule);
      $config_dependencies = $config
        ->get('dependencies.config') ?: [];
      $indx = array_search($event_type, $config_dependencies);
      if ($indx !== FALSE) {
        $config_dependencies[$indx] = $new_config;
        $config
          ->set('dependencies.config', $config_dependencies);
      }
      $config
        ->save();
    }
  }

  // Remove old type.
  $old_event_type = $updateManager
    ->getEntityType('event_type');
  if (!empty($old_event_type)) {
    $updateManager
      ->uninstallEntityType($old_event_type);
  }
}