Skip to content

Instantly share code, notes, and snippets.

@arpitr
Created January 4, 2018 08:11
Show Gist options
  • Save arpitr/6c68cf3cdbb24f546e25546c41a96a1b to your computer and use it in GitHub Desktop.
Save arpitr/6c68cf3cdbb24f546e25546c41a96a1b to your computer and use it in GitHub Desktop.
Drupal 8 Firld config import via install hooks
use Drupal\Core\Serialization\Yaml;
use \Drupal\field\Entity\FieldConfig;
use \Drupal\field\Entity\FieldStorageConfig;
function moudlename_config_updates_update_8103() {
//Code for importing field storage.
$storage = 'field.storage.paragraph.field_related_media';
$config_path = file_get_contents(DRUPAL_ROOT . '/profiles/custom/profile_name/modules/custom/
_base_fields/config/install/' . $storage . '.yml');
$data = Yaml::decode($config_path);
$fieldStorage = FieldStorageConfig::create($data);
$fieldStorage->save();
$files = [
'paragraphs.paragraphs_type.related_media',
'core.entity_view_display.paragraph.related_media.default',
'core.entity_form_display.paragraph.related_media.default'
];
foreach($files as $file_name) {
$config_path = file_get_contents(DRUPAL_ROOT . '/profiles/custom/profile_name/modules/custom/content_type/article_content_type/config/install/' . $file_name . '.yml');
$data = Yaml::decode($config_path);
\Drupal::configFactory()->getEditable($file_name)->setData($data)->save(TRUE);
}
$file_name = 'field.field.paragraph.related_media.field_related_media';
$config_path = file_get_contents(DRUPAL_ROOT . '/profiles/custom/profile_name/modules/custom/content_type/article_content_type/config/install/' . $file_name . '.yml');
$data = Yaml::decode($config_path);
$field = FieldConfig::create($data);
$field->save();
$storage_file = 'field.field.node.article.field_paragraphs';
$storage = FieldConfig::load('field.storage.node.field_paragraphs');
$config_path = file_get_contents(DRUPAL_ROOT . '/profiles/custom/profile_name/modules/custom/content_type/article_content_type/config/install/' . $storage_file . '.yml');
$data = Yaml::decode($config_path);
if(!empty($storage)) {
$uuid = $storage->get('uuid');
$field = FieldConfig::create($data);
$field->enforceIsNew(FALSE);
$field->set('uuid',$uuid);
$field->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment