class LogEventSubscriber in farmOS 2.x
Same name in this branch
- 2.x modules/log/birth/src/EventSubscriber/LogEventSubscriber.php \Drupal\farm_birth\EventSubscriber\LogEventSubscriber
- 2.x modules/core/owner/src/EventSubscriber/LogEventSubscriber.php \Drupal\farm_owner\EventSubscriber\LogEventSubscriber
Sync child asset fields to reflect those saved in a birth log.
Hierarchy
- class \Drupal\farm_birth\EventSubscriber\LogEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
Expanded class hierarchy of LogEventSubscriber
1 string reference to 'LogEventSubscriber'
- farm_birth.services.yml in modules/
log/ birth/ farm_birth.services.yml - modules/log/birth/farm_birth.services.yml
1 service uses LogEventSubscriber
File
- modules/
log/ birth/ src/ EventSubscriber/ LogEventSubscriber.php, line 13
Namespace
Drupal\farm_birth\EventSubscriberView source
class LogEventSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* MyModuleService constructor.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*
* @return array
* The event names to listen for, and the methods that should be executed.
*/
public static function getSubscribedEvents() {
return [
LogEvent::INSERT => 'syncBirthChildren',
LogEvent::UPDATE => 'syncBirthChildren',
];
}
/**
* Sync child asset fields to reflect those saved in a birth log.
*
* @param \Drupal\log\Event\LogEvent $event
* The log event.
*/
public function syncBirthChildren(LogEvent $event) : void {
// Get the log entity from the event.
$log = $event->log;
// If this is not a birth log, bail.
if ($log
->bundle() != 'birth') {
return;
}
// Load mother asset.
/** @var \Drupal\asset\Entity\AssetInterface $mother */
$mothers = $log
->get('mother')
->referencedEntities();
$mother = reset($mothers);
// Load children assets.
/** @var \Drupal\asset\Entity\AssetInterface[] $children */
$children = $log
->get('asset')
->referencedEntities();
// If the log doesn't reference any children, bail.
if (empty($children)) {
return;
}
// Iterate through the children.
foreach ($children as $child) {
$save = FALSE;
$revision_log = [];
// If the child is an animal, and their date of birth does not match the
// timestamp of the birth log, sync it.
if ($child
->bundle() == 'animal' && $child
->get('birthdate')->value != $log
->get('timestamp')->value) {
$args = [
':child_url' => $child
->toUrl()
->toString(),
'%child_name' => $child
->label(),
];
$message = $this
->t('<a href=":child_url">%child_name</a> date of birth was updated to match their birth log.', $args);
$this->messenger
->addMessage($message);
$revision_log[] = $message;
$child->birthdate = $log
->get('timestamp')->value;
$save = TRUE;
}
// If a mother is specified, make sure that it is linked as one of the
// child's parents.
if (!empty($mother)) {
// Iterate through the child's parents to see if the mother is linked.
$mother_linked = FALSE;
$parents = $child
->get('parent')
->referencedEntities();
foreach ($parents as $parent) {
if ($parent
->id() == $mother
->id()) {
$mother_linked = TRUE;
break;
}
}
// Link to the mother, if not already.
if (!$mother_linked) {
$args = [
':mother_url' => $mother
->toUrl()
->toString(),
'%mother_name' => $mother
->label(),
':child_url' => $child
->toUrl()
->toString(),
'%child_name' => $child
->label(),
];
$message = $this
->t('<a href=":mother_url">%mother_name</a> added as a parent of <a href=":child_url">%child_name</a>.', $args);
$this->messenger
->addMessage($message);
$revision_log[] = $message;
$child->parent[] = [
'target_id' => $mother
->id(),
];
$save = TRUE;
}
}
// Save the child, if necessary.
if ($save) {
$revision_log[] = $this
->t('Birth log saved: <a href=":birth_url">%birth_label</a>', [
':birth_url' => $log
->toUrl()
->toString(),
'%birth_label' => $log
->label(),
]);
$child
->setRevisionLogMessage(implode(" ", $revision_log));
$child
->save();
}
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LogEventSubscriber:: |
protected | property | The Messenger service. | |
LogEventSubscriber:: |
public static | function | ||
LogEventSubscriber:: |
public | function | Sync child asset fields to reflect those saved in a birth log. | |
LogEventSubscriber:: |
public | function | MyModuleService constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |