You are here

function simplenews_update_8203 in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 simplenews.install \simplenews_update_8203()

Convert simplenews_subscriber.subscriptions to base field.

File

./simplenews.install, line 162
Install, update and uninstall functions for the simplenews module.

Code

function simplenews_update_8203() {

  // Add new field definition.
  $field_definition = BaseFieldDefinition::create('simplenews_subscription')
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->setLabel(t('Subscriptions'))
    ->setSetting('target_type', 'simplenews_newsletter')
    ->setDisplayOptions('form', [
    'type' => 'simplenews_subscription_select',
    'weight' => '0',
    'settings' => [],
    'third_party_settings' => [],
  ]);
  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('subscriptions', 'simplenews_subscriber', 'simplenews', $field_definition);

  // Remove old configuration.
  // Cannot use FieldStorageConfig because it throws an exception due to the
  // clashing base field.
  $configurations_to_delete = [
    'field.field.simplenews_subscriber.simplenews_subscriber.subscriptions',
    'field.storage.simplenews_subscriber.subscriptions',
  ];
  $config_factory = \Drupal::configFactory();
  $dependents = \Drupal::service('config.manager')
    ->findConfigEntityDependents('config', $configurations_to_delete);
  foreach ($dependents as $config_name => $config_entity) {
    $config_entity = $config_factory
      ->getEditable($config_name);
    $dependencies = $config_entity
      ->get('dependencies.config');
    $dependencies = array_diff($dependencies, $configurations_to_delete);
    $config_entity
      ->set('dependencies.config', $dependencies);
    $config_entity
      ->save();
  }
  foreach ($configurations_to_delete as $config) {
    $config_factory
      ->getEditable($config)
      ->delete();
  }
}