Created
October 6, 2017 05:39
-
-
Save nikunjkotecha/1484f2be23b7a5e4a92a7ec71bb25fed to your computer and use it in GitHub Desktop.
Example to demonstrate how to update field config in update hooks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Contains install, update, uninstall hooks for module. | |
*/ | |
use Drupal\field\Entity\FieldConfig; | |
/** | |
* Implements hook_update_N(). | |
* | |
* Enable translation for field_x field in vocabulary. | |
*/ | |
function module_update_8001() { | |
$config = \Drupal::configFactory()->getEditable('field.field.taxonomy_term.vocabulary.field_x'); | |
$config->set('translatable', TRUE); | |
$config->save(); | |
$field = FieldConfig::loadByName('taxonomy_term', 'vocabulary', 'field_x'); | |
$field->setTranslatable(TRUE); | |
$field->save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We need to ensure FieldStorageConfig is also translatable.
`
foreach ($fields as $field) {
$fieldDefinition = $entityDefinitionUpdateManager->getFieldStorageDefinition($field, 'node');
if ($fieldDefinition && !$fieldDefinition->isTranslatable()) {
$fieldDefinition->setTranslatable(TRUE);
$entityDefinitionUpdateManager->updateFieldStorageDefinition($fieldDefinition);
}
}
`