Added callback to default to current date

But it isn't displaying, possibly because of having the wrong kind of widget
This commit is contained in:
Naomi 2020-02-23 17:36:48 +00:00
parent a7a96ab03c
commit c63cae6b71
2 changed files with 22 additions and 2 deletions

View File

@ -1,5 +1,15 @@
<?php <?php
function opencase_entities_update_8013() {
$update_manager = \Drupal::entityDefinitionUpdateManager();
$definition = $update_manager->getFieldStorageDefinition('activity_date_time', 'oc_activity');
// $definition->setDescription('');
// $definition->setRequired('');
//->setDefaultValueCallback('Drupal\opencase_entities\Entity\OCActivity::currentDateTime')
$update_manager->updateFieldStorageDefinition($definition);
}
/** /**
* Add 'activity_date_time' field to 'oc_activity' entities. * Add 'activity_date_time' field to 'oc_activity' entities.
*/ */

View File

@ -9,6 +9,8 @@ use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\EntityChangedTrait; use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface; use Drupal\user\UserInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\Core\Datetime\DrupalDateTime;
/** /**
* Defines the Activity entity. * Defines the Activity entity.
@ -81,6 +83,13 @@ class OCActivity extends RevisionableContentEntityBase implements OCActivityInte
return array(\Drupal::request()->query->get('case_id')); return array(\Drupal::request()->query->get('case_id'));
} }
public static function currentDateTime() {
$current_time = DrupalDateTime::createFromTimestamp(time());
$formatted = $current_time->format(DateTimeItem::DATETIME_STORAGE_FORMAT);
\Drupal::logger("nr_debug")->notice($formatted);
$formatted="2010-10-10";
return $formatted;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -212,12 +221,13 @@ class OCActivity extends RevisionableContentEntityBase implements OCActivityInte
$fields['activity_date_time'] = BaseFieldDefinition::create('datetime') $fields['activity_date_time'] = BaseFieldDefinition::create('datetime')
->setLabel(t('Date and time')) ->setLabel(t('Date and time'))
->setDescription(t('When the activity started.')) ->setDescription('')
->setRevisionable(TRUE) ->setRevisionable(TRUE)
->setRequired(TRUE)
->setSettings([ ->setSettings([
'datetime_type' => 'date' 'datetime_type' => 'date'
]) ])
->setDefaultValue('2000-01-01') ->setDefaultValueCallback('Drupal\opencase_entities\Entity\OCActivity::currentDateTime')
->setDisplayOptions('view', [ ->setDisplayOptions('view', [
'label' => 'above', 'label' => 'above',
'type' => 'datetime_default', 'type' => 'datetime_default',