You are here

function oauth2_server_flush_caches in OAuth2 Server 7

Implements hook_flush_caches().

File

./oauth2_server.module, line 334
Provides OAuth2 server functionality.

Code

function oauth2_server_flush_caches() {
  $field = field_info_field('scopes');

  // Create the scopes reference field if it's missing.
  if (!$field) {
    $field = array(
      'field_name' => 'scopes',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'translatable' => FALSE,
      'settings' => array(
        'target_type' => 'oauth2_server_scope',
      ),
      'module' => 'entityreference',
      'type' => 'entityreference',
    );
    field_create_field($field);
  }

  // Go over all bundles that should have an instance of the scopes field,
  // and create the instance where missing.
  $needed_instances = array(
    'oauth2_server_token' => array_keys(oauth2_server_token_bundles()),
    'oauth2_server_authorization_code' => array(
      'oauth2_server_authorization_code',
    ),
  );
  foreach ($needed_instances as $entity_type => $bundles) {
    $existing = array();
    if (!empty($field['bundles'][$entity_type])) {
      $existing = $field['bundles'][$entity_type];
    }
    $diff = array_diff($bundles, $existing);
    foreach ($diff as $new_bundle) {
      $instance = array(
        'label' => 'Scopes',
        'field_name' => 'scopes',
        'entity_type' => $entity_type,
        'bundle' => $new_bundle,
      );
      field_create_instance($instance);
    }
  }
}