Skip to content

Commit

Permalink
Fix links in database
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Nov 18, 2024
1 parent d575af1 commit 5e9d601
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,69 @@ function helfi_rekry_content_update_9009(): void {
->dropTable($table_name);
}
}

/**
* Trim links.
*/
function helfi_rekry_content_update_9010(): void {
$fields = [
'node' => [
'field_link_to_presentation',
],
];

$config = \Drupal::configFactory();
$entityUsageConfig = $config
->getEditable('entity_usage.settings');

$trackEnabledSourceEntityTypes = $entityUsageConfig
->get('track_enabled_source_entity_types');

// Disable entity_usage module.
$entityUsageConfig->set('track_enabled_source_entity_types', []);
$entityUsageConfig->save();

$entityTypeManager = \Drupal::entityTypeManager();

foreach ($fields as $entityType => $fields) {
foreach ($fields as $fieldName) {
$query = $entityTypeManager
->getStorage($entityType)
->getQuery();

$conditionGroup = $query->orConditionGroup();
$conditionGroup
->condition($fieldName, '% ', 'LIKE')
->condition($fieldName, ' %', 'LIKE');

$query
->exists($fieldName)
->condition($conditionGroup)
->accessCheck(FALSE);

$ids = $query->execute();

Check failure on line 408 in public/modules/custom/helfi_rekry_content/helfi_rekry_content.install

View workflow job for this annotation

GitHub Actions / tests

Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. Call \Drupal\Core\Entity\Query\QueryInterface::accessCheck() with TRUE or FALSE to specify whether access should be checked.

foreach ($ids as $id) {
$entity = $entityTypeManager->getStorage($entityType)
->load($id);

assert($entity instanceof TranslatableInterface);

Check failure on line 414 in public/modules/custom/helfi_rekry_content/helfi_rekry_content.install

View workflow job for this annotation

GitHub Actions / tests

Class TranslatableInterface not found.
foreach ($entity->getTranslationLanguages() as $language) {

Check failure on line 415 in public/modules/custom/helfi_rekry_content/helfi_rekry_content.install

View workflow job for this annotation

GitHub Actions / tests

Call to method getTranslationLanguages() on an unknown class TranslatableInterface.
$entity = $entity->getTranslation($language->getId());

Check failure on line 416 in public/modules/custom/helfi_rekry_content/helfi_rekry_content.install

View workflow job for this annotation

GitHub Actions / tests

Call to method getTranslation() on an unknown class TranslatableInterface.

// Trim urls.
$links = array_map(static fn ($link) => array_merge($link, [
'uri' => trim($link['uri']),
]), $entity->get($fieldName)->getValue());

Check failure on line 421 in public/modules/custom/helfi_rekry_content/helfi_rekry_content.install

View workflow job for this annotation

GitHub Actions / tests

Call to method get() on an unknown class TranslatableInterface.

$entity->set($fieldName, $links);

Check failure on line 423 in public/modules/custom/helfi_rekry_content/helfi_rekry_content.install

View workflow job for this annotation

GitHub Actions / tests

Call to method set() on an unknown class TranslatableInterface.
$entity->save();

Check failure on line 424 in public/modules/custom/helfi_rekry_content/helfi_rekry_content.install

View workflow job for this annotation

GitHub Actions / tests

Call to method save() on an unknown class TranslatableInterface.
}
}
}

// Re-enable entity_usage module.
$entityUsageConfig->set('track_enabled_source_entity_types', $trackEnabledSourceEntityTypes);
$entityUsageConfig->save();
}
}

0 comments on commit 5e9d601

Please sign in to comment.