You are here

function yamlform_update_8075 in YAML Form 8

Issue #2834203: Convert yamlform field target_id to 32 characters.

File

./yamlform.install, line 198
Install, update and uninstall functions for the YAML Form module.

Code

function yamlform_update_8075() {
  $database_schema = \Drupal::database()
    ->schema();
  $schema = \Drupal::keyValue('entity.storage_schema.sql')
    ->getAll();
  foreach ($schema as $item_name => $item) {
    foreach ($item as $table_name => $table_schema) {
      foreach ($table_schema as $schema_key => $schema_data) {
        if ($schema_key == 'fields') {
          foreach ($schema_data as $field_name => $field_data) {
            if (preg_match('/_target_id$/', $field_name) && $field_data['description'] == 'The ID of the form entity.' && $schema[$item_name][$table_name]['fields'][$field_name]['length'] === 255) {
              $schema[$item_name][$table_name]['fields'][$field_name]['length'] = 32;
              if ($database_schema
                ->tableExists($table_name)) {
                $database_schema
                  ->changeField($table_name, $field_name, $field_name, $schema[$item_name][$table_name]['fields'][$field_name]);
              }
            }
          }
        }
      }
    }
  }
  \Drupal::keyValue('entity.storage_schema.sql')
    ->setMultiple($schema);
}