You are here

public function QuickAssetTrait::createAsset in farmOS 2.x

Create an asset.

Parameters

array $values: An array of values to initialize the asset with.

Return value

\Drupal\asset\Entity\AssetInterface The asset entity that was created.

1 call to QuickAssetTrait::createAsset()
Test::submitForm in modules/core/quick/tests/modules/farm_quick_test/src/Plugin/QuickForm/Test.php
Form submission handler.

File

modules/core/quick/src/Traits/QuickAssetTrait.php, line 36

Class

QuickAssetTrait
Provides methods for working with assets.

Namespace

Drupal\farm_quick\Traits

Code

public function createAsset(array $values = []) {

  // Start a new asset entity with the provided values.

  /** @var \Drupal\asset\Entity\AssetInterface $asset */
  $asset = Asset::create($values);

  // Track which quick form created the entity.
  $asset->quick[] = $this
    ->getId();

  // Save the asset.
  $asset
    ->save();

  // Display a message with a link to the asset.
  $message = $this
    ->t('Asset created: <a href=":url">@name</a>', [
    ':url' => $asset
      ->toUrl()
      ->toString(),
    '@name' => $asset
      ->label(),
  ]);
  $this->messenger
    ->addStatus($message);

  // Return the asset entity.
  return $asset;
}