Skip to content

Commit

Permalink
Merge pull request #603 from City-of-Helsinki/UFH-X-trim-links
Browse files Browse the repository at this point in the history
UHF-X: trim links
  • Loading branch information
hyrsky authored Nov 19, 2024
2 parents 5aa591e + 364a4fe commit 55ee443
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

declare(strict_types=1);

use Drupal\Core\Entity\TranslatableInterface;
use Drupal\media\Entity\Media;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateMessage;
Expand Down Expand Up @@ -365,3 +366,68 @@ 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');

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

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

assert($entity instanceof TranslatableInterface);
foreach ($entity->getTranslationLanguages() as $language) {
$entity = $entity->getTranslation($language->getId());

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

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

// Re-enable entity_usage module.
$entityUsageConfig->set('track_enabled_source_entity_types', $trackEnabledSourceEntityTypes);
$entityUsageConfig->save();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ process:
plugin: callback
callable: _helfi_rekry_content_add_schema
source: link_to_presentation
- plugin: callback
callable: trim
field_organization/target_id:
plugin: entity_lookup
entity_type: taxonomy_term
Expand Down

0 comments on commit 55ee443

Please sign in to comment.