65 lines
2.1 KiB
PHP
65 lines
2.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Contains farm_birth.module.
|
|
*/
|
|
|
|
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
|
use Drupal\Core\Entity\EntityInterface;
|
|
use Drupal\Core\Entity\EntityTypeInterface;
|
|
use Drupal\Core\Field\Entity\BaseFieldOverride;
|
|
use Drupal\Core\Url;
|
|
|
|
/**
|
|
* Implements hook_ENTITY_TYPE_view_alter().
|
|
*/
|
|
function farm_birth_asset_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
|
|
|
|
// Only alter animal assets.
|
|
if ($entity->bundle() != 'animal') {
|
|
return;
|
|
}
|
|
|
|
// Add a link to the asset's birth log.
|
|
if (!empty($build['birthdate'][0])) {
|
|
/** @var \Drupal\Core\Entity\EntityStorageInterface $log_storage */
|
|
$log_storage = \Drupal::service('entity_type.manager')->getStorage('log');
|
|
$log_ids = $log_storage->getQuery()
|
|
->accessCheck(TRUE)
|
|
->condition('type', 'birth')
|
|
->condition('asset', $entity->id())
|
|
->execute();
|
|
|
|
// Render a link to the log with a title of the timestamp field value.
|
|
if (!empty($log_ids)) {
|
|
$title = $build['birthdate'][0];
|
|
$build['birthdate'][0] = [
|
|
'#type' => 'link',
|
|
'#title' => $title,
|
|
'#url' => Url::fromRoute('entity.log.canonical', ['log' => reset($log_ids)]),
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_bundle_field_info().
|
|
*/
|
|
function farm_birth_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
|
|
$fields = [];
|
|
|
|
// Add the UniqueBirthLog validation constraint to the asset field of birth
|
|
// logs. We need to do this via hook_entity_bundle_field_info() instead of
|
|
// hook_entity_field_info_alter() because this module also provides a
|
|
// BaseFieldOverride for the asset field, and there is a Drupal core issue
|
|
// that prevents these from working together normally.
|
|
// @see https://www.drupal.org/project/drupal/issues/3193351
|
|
if ($entity_type->id() == 'log' && $bundle == 'birth') {
|
|
$fields['asset'] = BaseFieldOverride::loadByName($entity_type->id(), $bundle, 'asset') ?: clone $base_field_definitions['asset'];
|
|
$fields['asset']->addConstraint('UniqueBirthLog');
|
|
}
|
|
|
|
return $fields;
|
|
}
|