Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/atoms / atoms.deploy.php
Size: Mime:
<?php

use Drupal\atoms\Atom;
use Drupal\dds_global_content\GlobalContent;
use Drupal\user\Plugin\Condition\UserRole;

/**
 * Migrate from Global Content to Atoms if exists
 */
function atoms_deploy_migrate_gc() {
  /** @var \Drupal\Core\Extension\ModuleHandler $module_handler */
  $module_handler = \Drupal::service('module_handler');
  if ($module_handler->moduleExists('dds_global_content')) {
    atoms_rebuild();
    $config_factory = \Drupal::configFactory();
    $old_config = \Drupal::config('dds_global_content.settings');
    if (!$old_config->isNew()) {
      $new_config = $config_factory->getEditable('atoms.settings');
      foreach ($old_config->getRawData() as $key => $value) {
        $new_config->set($key, $value);
      }
      $new_config->save();
    }

    /** @var \Drupal\atoms\AtomsStorage $storage */
    $storage = \Drupal::service('atoms.storage');
    $machine_names = $storage->getMachineNames();
    foreach ($machine_names as $machine_name) {
      $gc = GlobalContent::load($machine_name);
      if (!empty($gc)) {
        $atom = Atom::load($machine_name);
        $atom->setData($gc->getData());
        $atom->save();
      }
    }

    $list = $config_factory->listAll('user.role.');
    foreach ($list as $config_id) {
      $config = $config_factory->getEditable($config_id);
      $permissions = $config->get('permissions');
      $updated = FALSE;
      foreach ($permissions as $key => $permission) {
        switch($permission) {
          case 'administer global contents':
            $permissions[$key] = 'administer atoms';
            $updated = TRUE;
            break;

          case 'configure global contents':
            $permissions[$key] = 'configure atoms';
            $updated = TRUE;
            break;

          case 'edit global content':
            $permissions[$key] = 'edit atom';
            $updated = TRUE;
            break;

          case 'translate global content':
            $permissions[$key] = 'translate atom';
            $updated = TRUE;
            break;
        }
      }
      if ($updated) {
        $config->set('permissions', $permissions);
        $config->save();
      }
    }

    /** @var \Drupal\Core\Extension\ModuleInstaller $module_installer */
    $module_installer = \Drupal::service('module_installer');
    $module_installer->uninstall(['dds_global_content']);
    return t('Migrated from Global Content');
  }
}