You are here

entity_share_client.install in Entity Share 8.3

File

modules/entity_share_client/entity_share_client.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Entity Share Client module.
 */
declare (strict_types=1);
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_share_client\ClientAuthorization\ClientAuthorizationInterface;
use Drupal\entity_share_client\Entity\Remote;
use Drupal\Core\Utility\UpdateException;

/**
 * Implements hook_uninstall().
 */
function entity_share_client_uninstall() {

  // Delete remote websites locally stored authentication credentials.
  $key_value_store = \Drupal::keyValue(ClientAuthorizationInterface::LOCAL_STORAGE_KEY_VALUE_COLLECTION);
  $key_value_store
    ->deleteAll();
}

/**
 * Install the "Entity import status" entity type.
 */
function entity_share_client_update_8301() {
  \Drupal::entityDefinitionUpdateManager()
    ->installEntityType(new ContentEntityType([
    'id' => 'entity_import_status',
    'label' => new TranslatableMarkup('Entity import status'),
    'base_table' => 'entity_import_status',
    'entity_keys' => [
      'id' => 'id',
      'langcode' => 'langcode',
    ],
  ]));
}

/**
 * Install the "Import config" entity type.
 */
function entity_share_client_update_8302() {
  \Drupal::entityDefinitionUpdateManager()
    ->installEntityType(new ConfigEntityType([
    'id' => 'import_config',
    'label' => new TranslatableMarkup('Import config'),
    'entity_keys' => [
      'id' => 'id',
      'label' => 'label',
      'uuid' => 'uuid',
    ],
  ]));
}

/**
 * Move any basic auth credentials stored in configuration into the new plugin.
 */
function entity_share_client_update_8303() {

  /** @var \Drupal\entity_share_client\ClientAuthorization\ClientAuthorizationPluginManager $manager */
  $manager = \Drupal::service('plugin.manager.entity_share_client_authorization');
  $state = \Drupal::state();

  // Iterate on remotes.

  /** @var \Drupal\entity_share_client\Entity\RemoteInterface[] $remotes */
  $remotes = Remote::loadMultiple();
  foreach ($remotes as $remote) {

    // Check that the remote config had not already been converted into the new
    // plugin system due to hook_update renaming.
    if (!is_null($remote
      ->get('auth'))) {
      continue;
    }
    if (!empty($remote
      ->get('basic_auth_username')) && !empty($remote
      ->get('basic_auth_password'))) {

      /** @var \Drupal\entity_share_client\Plugin\ClientAuthorization\BasicAuth $plugin */
      $plugin = $manager
        ->createInstance('basic_auth');
      $configuration = $plugin
        ->getConfiguration();

      // For the update. Init plugin UUID with the same UUID as the remote
      // entity.
      $configuration['uuid'] = $remote
        ->uuid();
      $credentials = [];
      $credentials['username'] = $remote
        ->get('basic_auth_username');
      $credentials['password'] = $remote
        ->get('basic_auth_password');
      $state
        ->set($configuration['uuid'], $credentials);
    }
    else {

      /** @var \Drupal\entity_share_client\Plugin\ClientAuthorization\Anonymous $plugin */
      $plugin = $manager
        ->createInstance('anonymous');
      $configuration = $plugin
        ->getConfiguration();

      // For the update. Init plugin UUID with the same UUID as the remote
      // entity.
      $configuration['uuid'] = $remote
        ->uuid();
    }
    unset($remote->basic_auth_username);
    unset($remote->basic_auth_password);
    $configuration['data'] = [
      'credential_provider' => 'entity_share',
      'storage_key' => $configuration['uuid'],
    ];
    $plugin
      ->setConfiguration($configuration);
    $remote
      ->mergePluginConfig($plugin);
    $remote
      ->save();
  }
}

/**
 * Adding the default 'rename' value to the physical file import config.
 */
function entity_share_client_update_8304() {
  try {

    /** @var \Drupal\entity_share_client\Entity\ImportConfig[] $import_config_items */
    $import_config_items = \Drupal::entityTypeManager()
      ->getStorage('import_config')
      ->loadMultiple();
    foreach ($import_config_items as $item) {
      $settings = $item
        ->get('import_processor_settings');
      if (isset($settings['physical_file']) && !isset($settings['physical_file']['rename'])) {
        $settings['physical_file']['rename'] = FALSE;
        $item
          ->set('import_processor_settings', $settings)
          ->save();
      }
    }
  } catch (Exception $exception) {
    throw new UpdateException($exception
      ->getMessage());
  }
}

/**
 * Install module Entity Share Diff.
 *
 * Its functionality was previously a part of Entity Share Client.
 */
function entity_share_client_update_8305() {
  if (\Drupal::moduleHandler()
    ->moduleExists('diff')) {
    \Drupal::service('module_installer')
      ->install([
      'entity_share_diff',
    ]);
  }
}

Functions

Namesort descending Description
entity_share_client_uninstall Implements hook_uninstall().
entity_share_client_update_8301 Install the "Entity import status" entity type.
entity_share_client_update_8302 Install the "Import config" entity type.
entity_share_client_update_8303 Move any basic auth credentials stored in configuration into the new plugin.
entity_share_client_update_8304 Adding the default 'rename' value to the physical file import config.
entity_share_client_update_8305 Install module Entity Share Diff.