You are here

public function Project::getProjects in GatherContent 8.3

Same name and namespace in other branches
  1. 8 src/DAO/Project.php \Drupal\gathercontent\DAO\Project::getProjects()

Fetch projects from GatherContent.

Return value

array Associative array of projects.

File

src/DAO/Project.php, line 66

Class

Project
Class Project.

Namespace

Drupal\gathercontent\DAO

Code

public function getProjects() {
  if (!$this->local) {

    // @FIXME
    $account = \Drupal::config('gathercontent.settings')
      ->get('gathercontent_account');
    $account = unserialize($account);
    $projects = [];
    reset($account);
    $account_id = key($account);
    try {
      $response = $this->client
        ->get('/projects?account_id=' . $account_id);
      if ($response
        ->getStatusCode() === 200) {
        $data = json_decode($response
          ->getBody());
        foreach ($data->data as $project) {
          if ($project->active) {
            $projects[$project->id] = $project->name;
          }
        }
      }
    } catch (\Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
      \Drupal::logger('gathercontent')
        ->error($e
        ->getMessage(), []);
    }
    return $projects;
  }
  else {
    drupal_set_message(t('Error occured, please contact your system administrator'), 'error');
    \Drupal::logger('gathercontent')
      ->alert('Object Project created as local, but trying to reach remote data.', []);
  }
}