You are here

trait QuickAssetTrait in farmOS 2.x

Provides methods for working with assets.

Hierarchy

1 file declares its use of QuickAssetTrait
Test.php in modules/core/quick/tests/modules/farm_quick_test/src/Plugin/QuickForm/Test.php

File

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

Namespace

Drupal\farm_quick\Traits
View source
trait QuickAssetTrait {
  use MessengerTrait;
  use StringTranslationTrait;

  /**
   * Get the plugin ID.
   *
   * This must be implemented by the quick form class that uses this trait.
   *
   * @return string
   *   The quick form ID.
   */
  protected abstract function getId();

  /**
   * Create an asset.
   *
   * @param array $values
   *   An array of values to initialize the asset with.
   *
   * @return \Drupal\asset\Entity\AssetInterface
   *   The asset entity that was created.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
QuickAssetTrait::createAsset public function Create an asset.
QuickAssetTrait::getId abstract protected function Get the plugin ID.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.