function field_inheritance_update_8002 in Field Inheritance 2.0.x
Same name and namespace in other branches
- 8 field_inheritance.install \field_inheritance_update_8002()
Update existing inheritances from old naming format.
File
- ./
field_inheritance.install, line 28 - Installation and update hooks for the field_inheritance module.
Code
function field_inheritance_update_8002() {
$inheritances = \Drupal::entityTypeManager()
->getStorage('field_inheritance')
->loadMultiple();
if (!empty($inheritances)) {
foreach ($inheritances as $inheritance) {
// Mimic the MachineName functionality to generate a machine name.
$label = \Drupal::service('transliteration')
->transliterate($inheritance
->get('label'), LanguageInterface::LANGCODE_DEFAULT, '_');
$label = strtolower($label);
$label = preg_replace('/[^a-z0-9_]+/', '_', $label);
$id = preg_replace('/_+/', '_', $label);
if ($inheritance
->get('id') !== $inheritance
->destinationEntityType() . '_' . $inheritance
->destinationEntityBundle() . '_' . $id) {
$new_inheritance = \Drupal::entityTypeManager()
->getStorage('field_inheritance')
->create([
'id' => $id,
'label' => $inheritance
->get('label'),
'type' => $inheritance
->get('type'),
'sourceEntityType' => $inheritance
->sourceEntityType(),
'sourceEntityBundle' => $inheritance
->sourceEntityBundle(),
'sourceField' => $inheritance
->sourceField(),
'destinationEntityType' => $inheritance
->destinationEntityType(),
'destinationEntityBundle' => $inheritance
->destinationEntityBundle(),
'destinationField' => $inheritance
->destinationField(),
'plugin' => $inheritance
->plugin(),
]);
$new_inheritance
->save();
$inheritance
->delete();
}
}
}
}