diff -u b/simple_oauth.install b/simple_oauth.install --- b/simple_oauth.install +++ b/simple_oauth.install @@ -5,10 +5,9 @@ * Installation and updating routines. */ +use Drupal\Core\Config\FileStorage; use Drupal\Core\Field\BaseFieldDefinition; -use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; -use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface; /** * Define the default batch size. @@ -54,59 +53,59 @@ function simple_oauth_update_8404() { $use_implicit = \Drupal::config('simple_oauth_extras.settings')->get('use_implicit'); - if ($use_implicit !== NULL) { - \Drupal::configFactory() - ->getEditable('simple_oauth.settings') - ->set('use_implicit', $use_implicit) - ->save(); + if ($use_implicit === NULL) { + $config_path = drupal_get_path('module', 'simple_oauth') . '/config/install'; + // Because of custom config_path, we don't call service here. + $source = new FileStorage($config_path); + $config_default = $source->read('simple_oauth.settings'); + $use_implicit = $config_default['use_implicit']; + } + else { \Drupal::configFactory() ->getEditable('simple_oauth_extras.settings') ->delete(); } + \Drupal::configFactory() + ->getEditable('simple_oauth.settings') + ->set('use_implicit', $use_implicit) + ->save(); $query = \Drupal::database()->delete('key_value') ->condition('collection', 'system.schema') ->condition('name', 'simple_oauth_extras') ->execute(); $field_names = ['redirect', 'user_id']; - $fields_installed = []; $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); -// $changes = $entity_definition_update_manager->getChangeList()['consumer']; - $field_definitions = [ - 'redirect' => BaseFieldDefinition::create('uri') - ->setLabel(new TranslatableMarkup('Redirect URI')) - ->setDescription(new TranslatableMarkup('The URI this client will redirect to when needed.')) - ->setTranslatable(TRUE) - // URIs are not length limited by RFC 2616, but we can only store 255 - // characters in our entity DB schema. - ->setSetting('max_length', 255) - ->setProvider('simple_oauth'), - - 'user_id' => BaseFieldDefinition::create('entity_reference') - ->setLabel(new TranslatableMarkup('User')) - ->setDescription(new TranslatableMarkup('When no specific user is authenticated Drupal will use this user as the author of all the actions made.')) - ->setRevisionable(TRUE) - ->setTranslatable(FALSE) - ->setCardinality(1) - ->setProvider('simple_oauth'), - ]; - // If simple_oauth_extras wasn't installed, definitions should be missing. + if ($query) { + // If simple_oauth_extras was installed, we need at least to update field + // provider. foreach ($field_names as $field_name) { $field_definition = $entity_definition_update_manager->getFieldStorageDefinition($field_name, 'consumer'); $field_definition->setProvider('simple_oauth'); $entity_definition_update_manager->updateFieldStorageDefinition($field_definition); } - } else { + // If simple_oauth_extras wasn't installed, definitions should be missing. + $field_definitions = [ + 'redirect' => BaseFieldDefinition::create('uri') + ->setLabel(new TranslatableMarkup('Redirect URI')) + ->setDescription(new TranslatableMarkup('The URI this client will redirect to when needed.')) + ->setTranslatable(TRUE) + // URIs are not length limited by RFC 2616, but we can only store 255 + // characters in our entity DB schema. + ->setSetting('max_length', 255) + ->setProvider('simple_oauth'), + + 'user_id' => BaseFieldDefinition::create('entity_reference') + ->setLabel(new TranslatableMarkup('User')) + ->setDescription(new TranslatableMarkup('When no specific user is authenticated Drupal will use this user as the author of all the actions made.')) + ->setRevisionable(TRUE) + ->setTranslatable(FALSE) + ->setCardinality(1) + ->setProvider('simple_oauth'), + ]; foreach ($field_names as $field_name) { - // if ($changes['field_storage_definitions'][$field_name] === EntityDefinitionUpdateManagerInterface::DEFINITION_CREATED) { - $entity_definition_update_manager->installFieldStorageDefinition($field_name, 'consumer', 'simple_oauth', $field_definitions[$field_name]); - $fields_installed []= $field_name; - // } + $entity_definition_update_manager->installFieldStorageDefinition($field_name, 'consumer', 'simple_oauth', $field_definitions[$field_name]); } - // If simple_oauth_extras was installed, we need at least to update field - // provider. - //$field_names = array_diff($field_names, $fields_installed); - } }