clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
<?php
namespace Drupal\farm_birth\EventSubscriber;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\log\Event\LogEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Sync child asset fields to reflect those saved in a birth log.
*/
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, and the child does not have any parents,
// add the mother to the child's parent reference field.
if (!empty($mother)) {
$parents = $child->get('parent')->referencedEntities();
if (empty($parents)) {
$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();
}
}
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Drupal\farm_birth\Plugin\Log\LogType;
use Drupal\farm_entity\Plugin\Log\LogType\FarmLogType;
/**
* Provides the birth log type.
*
* @LogType(
* id = "birth",
* label = @Translation("Birth"),
* )
*/
class Birth extends FarmLogType {
/**
* {@inheritdoc}
*/
public function buildFieldDefinitions() {
$fields = parent::buildFieldDefinitions();
// Mother.
$options = [
'type' => 'entity_reference',
'label' => $this->t('Mother'),
'target_type' => 'asset',
'target_bundle' => 'animal',
'weight' => [
'form' => 45,
'view' => -15,
],
];
$fields['mother'] = $this->farmFieldFactory->bundleFieldDefinition($options);
// Veterinarian.
$options = [
'type' => 'string',
'label' => $this->t('Veterinarian'),
'description' => $this->t('If a veterinarian was involved, enter their name here.'),
'weight' => [
'form' => -40,
'view' => -40,
],
];
$fields['vet'] = $this->farmFieldFactory->bundleFieldDefinition($options);
return $fields;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Drupal\farm_birth\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Checks that only one birth log references an asset.
*
* @Constraint(
* id = "UniqueBirthLog",
* label = @Translation("Unique birth log", context = "Validation"),
* )
*/
class UniqueBirthLogConstraint extends Constraint {
/**
* The default violation message.
*
* @var string
*/
public $message = '%child already has a birth log. More than one birth log cannot reference the same child.';
}

View File

@@ -0,0 +1,86 @@
<?php
namespace Drupal\farm_birth\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the UniqueBirthLog constraint.
*/
class UniqueBirthLogConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a UniqueBirthLogConstraintValidator object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
/** @var \Drupal\Core\Field\EntityReferenceFieldItemList $value */
/** @var \Drupal\farm_birth\Plugin\Validation\Constraint\UniqueBirthLogConstraint $constraint */
// Only continue if this is a birth log.
/** @var \Drupal\log\Entity\LogInterface $log */
$log = $value->getParent()->getValue();
if (!is_null($log) && $log->bundle() != 'birth') {
return;
}
// Iterate through referenced entities.
foreach ($value->referencedEntities() as $delta => $asset) {
// If the log is not new, skip validation.
// A birth log exits so there is no need to check if one can be created.
/** @var \Drupal\log\Entity\LogInterface $log */
$log = $value->getParent()->getValue();
if (!$log->isNew()) {
return;
}
// Query the number of birth logs that reference the asset.
// We do not check access to ensure that all matching logs are found.
$count = $this->entityTypeManager->getStorage('log')->getAggregateQuery()
->accessCheck(FALSE)
->condition('type', 'birth')
->condition('asset', $asset->id())
->count()
->execute();
// If more than 0 birth logs reference the asset, add a violation.
if ($count > 0) {
$this->context->buildViolation($constraint->message, ['%child' => $asset->label()])
->atPath((string) $delta . '.target_id')
->setInvalidValue($asset->id())
->addViolation();
}
}
}
}