56 lines
1.5 KiB
Text
56 lines
1.5 KiB
Text
<?php
|
|
|
|
/**
|
|
* Entity query helper.
|
|
*/
|
|
function migration_helpers_entity_query(
|
|
$entity_type = 'node',
|
|
$bundle = NULL,
|
|
$field = NULL,
|
|
) {
|
|
|
|
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
|
|
$entity_type_manager = \Drupal::service('entity_type.manager');
|
|
|
|
$entity_storage = $entity_type_manager->getStorage($entity_type);
|
|
|
|
$entity_query = $entity_storage->getQuery();
|
|
|
|
if ($bundle) {
|
|
$bundle_key = $entity_storage->getEntityType()->getKey('bundle');
|
|
$entity_query->condition($bundle_key, $bundle);
|
|
}
|
|
|
|
if ($field) {
|
|
// Check that field BOTH is present on entity bundle AND has a value saved.
|
|
$entity_query->exists($field);
|
|
}
|
|
|
|
$entity_query->accessCheck(FALSE);
|
|
|
|
$entity_ids = $entity_query->execute();
|
|
|
|
return $entity_type_manager->getStorage($entity_type)->loadMultiple($entity_ids);
|
|
|
|
}
|
|
|
|
|
|
// The below almost serve more as examples that can be followed
|
|
|
|
function migration_helpers_text_list_to_boolean($old_field_name, $new_field_name, $bundle, $entity_type = 'node', $value_map = NULL) {
|
|
|
|
$value_map = $value_map ?? [
|
|
'Yes' => TRUE,
|
|
'No' => FALSE,
|
|
];
|
|
|
|
$nodes = migration_helpers_entity_query($entity_type, $bundle, $old_field_name);
|
|
foreach ($nodes as $node) {
|
|
/** @var \Drupal\node\NodeInterface $node */
|
|
$orig_value = trim($node->$old_field_name->value);
|
|
$new_value = $value_map[$orig_value] ?? (boolean) $orig_value;
|
|
$node->set($new_field_name, $new_value);
|
|
$node->save();
|
|
}
|
|
}
|
|
|