You are here

public function QuickLogTrait::createLog in farmOS 2.x

Create a log.

Parameters

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

Return value

\Drupal\log\Entity\LogInterface The log entity that was created.

1 call to QuickLogTrait::createLog()
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/QuickLogTrait.php, line 37

Class

QuickLogTrait
Provides methods for working with logs.

Namespace

Drupal\farm_quick\Traits

Code

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;
}