You are here

function cms_content_sync_requirements in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x cms_content_sync.install \cms_content_sync_requirements()
  2. 2.0.x cms_content_sync.install \cms_content_sync_requirements()

Implements hook_requirements.

Parameters

$phase:

Return value

array

File

./cms_content_sync.install, line 160
Install file for cms_content_sync.

Code

function cms_content_sync_requirements($phase) {
  $requirements = [];
  if ($phase == 'runtime') {

    // Show error when the Content Sync user aint got the role Content Sync role.
    $users = Drupal::entityTypeManager()
      ->getStorage('user')
      ->loadByProperties([
      'name' => 'CMS Content Sync',
    ]);
    if ($users) {
      $user = reset($users);
      if (!$user
        ->hasRole('cms_content_sync')) {
        $requirements['cms_content_sync_user_role_missing'] = [
          'title' => t('CMS Content Sync'),
          'value' => t('The service user "@username" is missing the required service role "Content Sync", this will cause several issues while trying to export/import entities.', [
            '@username' => $user
              ->getAccountName(),
          ]),
          'severity' => REQUIREMENT_ERROR,
        ];
      }
    }

    // Get Sync Core Version.
    $sync_cores = SyncCoreFactory::getAllSyncCores();
    if ($sync_cores) {
      foreach ($sync_cores as $id => $sync_core) {
        $status = [];
        try {
          $status = $sync_core
            ->getReportingService()
            ->getStatus();
        } catch (Exception $e) {

          // Can't connect.
          $requirements['cms_content_sync_sync_core_' . $id . '_status'] = [
            'title' => t('CMS Content Sync'),
            'value' => t('Can not connect:<br>Sync Core Host: @id', [
              '@id' => $id,
            ]),
            'severity' => REQUIREMENT_ERROR,
          ];
        }
        if ($status) {

          // Get module Version.
          $module_info = Drupal::service('extension.list.module')
            ->getExtensionInfo('cms_content_sync');
          if (isset($module_info['version'])) {
            $module_version = $module_info['version'];
            $module_version = preg_replace('@^\\d\\.x-(.*)$@', '$1', $module_version);
          }

          // Connected and core == module version.
          if (isset($module_version) && $module_version == $status['version']) {
            $requirements['cms_content_sync_sync_core_' . $id . '_status'] = [
              'title' => t('CMS Content Sync'),
              'value' => t('Connected:<br>Sync Core Host: @id<br>Sync Core Version: @sync_core_version<br>Content Sync Module Version: @module_version', [
                '@id' => $id,
                '@sync_core_version' => $status['version'],
                '@module_version' => $module_version,
              ]),
              'severity' => REQUIREMENT_INFO,
            ];
          }
          elseif (isset($module_version) && $module_version != $status['version']) {
            $requirements['cms_content_sync_sync_core_' . $id . '_status'] = [
              'title' => t('CMS Content Sync'),
              'value' => t('Connected:<br> Sync Core Host: @id<br>Sync Core Version: @sync_core_version <br> Content Sync Module Version: @module_version <br><br> The CMS Content Sync module version
                        does not match the Sync Core Version. It is <b>highly recommend</b> to update the CMS Content Sync module to match the version of the Sync Core.', [
                '@id' => $id,
                '@sync_core_version' => $status['version'],
                '@module_version' => $module_version,
              ]),
              'severity' => REQUIREMENT_WARNING,
            ];
          }
          else {
            $requirements['cms_content_sync_sync_core_' . $id . '_status'] = [
              'title' => t('CMS Content Sync'),
              'value' => t('Connected:<br> Sync Core Host: @id<br>Sync Core Version: @sync_core_version <br><br> <i>The CMS Content Sync module version could not be determined.
                        This is ususally caused by the fact that the dev version of the module is being used, or the Core "Update manager" module is not enabled.</i>', [
                '@id' => $id,
                '@sync_core_version' => $status['version'],
              ]),
              'severity' => REQUIREMENT_INFO,
            ];
          }
        }
      }
    }
  }
  return $requirements;
}