popsu-d9/web/modules/custom/popsu_uniqdate/popsu_uniqdate.module

73 lines
2.5 KiB
PHP

<?php
use Drupal\node\Entity\Node;
// use Drupal\Core\Entity\EntityTypeInterface;
// use Drupal\Core\Field\BaseFieldDefinition;
// use Drupal\Core\Field\FieldStorageDefinitionInterface;
// https://www.drupal.org/project/drupal/issues/2916266#comment-12301574
function popsu_uniqdate_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityTypeId() === 'node' && $entity->bundle() === 'evenement') {
if ($entity->hasField('field_date') && !$entity->get('field_date')->isEmpty()) {
// $first_term = $entity->get('field_tags')->first()->target_id;
$dates = $entity->get('field_date')->getValue();
asort($dates);
$last_date = array_pop($dates);
$entity->set('field_last_date_only', $last_date);
}
else {
$entity->set('field_last_date_only', NULL);
}
}
}
// /**
// * Implement hook_entity_bundle_field_info().
// *
// * @param EntityTypeInterface $entity_type
// * @param $bundle
// * @param array $base_field_definitions
// * @return array
// */
// function popsu_uniqdate_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
// if ($entity_type->id() == 'node' && $bundle == 'evenement') {
// $fields['computed_date_unique'] = BaseFieldDefinition::create('datetime')
// ->setName('computed_date_unique')
// ->setLabel(t('Computed Date Unique'))
// ->setDescription(t('Computed Date Unique.'))
// // // The Entity Type this field belongs to.
// ->setTargetEntityTypeId($entity_type->id())
// // // The Entity Type bundle this field belongs to.
// ->setTargetBundle($bundle)
// // ->setSetting('target_type', 'node')
// ->setCardinality(1)
// ->setComputed(TRUE)
// ->setRevisionable(FALSE)
// ->setTranslatable(FALSE)
// ->setDisplayConfigurable('view', TRUE)
// ->setDisplayOptions('view', [
// 'label' => 'hidden',
// 'weight' => -5,
// ])
// ->setClass(\Drupal\popsu_uniqdate\Plugin\Field\FieldType\ComputedUniqDate::class);
// return $fields;
// }
// }
// /**
// * Implements hook_views_data_alter().
// */
// function popsu_uniqdate_views_data_alter(array &$data) {
// if (isset($data['node'])) {
// // Add the Computed Date Unique field to Views.
// $data['node']['computed_date_unique'] = [
// 'title' => t('Computed Date Unique'),
// 'field' => [
// 'id' => 'views_computed_date_unique',
// ],
// ];
// }
// }