Commit 1a739e9a authored by narendraR's avatar narendraR Committed by Tim Plunkett
Browse files

Issue #3275413 by narendraR, srishti.bankar, chrisfromredfin: Create help page in .module file

parent 95865c19
Loading
Loading
Loading
Loading
+12 −92
Original line number Diff line number Diff line
@@ -2,100 +2,20 @@

/**
 * @file
 * Provides hook implementations for the Project Browser module.
 */

use Composer\Semver\Semver;
use Drupal\Core\Url;
use Drupal\project_browser\DrupalOrg\DrupalOrgClient;
use Drupal\project_browser\DrupalOrg\DrupalOrgProjects;
use Drupal\Core\Routing\RouteMatchInterface;

function _project_browser_batch_finished($success, $results, $operations) {
  if ($success) {
    \Drupal::messenger()->addMessage('All Done');

  }
  else {
    \Drupal::messenger()->addMessage('Error');

  }
}

function _add_projects_to_tempstore() {
  $operations = [];
  foreach (range(0, 40) as $page) {
    $operations[] = [
      '_project_browser_get_all_projects',
      [$page],
    ];
  }

  $batch = [
    'title' => 'Importing project data',
    'operations' => $operations,
    'finished' => '_project_browser_batch_finished',
  ];
  batch_set($batch);
  return batch_process('admin/modules/browse', Url::fromRoute('project_browser.batch_controller'));
}

function _project_browser_get_all_projects($page, &$context) {
  $drupal_org_client = \Drupal::classResolver(DrupalOrgClient::class);
  $all_projects = \Drupal::service('tempstore.shared')->get('project_browser')->get('all_projects') ?? [];

  $query = [
    'page' => $page,
    'limit' => 50,
    'field_project_type' => 'full',
    'field_project_has_releases' => 1,
    'taxonomy_vocabulary_44' => 13028,
    'field_security_advisory_coverage' => 'covered',
    'field_project_has_issue_queue' => 1,
    'type' => 'project_module',
    'status' => 1,
  ];
  $projects = new \ArrayObject();
  $all_done = NULL;
  while (count($projects) < 70 || isset($all_done)) {
    $drupal_org_response = $drupal_org_client->getProjects($query);
    $returned_projects = new DrupalOrgProjects($drupal_org_response['list']);

    // If there are no returned projects, we've reached the end of the
    // list and should exit the while loop.
    if (count($returned_projects) === 0) {
      $all_done = TRUE;
      break;
    }

    foreach ($returned_projects as $project) {
      // This is only reached if there is not cached release data.
      // It will be necessary to query Drupal.org to get the project
      // release data, then check each releases compatibility against
      // the current Drupal version.
      // @todo flush this cache in a post_update hook.
      $releases = $drupal_org_client->getProjectReleases($project->field_project_machine_name);
      if (!empty($releases['releases'])) {
        $compatible_releases = array_filter($releases['releases'], function ($release) {
          return !empty($release['core_compatibility']) && Semver::satisfies(\Drupal::VERSION, $release['core_compatibility']);
        });
        if (!empty($compatible_releases)) {
          $projects->append($project);
        }
      }
    }
    $query['page'] = isset($query['page']) ? $query['page'] + 1 : 1;
  }
  $projects_to_store = (array) $projects;
  $projects_to_store = array_map(function ($a_project) {
    $the_project = (array) $a_project;
    unset($the_project['body']['value']);
    unset($the_project['flag_project_star_user']);
    unset($the_project['field_supporting_organizations']);
    foreach ($the_project as $key => $value) {
      if (strpos($key, "\0*\0") !== FALSE) {
        unset($the_project[$key]);
      }
/**
 * Implements hook_help().
 */
function project_browser_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.project_browser':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t("The Project Browser makes it easy for site builders to find modules and get instructions to add the project to your site. The browser lives inside the Drupal site itself (See \"Browse\" under the \"Extend\" section), which means you don't need to leave your site in order to look for modules.") . '</p>';
      return $output;
  }
    return $the_project;
  }, $projects_to_store);
  \Drupal::service('tempstore.shared')->get('project_browser')->set('all_projects', array_merge($all_projects, $projects_to_store));
}