You are here

function maestro_update_8003 in Maestro 8.2

Same name and namespace in other branches
  1. 3.x maestro.install \maestro_update_8003()

Update 8003 - Create maestro_entity_identifiers entity.

File

./maestro.install, line 68
Install, update and uninstall functions for the Maestro module.

Code

function maestro_update_8003() {

  // Check if the table exists first.  If not, then create the entity.
  if (\Drupal::service('database')
    ->schema()
    ->tableExists('maestro_entity_identifiers') === FALSE) {
    \Drupal::entityTypeManager()
      ->clearCachedDefinitions();
    \Drupal::entityDefinitionUpdateManager()
      ->installEntityType(\Drupal::entityTypeManager()
      ->getDefinition('maestro_entity_identifiers'));

    // Now to update the new entity identifiers entity with any existing entity_identifiers data.
    // we will assume that all of the identifiers have an entity type of 'node'.
    $query = \Drupal::entityQuery('maestro_process_variables')
      ->condition('variable_name', 'entity_identifiers');
    $entityIDs = $query
      ->execute();
    foreach ($entityIDs as $entity_id) {
      $record = \Drupal::entityTypeManager()
        ->getStorage('maestro_process_variables')
        ->load($entity_id);
      if ($record) {

        // Inject into the new maestro_entity_identifiers entity.
        if ($record->variable_value
          ->getString() != '') {

          // Parse out the entity entries.
          $arr = explode(',', $record->variable_value
            ->getString());
          foreach ($arr as $eids) {
            $entry = explode(':', $eids);
            $entityType = $entry[0];
            $uniqueID = $entry[1];
            $entityID = $entry[2];
            $values = [
              'process_id' => $record->process_id
                ->getString(),
              'unique_id' => $uniqueID,
              'entity_type' => $entityType,
              'entity_id' => $entityID,
              // we're forcing it to node as this is all we've had implemented as of this writing.
              'bundle' => 'node',
            ];
            $new_entry = \Drupal::entityTypeManager()
              ->getStorage('maestro_entity_identifiers')
              ->create($values);
            $new_entry
              ->save();
          }
        }
      }
    }
  }
  else {
    return 'Entity Identifiers entity already exists';
  }
}