You are here

public function ContributeManager::getAccount in Contribute 8

Get account status.

Return value

array An associative array containing account status.

Overrides ContributeManagerInterface::getAccount

File

src/ContributeManager.php, line 109

Class

ContributeManager
Class ContributeManager.

Namespace

Drupal\contribute

Code

public function getAccount() {
  $account_type = $this
    ->getAccountType();
  $account_id = $this
    ->getAccountId() ?: 'anonymous';
  $cid = 'contribute.account.' . md5("{$account_type}.{$account_id}");
  $cache = $this->cache
    ->get($cid);
  if ($cache) {
    return $cache->data;
  }
  $account = [];
  $account['status'] = FALSE;
  if ($account_id != 'anonymous') {
    switch ($account_type) {
      case 'organization':
        $organization = $this
          ->getOrganization();
        if ($organization) {
          $account['status'] = TRUE;
          $account['url'] = Url::fromUri($organization['url']);
          $account['name'] = $organization['title'];
          $account['created'] = $organization['created'];
          $account['image'] = $this
            ->getOrganizationLogo();
        }
        break;
      case 'user':
        $user = $this
          ->getUser();
        if ($user) {
          $account['status'] = TRUE;
          $account['url'] = Url::fromUri($user['url']);
          if ($user['field_first_name'] || $user['field_last_name']) {
            $account['name'] = $user['field_first_name'] . ($user['field_first_name'] && $user['field_last_name'] ? ' ' : '') . $user['field_last_name'];
          }
          else {
            $account['name'] = $user['name'];
          }
          $account['created'] = $user['created'];
          $account['image'] = $this
            ->getUserPicture();
          $account['organizations'] = [];
          foreach ($user['field_organizations'] as $item) {
            $data = $this
              ->get($item['uri'] . '.json');
            if (!empty($data['field_organization_name'])) {
              $account['organizations'][] = $data['field_organization_name'];
            }
          }
        }
        break;
    }
  }
  $configure_attributes = [
    'data-dialog-type' => 'modal',
    'data-dialog-options' => Json::encode([
      'width' => 600,
    ]),
  ];
  if (isset($account['name'])) {
    $account['value'] = [];
    $account['value']['name'] = [
      '#type' => 'link',
      '#title' => $account['name'],
      '#url' => $account['url'],
      '#prefix' => '<strong>',
      '#suffix' => '</strong>',
    ];
    if (!empty($account['organizations'])) {
      $account['value']['organizations'] = [
        '#prefix' => ' @ ',
        '#plain_text' => implode('; ', $account['organizations']),
      ];
    }
    $t_args = [
      '@date' => $this->dateFormatter
        ->formatTimeDiffSince($account['created']),
    ];
    $account['value']['drupal'] = [
      '#prefix' => '<br/>',
      '#markup' => $this
        ->t('On Drupal.org for @date', $t_args),
    ];
    $account['description']['link'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Configure'),
      '#url' => Url::fromRoute('contribute.settings'),
      '#attributes' => $configure_attributes + [
        'class' => [
          'use-ajax',
        ],
      ],
    ];
  }
  else {
    $t_args = [
      ':href_register' => 'https://register.drupal.org/user/register',
      ':href_ groups' => 'https://groups.drupal.org',
      ':href_jobs' => 'https://jobs.drupal.org',
      ':href_association' => 'https://www.drupal.org/association',
    ];
    $account['value'] = [
      '#markup' => $this
        ->t('When you <a href=":href_register">create a Drupal.org account</a>, you gain access to a whole ecosystem of Drupal.org sites and services.', $t_args) . ' ' . $this
        ->t('Your account works on Drupal.org and any of its subsites including <a href=":href_ groups">Drupal Groups</a>, <a href=":href_jobs">Drupal Jobs</a>, <a href=":href_association">Drupal Association</a> and more.', $t_args),
    ];
    $account['description'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Configure'),
      '#url' => Url::fromRoute('contribute.settings'),
      '#attributes' => $configure_attributes + [
        'class' => [
          'use-ajax',
          'button',
          'button--small',
          'button--primary',
          'contribute-status-report-community-info__button',
        ],
      ],
    ];
  }

  // Cache account information.
  $this->cache
    ->set($cid, $account, strtotime('+1 hour'), [
    'contribute',
  ]);
  return $account;
}