You are here

function apigee_api_catalog_node_presave in Apigee API Catalog 8.2

Implements hook_ENTITY_TYPE_presave().

File

./apigee_api_catalog.module, line 72
Copyright 2019 Google Inc.

Code

function apigee_api_catalog_node_presave(EntityInterface $entity) {

  /* @var \Drupal\node\NodeInterface $entity */
  if ($entity
    ->bundle() != 'apidoc') {
    return;
  }

  // Fetch spec file if using URL.
  if ($entity
    ->get('field_apidoc_spec_file_source')->value === SpecFetcherInterface::SPEC_AS_URL) {
    \Drupal::service('apigee_api_catalog.spec_fetcher')
      ->fetchSpec($entity);
  }

  // API docs that use the "file" source will still need their md5 updated.
  if ($entity
    ->get('field_apidoc_spec_file_source')->value === SpecFetcherInterface::SPEC_AS_FILE) {
    $spec_value = $entity
      ->get('field_apidoc_spec')
      ->isEmpty() ? [] : $entity
      ->get('field_apidoc_spec')
      ->getValue()[0];
    if (!empty($spec_value['target_id'])) {

      /* @var \Drupal\file\Entity\File $file */
      $file = \Drupal::entityTypeManager()
        ->getStorage('file')
        ->load($spec_value['target_id']);
      if ($file) {
        $entity
          ->set('field_apidoc_spec_md5', md5_file($file
          ->getFileUri()));
      }
    }
  }
}