You are here

public function ApiDoc::preSave in Apigee API Catalog 8.2

Same name and namespace in other branches
  1. 8 src/Entity/ApiDoc.php \Drupal\apigee_api_catalog\Entity\ApiDoc::preSave()

Acts on an entity before the presave hook is invoked.

Used before the entity is saved and before invoking the presave hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed. This is different from its counterpart in the Field API, FieldItemListInterface::preSave(), which is fired on all field translations automatically. @todo Adjust existing implementations and the documentation according to https://www.drupal.org/node/2577609 to have a consistent API.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

Throws

\Exception When there is a problem that should prevent saving the entity.

Overrides ContentEntityBase::preSave

See also

\Drupal\Core\Field\FieldItemListInterface::preSave()

File

src/Entity/ApiDoc.php, line 140

Class

ApiDoc
Defines the API Doc entity.

Namespace

Drupal\apigee_api_catalog\Entity

Code

public function preSave(EntityStorageInterface $storage) {
  parent::preSave($storage);

  // If no revision author has been set explicitly, make the current user
  // the revision author.
  if (!$this
    ->getRevisionUser()) {
    $this
      ->setRevisionUserId(\Drupal::currentUser()
      ->id());
  }
  \Drupal::service('apigee_api_catalog.spec_fetcher')
    ->fetchSpec($this, FALSE, FALSE);

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

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