You are here

trait QuickLogTrait in farmOS 2.x

Provides methods for working with logs.

Hierarchy

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

File

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

Namespace

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

  /**
   * 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 a log.
   *
   * @param array $values
   *   An array of values to initialize the log with.
   *
   * @return \Drupal\log\Entity\LogInterface
   *   The log entity that was created.
   */
  public function createLog(array $values = []) {

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

    /** @var \Drupal\log\Entity\LogInterface $log */
    $log = Log::create($values);

    // If quantity measurements are provided, create quantity entities and
    // reference them from the log.
    if (!empty($values['quantity'])) {
      foreach ($values['quantity'] as $qty) {
        $log->quantity[] = $this
          ->createQuantity($qty);
      }
    }

    // If not specified, set the log's status to "done".
    if (!isset($values['status'])) {
      $log->status = 'done';
    }

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

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

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

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

}

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.
QuickLogTrait::createLog public function Create a log.
QuickLogTrait::getId abstract protected function Get the plugin ID.
QuickQuantityTrait::createQuantity public function Create a quantity.
QuickTermTrait::createOrLoadTerm public function Given a term name, create or load a matching term entity.
QuickTermTrait::createTerm public function Create a term.
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.