You are here

function opigno_learning_path_update_8026 in Opigno Learning path 8

Add training certificate expire fields.

File

./opigno_learning_path.install, line 753
Install, update and uninstall functions for the module.

Code

function opigno_learning_path_update_8026() {
  $config_path = drupal_get_path('module', 'opigno_learning_path') . '/config/install';
  $storage = new FileStorage($config_path);
  $data = $storage
    ->read('field.storage.group.field_certificate_expire');
  if (!FieldStorageConfig::loadByName($data['entity_type'], $data['field_name'])) {
    FieldStorageConfig::create($data)
      ->save();
  }
  $data = $storage
    ->read('field.field.group.learning_path.field_certificate_expire');
  if (!FieldConfig::loadByName($data['entity_type'], $data['bundle'], $data['field_name'])) {
    FieldConfig::create($data)
      ->save();
  }
  $module_handler = \Drupal::moduleHandler();
  $config_storage = new FileStorage($module_handler
    ->getModule('opigno_learning_path')
    ->getPath() . '/config/install');
  foreach ([
    'field.storage.group.field_certificate_expire_results',
    'field.field.group.learning_path.field_certificate_expire_results',
  ] as $config_name) {
    $config_record = $config_storage
      ->read($config_name);
    $entity_type = \Drupal::service('config.manager')
      ->getEntityTypeIdByName($config_name);

    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()
      ->getStorage($entity_type);
    $entity = $storage
      ->createFromStorageRecord($config_record);
    $entity
      ->save();
  }
  $config_path = drupal_get_path('module', 'opigno_learning_path') . '/config/install';
  $storage = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $configs = [
    'core.entity_form_display.group.learning_path.default',
    'core.entity_view_display.group.learning_path.default',
  ];
  foreach ($configs as $item) {
    $data = $storage
      ->read($item);
    $config_storage
      ->write($item, $data);
  }
}