You are here

rng.install in RNG - Events and Registrations 8.2

Same filename and directory in other branches
  1. 8 rng.install
  2. 3.x rng.install

Contains install and update functions for RNG.

File

rng.install
View source
<?php

/**
 * @file
 * Contains install and update functions for RNG.
 */
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\field\Entity\FieldConfig;
use Drupal\rng\Entity\RegistrantType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\rng\Entity\EventType;
use Drupal\rng\Entity\EventTypeRule;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Add default rules to event types.
 */
function rng_update_8001() {

  // Refresh entity type cache will show rng_event_type_rule configs.
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();

  // Clear config schema cache to prevent complaining about plugin schemas
  // saved in the rules below.
  \Drupal::service('config.typed')
    ->clearCachedDefinitions();
  $config_factory = \Drupal::configFactory();
  foreach (EventType::loadMultiple() as $event_type) {

    /** @var \Drupal\rng\EventTypeInterface $event_type */
    $entity_type_id = $event_type
      ->getEventEntityTypeId();
    $bundle = $event_type
      ->getEventBundle();
    if (!$config_factory
      ->listAll('rng.rule.' . $entity_type_id . '.' . $bundle)) {
      $rule = EventTypeRule::create([
        'trigger' => 'rng_event.register',
        'entity_type' => $entity_type_id,
        'bundle' => $bundle,
        'machine_name' => 'user_role',
      ]);
      $rule
        ->setCondition('role', [
        'id' => 'rng_user_role',
        'roles' => [],
      ]);
      $rule
        ->setAction('registration_operations', [
        'id' => 'registration_operations',
        'configuration' => [
          'operations' => [
            'create' => TRUE,
          ],
        ],
      ]);
      $rule
        ->save();

      // Registrant.
      $rule = EventTypeRule::create([
        'trigger' => 'rng_event.register',
        'entity_type' => $entity_type_id,
        'bundle' => $bundle,
        'machine_name' => 'registrant',
      ]);
      $rule
        ->setCondition('identity', [
        'id' => 'rng_registration_identity',
      ]);
      $rule
        ->setAction('registration_operations', [
        'id' => 'registration_operations',
        'configuration' => [
          'operations' => [
            'view' => TRUE,
            'update' => TRUE,
          ],
        ],
      ]);
      $rule
        ->save();

      // Event managers.
      $rule = EventTypeRule::create([
        'trigger' => 'rng_event.register',
        'entity_type' => $entity_type_id,
        'bundle' => $bundle,
        'machine_name' => 'event_manager',
      ]);
      $rule
        ->setCondition('operation', [
        'id' => 'rng_event_operation',
        'operations' => [
          'manage event' => TRUE,
        ],
      ]);
      $rule
        ->setAction('registration_operations', [
        'id' => 'registration_operations',
        'configuration' => [
          'operations' => [
            'create' => TRUE,
            'view' => TRUE,
            'update' => TRUE,
            'delete' => TRUE,
          ],
        ],
      ]);
      $rule
        ->save();
    }
  }
}

/**
 * Convert registrant entity to one with bundles.
 */
function rng_update_8002() {
  $entity_manager = \Drupal::entityTypeManager();
  $config_factory = \Drupal::configFactory();
  $update_manager = \Drupal::entityDefinitionUpdateManager();

  // Install 'registrant_type' config entity.
  $entity_manager
    ->clearCachedDefinitions();
  $type = $entity_manager
    ->getDefinition('registrant_type');
  $update_manager
    ->installEntityType($type);

  // Clear config cache so new registrant type saves.
  \Drupal::service('config.typed')
    ->clearCachedDefinitions();

  // Install 'type' field.
  $field_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel('Registrant type')
    ->setSetting('target_type', 'registrant_type');
  $update_manager
    ->installFieldStorageDefinition('type', 'registrant', 'registrant', $field_definition);

  // Create the first registrant_type entity which will re-capture any
  // pre-existing fields from the formerly bundle-less entity.
  $default_registrant_type_id = 'registrant';
  $registrant_type = RegistrantType::create([
    'id' => $default_registrant_type_id,
    'label' => 'Registrant',
  ]);
  $registrant_type
    ->save();

  // Re-calculate dependencies by re-saving fields.
  $config_names = $config_factory
    ->listAll('field.field.registrant.registrant.');
  foreach ($config_names as $name) {

    // Remove prefix.
    $id = str_replace('field.field.', '', $name);
    $entity = FieldConfig::load($id);
    $entity
      ->save();
  }
}

/**
 * Update event types with new registrant and people type configuration.
 */
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();
  }
}

/**
 * Replace event_type config entity with rng_event_type entity.
 */
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);
  }
}

/**
 * Add a registrant_qty field to registrations.
 */
function rng_update_8201() {
  $definition_manager = \Drupal::entityDefinitionUpdateManager();
  $registrant_qty_field = BaseFieldDefinition::create('integer')
    ->setLabel(t('Registrant Qty'))
    ->setDescription(t('Number of registrants on this registration'))
    ->setDefaultValue(0)
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE);
  $definition_manager
    ->installFieldStorageDefinition('registrant_qty', 'registration', 'registration', $registrant_qty_field);
}

/**
 * Add a UID field to registration.
 */
function rng_update_8202() {
  $definition_manager = \Drupal::entityDefinitionUpdateManager();
  $user_field = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Owner'))
    ->setDescription(t('The owner of the registration.'))
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDefaultValueCallback('Drupal\\rng\\Entity\\Registration::getCurrentUserId')
    ->setTranslatable(TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $definition_manager
    ->installFieldStorageDefinition('uid', 'registration', 'registration', $user_field);
}

/**
 * Add status field to registrations.
 */
function rng_update_8203() {
  $definition_manager = \Drupal::entityDefinitionUpdateManager();
  $status = BaseFieldDefinition::create('boolean')
    ->setLabel(new TranslatableMarkup('Confirmed'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => 90,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $definition_manager
    ->installFieldStorageDefinition('status', 'registration', 'registration', $status);
}

/**
 * Add Event dynamic entity reference to registrant.
 */
function rng_update_8204() {
  $definition_manager = \Drupal::entityDefinitionUpdateManager();
  $event = BaseFieldDefinition::create('dynamic_entity_reference')
    ->setLabel(t('Event'))
    ->setDescription(t('The event for the registrant.'))
    ->setSetting('exclude_entity_types', 'true')
    ->setSetting('entity_type_ids', [
    'registrant',
    'registration',
  ])
    ->setDescription(t('The relationship between this registrant and an event.'))
    ->setRevisionable(TRUE)
    ->setReadOnly(TRUE);
  $definition_manager
    ->installFieldStorageDefinition('event', 'registrant', 'registrant', $event);
}

/**
 * Resave all registrants to populate event links.
 */
function rng_update_8205() {
  $storage = \Drupal::entityTypeManager()
    ->getStorage('registrant');
  $registrants = $storage
    ->loadMultiple(NULL);
  foreach ($registrants as $registrant) {
    if ($registrant
      ->id() < 73) {
      $registrant
        ->delete();
    }
    $registrant
      ->save();
  }
}

Functions

Namesort descending Description
rng_update_8001 Add default rules to event types.
rng_update_8002 Convert registrant entity to one with bundles.
rng_update_8003 Update event types with new registrant and people type configuration.
rng_update_8004 Replace event_type config entity with rng_event_type entity.
rng_update_8201 Add a registrant_qty field to registrations.
rng_update_8202 Add a UID field to registration.
rng_update_8203 Add status field to registrations.
rng_update_8204 Add Event dynamic entity reference to registrant.
rng_update_8205 Resave all registrants to populate event links.