2021-07-16 05:26:23 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Drupal\farm_quick\Traits;
|
|
|
|
|
2021-07-20 14:32:09 +02:00
|
|
|
use Drupal\Core\Messenger\MessengerTrait;
|
2021-07-16 05:26:23 +02:00
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
|
|
|
use Drupal\log\Entity\Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides methods for working with logs.
|
|
|
|
*/
|
|
|
|
trait QuickLogTrait {
|
|
|
|
|
2021-07-20 14:32:09 +02:00
|
|
|
use MessengerTrait;
|
2021-07-16 05:26:23 +02:00
|
|
|
use StringTranslationTrait;
|
2021-07-20 14:45:34 +02:00
|
|
|
use QuickQuantityTrait;
|
2022-01-15 20:37:50 +01:00
|
|
|
use QuickStringTrait;
|
2021-07-16 05:26:23 +02:00
|
|
|
|
2021-07-19 22:18:18 +02:00
|
|
|
/**
|
2023-03-13 13:06:47 +01:00
|
|
|
* Returns the quick form ID.
|
2021-07-19 22:18:18 +02:00
|
|
|
*
|
|
|
|
* This must be implemented by the quick form class that uses this trait.
|
|
|
|
*
|
2023-03-13 13:06:47 +01:00
|
|
|
* @see \Drupal\farm_quick\Plugin\QuickForm\QuickFormInterface
|
|
|
|
*
|
2021-07-19 22:18:18 +02:00
|
|
|
* @return string
|
|
|
|
* The quick form ID.
|
|
|
|
*/
|
2023-03-13 13:06:47 +01:00
|
|
|
abstract public function getId();
|
2021-07-19 22:18:18 +02:00
|
|
|
|
2021-07-16 05:26:23 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-01-15 20:30:20 +01:00
|
|
|
protected function createLog(array $values = []) {
|
2021-07-16 05:26:23 +02:00
|
|
|
|
2022-01-15 20:37:50 +01:00
|
|
|
// Trim the log name to 255 characters.
|
|
|
|
if (!empty($values['name'])) {
|
|
|
|
$values['name'] = $this->trimString($values['name'], 255);
|
|
|
|
}
|
|
|
|
|
2021-07-16 05:26:23 +02:00
|
|
|
// Start a new log entity with the provided values.
|
|
|
|
/** @var \Drupal\log\Entity\LogInterface $log */
|
|
|
|
$log = Log::create($values);
|
|
|
|
|
2022-01-18 17:23:09 +01:00
|
|
|
// If quantity measurements are provided, reference them from the log.
|
2021-07-20 14:45:34 +02:00
|
|
|
if (!empty($values['quantity'])) {
|
|
|
|
foreach ($values['quantity'] as $qty) {
|
2022-01-18 17:23:09 +01:00
|
|
|
|
|
|
|
// If the quantity is an array of values, pass it to createQuantity.
|
|
|
|
if (is_array($qty)) {
|
2022-11-22 12:46:05 +01:00
|
|
|
$log->quantity[] = $this->createQuantity($qty, $log->bundle());
|
2022-01-18 17:23:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, add it directly to the log.
|
|
|
|
else {
|
|
|
|
$log->quantity[] = $qty;
|
|
|
|
}
|
2021-07-20 14:45:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 14:45:09 +02:00
|
|
|
// If not specified, set the log's status to "done".
|
|
|
|
if (!isset($values['status'])) {
|
|
|
|
$log->status = 'done';
|
|
|
|
}
|
|
|
|
|
2021-07-19 22:18:18 +02:00
|
|
|
// Track which quick form created the entity.
|
|
|
|
$log->quick[] = $this->getId();
|
|
|
|
|
2021-07-16 05:26:23 +02:00
|
|
|
// Save the log.
|
|
|
|
$log->save();
|
|
|
|
|
2021-07-20 14:32:09 +02:00
|
|
|
// 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);
|
|
|
|
|
2021-07-16 05:26:23 +02:00
|
|
|
// Return the log entity.
|
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|