You are here

protected function InstagramAccountForm::prepareEntity in Instagram Feeds 8

Prepares the entity object before the form is built first.

Overrides ContentEntityForm::prepareEntity

File

src/Form/InstagramAccountForm.php, line 94

Class

InstagramAccountForm
Provides the InstagramAccount add/edit form.

Namespace

Drupal\instagram_feeds\Form

Code

protected function prepareEntity() {
  parent::prepareEntity();

  /** @var \Drupal\instagram_feeds\Entity\InstagramAccountInterface $i_account */
  $i_account = $this
    ->getEntity();

  // Obtain Instagram short-lived and exchange to long-lived token.
  $code = $this
    ->getRequest()->query
    ->get('code');
  if ($i_account
    ->isNew() && isset($code) && !empty($code)) {
    if ($i_account
      ->get('token')
      ->isEmpty() || $i_account
      ->get('token_expiration')
      ->isEmpty()) {
      $i_account
        ->getToken($this
        ->getConfig('client_id'), $this
        ->getConfig('client_secret'), $code);
    }
    if ($i_account
      ->get('account')
      ->isEmpty() && $i_account
      ->tokenIsValid()) {
      $i_account
        ->getAccountName();
    }
  }
}