You are here

salesforce_mapping.install in Salesforce Suite 5.0.x

Install file.

File

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

/**
 * @file
 * Install file.
 */
use Drupal\salesforce_mapping\Entity\SalesforceMapping;
use Drupal\views\Views;

/**
 * Copy entity_id-entity_type_id data into new mapped_entity field.
 */
function salesforce_mapping_update_8001(&$sandbox) {
  if (!\Drupal::moduleHandler()
    ->moduleExists('dynamic_entity_reference')) {
    throw new \Exception('Please enable new dependency dynamic_entity_reference before continuing.');
  }
  if (!\Drupal::database()
    ->schema()
    ->fieldExists('salesforce_mapped_object', 'drupal_entity__target_id')) {
    throw new \Exception('Refused to run ' . __FUNCTION__ . ' with pending entity definition updates. Please run `entup` first.');
  }
  if (empty($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_id'] = 0;
    $sandbox['max'] = \Drupal::service('database')
      ->query("SELECT count(*) FROM {salesforce_mapped_object}")
      ->fetchField();
    if (empty($sandbox['max'])) {
      $sandbox['#finished'] = 1;
      return t('No mapped objects to update.');
    }
  }

  // Have to go directly to the database for the entity values because they've
  // been removed from baseFieldDefinitions, therefore they don't get attached
  // to the entity on load.
  $mapped_objects = \Drupal::service('database')
    ->query("SELECT id, entity_type_id, entity_id FROM {salesforce_mapped_object} WHERE id > {$sandbox['current_id']} ORDER BY id ASC LIMIT 3");
  foreach ($mapped_objects as $mapped_object_data) {
    $sandbox['current_id'] = $mapped_object_data->id;
    $sandbox['progress']++;
    $mapped_object = \Drupal::entityTypeManager()
      ->getStorage('salesforce_mapped_object')
      ->load($mapped_object_data->id);
    $mapped_object->drupal_entity
      ->setValue([
      'target_type' => $mapped_object_data->entity_type_id,
      'target_id' => $mapped_object_data->entity_id,
    ]);
    $mapped_object
      ->save();
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished'] >= 1) {
    return t('Mapped object update complete.');
  }
  return t('Updated !n of !max mapped objects.', [
    '!n' => $sandbox['progress'],
    '!max' => $sandbox['max'],
  ]);
}

/**
 * Drop vestigial fields manually, since entity update refuses to do it.
 */
function salesforce_mapping_update_8002() {
  $database = \Drupal::service('database');
  try {

    // Drop this index if it exists.
    $database
      ->schema()
      ->dropIndex('salesforce_mapped_object', 'entity__mapping');
  } catch (\Exception $e) {

    // Noop.
  }
  $database
    ->schema()
    ->dropField('salesforce_mapped_object', 'entity_id');
  $database
    ->schema()
    ->dropField('salesforce_mapped_object', 'entity_type_id');
  $database
    ->schema()
    ->dropField('salesforce_mapped_object_revision', 'entity_id');
  $database
    ->schema()
    ->dropField('salesforce_mapped_object_revision', 'entity_type_id');
}

/**
 * Add new "always_upsert" field to all existing mappings.
 */
function salesforce_mapping_update_8003() {
  foreach (SalesforceMapping::loadMultiple() as $mapping) {
    if ($mapping
      ->hasKey() && $mapping
      ->get('always_upsert') !== FALSE) {
      $mapping
        ->set('always_upsert', TRUE)
        ->save();
    }
  }
}

/**
 * Verify Salesforce admin content permissions.
 */
function salesforce_mapping_update_8004() {
  try {
    $view = Views::getView('salesforce_mapped_objects');
  } catch (\Exception $e) {
    return t('Exception while loading Salesforce admin view.');
  }
  if (!$view) {
    return t('Salesforce objects admin view not found');
  }

  // If access has been changed from defaults, we're done.
  $options = $view
    ->getDisplay()
    ->getOption('access');
  if (!empty($options['type']) && $options['type'] != 'none') {
    return t('Salesforce admin permissions confirmed.');
  }

  // Otherwise, set access.
  $view
    ->getDisplay()
    ->setOption('access', [
    'type' => 'perm',
    'options' => [
      'perm' => 'administer salesforce mapped objects',
    ],
  ]);
  $view
    ->save();
  return t('Salesforce admin permissions updated. You may wish to review your user permission settings.');
}

Functions

Namesort descending Description
salesforce_mapping_update_8001 Copy entity_id-entity_type_id data into new mapped_entity field.
salesforce_mapping_update_8002 Drop vestigial fields manually, since entity update refuses to do it.
salesforce_mapping_update_8003 Add new "always_upsert" field to all existing mappings.
salesforce_mapping_update_8004 Verify Salesforce admin content permissions.