View source
<?php
namespace Drupal\scheduled_updates\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\scheduled_updates\ScheduledUpdateInterface;
use Drupal\user\UserInterface;
class ScheduledUpdate extends ContentEntityBase implements ScheduledUpdateInterface {
use EntityChangedTrait;
use StringTranslationTrait;
public function getUpdateEntityIds() {
$ids = [];
$field_values = $this->entity_ids
->getValue();
foreach ($field_values as $field_value) {
$ids[] = $field_value['target_id'];
}
return $ids;
}
public function setUpdateEntityIds(array $update_entity_ids) {
$this->entity_ids = $update_entity_ids;
}
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Scheduled update entity.'))
->setReadOnly(TRUE);
$fields['type'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Type'))
->setDescription(t('The Scheduled update type/bundle.'))
->setSetting('target_type', 'scheduled_update_type')
->setRequired(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Scheduled update entity.'))
->setReadOnly(TRUE);
$status_options = [
ScheduledUpdateInterface::STATUS_UNRUN => 'Un-run',
ScheduledUpdateInterface::STATUS_INQUEUE => 'In Queue',
ScheduledUpdateInterface::STATUS_REQUEUED => 'Re-queued',
ScheduledUpdateInterface::STATUS_SUCCESSFUL => 'Successful',
ScheduledUpdateInterface::STATUS_UNSUCESSFUL => 'Un-successful',
ScheduledUpdateInterface::STATUS_INACTIVE => 'Inactive',
];
$fields['status'] = BaseFieldDefinition::create('list_integer')
->setLabel(t('Status'))
->setDescription(t('The status of the update.'))
->setDefaultValue(ScheduledUpdateInterface::STATUS_UNRUN)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'list_default',
'weight' => -100,
])
->setSetting('allowed_values', $status_options);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Created by'))
->setDescription(t('The user ID of author of the Scheduled update entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\\scheduled_updates\\Entity\\ScheduledUpdate::getCurrentUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'author',
'weight' => 0,
])
->setDisplayConfigurable('view', TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The language code for the Scheduled update entity.'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['update_timestamp'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Update Date/time'))
->setDescription(t('The time that the update will happen.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setRequired(TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'timestamp',
'weight' => 10,
])
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => -9,
])
->setDisplayConfigurable('form', TRUE);
$fields['entity_ids'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Entities to Update'))
->setDescription(t('The entities that will be updated.'))
->setRequired(TRUE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
return $fields;
}
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
if ($update_type = ScheduledUpdateType::load($bundle)) {
$fields['entity_ids'] = clone $base_field_definitions['entity_ids'];
$definition =& $fields['entity_ids'];
if ($update_type
->isIndependentType()) {
$definition
->setSetting('target_type', $update_type
->getUpdateEntityType());
$definition
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => -10,
]);
$runner_settings = $update_type
->getUpdateRunnerSettings();
if (isset($runner_settings['bundles'])) {
$bundles = array_filter($runner_settings['bundles']);
$definition
->setSetting('handler_settings', [
'target_bundles' => $bundles,
]);
}
}
else {
$definition
->setDisplayConfigurable('form', FALSE);
$definition
->setDisplayConfigurable('view', FALSE);
}
return $fields;
}
return [];
}
public function label() {
if (!$this
->get('update_timestamp')
->isEmpty()) {
$formatter = \Drupal::service('date.formatter');
return $formatter
->format($this
->get('update_timestamp')
->getString());
}
return $this
->t('No update time specified');
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
}
public function isArchived() {
return $this->status->value === ScheduledUpdateInterface::STATUS_SUCCESSFUL || $this->status->value === ScheduledUpdateInterface::STATUS_UNSUCESSFUL;
}
}