View source
<?php
namespace Drupal\google_api_client\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\google_api_client\GoogleApiClientInterface;
use Drupal\user\UserInterface;
class GoogleApiClient extends ContentEntityBase implements GoogleApiClientInterface {
public function getId() {
return $this
->get('id')->value;
}
public function getName() {
return $this
->get('name')->value;
}
public function getDeveloperKey() {
return $this
->get('developer_key')->value;
}
public function getClientId() {
return $this
->get('client_id')->value;
}
public function getClientSecret() {
return $this
->get('client_secret')->value;
}
public function getScopes() {
$scopes = $this
->get('scopes')
->getValue();
$merged_scopes = [];
foreach ($scopes as $key => $scope) {
$merged_scopes[$key] = $scope['value'];
}
$services = $this
->getServices();
$all_scopes = google_api_client_google_services_scopes($services);
$return = [];
foreach ($merged_scopes as $scope) {
foreach ($all_scopes as $class => $scopes) {
if (isset($scopes[$scope])) {
$return[] = $scopes[$scope];
break;
}
}
}
return $return;
}
public function getServices() {
$services = $this
->get('services')
->getValue();
$return = [];
foreach ($services as $key => $service) {
$return[$key] = $service['value'];
}
return $return;
}
public function getAccessToken() {
return $this
->get('access_token')->value;
}
public function getAuthenticated() {
return $this
->get('is_authenticated')->value;
}
public function getOwner() {
return $this
->get('uid')->entity;
}
public function getOwnerId() {
return $this
->get('uid')->target_id;
}
public function getAccessType() {
return $this
->get('access_type')->value;
}
public function setName($name) {
return $this
->set('name', $name);
}
public function setDeveloperKey($key) {
return $this
->set('developer_key', $key);
}
public function setClientId($client_id) {
return $this
->set('client_id', $client_id);
}
public function setClientSecret($secret) {
return $this
->set('client_secret', $secret);
}
public function setScopes(array $scopes) {
$services = $this
->getServices();
$all_scopes = google_api_client_google_services_scopes($services);
$merged_scopes = [];
foreach ($scopes as $scope) {
foreach ($all_scopes as $class => $scopes) {
if (in_array($scope, $scopes)) {
$merged_scopes[] = array_search($scope, $scopes);
break;
}
}
}
return $this
->set('scopes', $merged_scopes);
}
public function setServices(array $services) {
return $this
->set('services', $services);
}
public function setAccessToken($token) {
return $this
->set('access_token', $token);
}
public function setAuthenticated($authentication) {
return $this
->set('is_authenticated', $authentication);
}
public function setAccessType($type) {
return $this
->set('access_type', $type);
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('GoogleApiClient ID'))
->setDescription(t('The ID of the Snapshot entity.'))
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -1,
])
->setDisplayConfigurable('view', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the GoogleApiClient entity.'))
->setSettings([
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 0,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 1,
])
->setRequired(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['developer_key'] = BaseFieldDefinition::create('string')
->setLabel(t('Developer Key'))
->setDescription(t('The developer key of the GoogleApiClient entity.'))
->setSettings([
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 1,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 2,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['client_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Client Id'))
->setDescription(t('The client id of the GoogleApiClient entity.'))
->setRequired(TRUE)
->setSettings([
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 2,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 3,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['client_secret'] = BaseFieldDefinition::create('string')
->setLabel(t('Client Secret'))
->setDescription(t('The client secret of the GoogleApiClient entity.'))
->setRequired(TRUE)
->setSettings([
'default_value' => '',
'max_length' => 255,
'text_processing' => 0,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => 3,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['services'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Services'))
->setDescription(t('services of the GoogleApiClient entity.'))
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 4,
'multiple' => TRUE,
])
->setSetting('allowed_values_function', 'google_api_client_google_services_names')
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['scopes'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Scopes'))
->setDescription(t('scopes of the GoogleApiClient entity.'))
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 5,
'multiple' => TRUE,
])
->setSetting('allowed_values_function', 'google_api_client_google_scopes_names')
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 6,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['access_token'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Access Token'))
->setDescription(t('Access token from google'))
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 7,
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', TRUE);
$fields['is_authenticated'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Is Authenticated'))
->setDescription(t('Is google_api_client account authenticated'))
->setDefaultValue(FALSE)
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 8,
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User Name'))
->setDescription(t('The Name of the associated user.'))
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 9,
])
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', TRUE);
$fields['access_type'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Is Access Type Offline'))
->setDescription(t('Access type of the GoogleApiClient entity.'))
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
'weight' => 6,
])
->setDefaultValue(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => 11,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
return $fields;
}
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
if (!isset($values['uid']) || $values['uid'] == NULL) {
$values += [
'uid' => \Drupal::currentUser()
->id(),
];
}
}
public function save() {
$original = $this->original ? $this->original : NULL;
if (!$original) {
$id = $this
->getOriginalId() !== NULL ? $this
->getOriginalId() : $this
->id();
$original = $this
->entityTypeManager()
->getStorage($this
->getEntityTypeId())
->loadUnchanged($id);
}
if ($original && ($original
->getServices() != $this
->getServices() || $original
->getClientId() != $this
->getClientId() || $original
->getClientSecret() != $this
->getClientSecret() || $original
->getScopes() != $this
->getScopes() || $original
->getDeveloperKey() != $this
->getDeveloperKey())) {
$this
->setAccessToken('{}');
$this
->setAuthenticated(FALSE);
}
return parent::save();
}
}