Commit 533ee3a1 authored by webchick's avatar webchick

Issue #2145103 by Berdir, das-peter, fago, amateescu: Provide non-configurable...

Issue #2145103 by Berdir, das-peter, fago, amateescu: Provide non-configurable field types for entity created, changed and timestamp fields.
parent ddade5bf
<?php
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\Field\FieldType\ChangedItem.
*/
namespace Drupal\Core\Field\Plugin\Field\FieldType;
/**
* Defines the 'changed' entity field type.
*
* @FieldType(
* id = "changed",
* label = @Translation("Last changed"),
* description = @Translation("An entity field containing a UNIX timestamp of when the entity has been last updated."),
* configurable = FALSE,
* constraints = {
* "ComplexData" = {"value" = {"EntityChanged" = {}}}
* }
* )
*/
class ChangedItem extends CreatedItem {
/**
* {@inheritdoc}
*/
public function preSave() {
parent::preSave();
$this->value = REQUEST_TIME;
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\Field\FieldType\CreatedItem.
*/
namespace Drupal\Core\Field\Plugin\Field\FieldType;
/**
* Defines the 'created' entity field type.
*
* @FieldType(
* id = "created",
* label = @Translation("Created"),
* description = @Translation("An entity field containing a UNIX timestamp of when the entity has been created."),
* configurable = FALSE
* )
*/
class CreatedItem extends TimestampItem {
/**
* {@inheritdoc}
*/
public function applyDefaultValue($notify = TRUE) {
parent::applyDefaultValue($notify);
// Created fields default to the current timestamp.
$this->setValue(array('value' => REQUEST_TIME), $notify);
return $this;
}
}
......@@ -27,7 +27,7 @@
* label = @Translation("Entity reference"),
* description = @Translation("An entity field containing an entity reference."),
* configurable = FALSE,
* constraints = {"ValidReference" = TRUE}
* constraints = {"ValidReference" = {}}
* )
*/
class EntityReferenceItem extends FieldItemBase {
......
<?php
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\Field\FieldType\TimestampItem.
*/
namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\TypedData\DataDefinition;
/**
* Defines the 'timestamp' entity field type.
*
* @FieldType(
* id = "timestamp",
* label = @Translation("Timestamp"),
* description = @Translation("An entity field containing a UNIX timestamp value."),
* configurable = FALSE
* )
*/
class TimestampItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('timestamp')
->setLabel(t('Timestamp value'));
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldDefinitionInterface $field_definition) {
return array(
'columns' => array(
'value' => array(
'type' => 'int',
'not null' => FALSE,
),
),
);
}
}
......@@ -17,7 +17,7 @@
* @DataType(
* id = "email",
* label = @Translation("Email"),
* constraints = {"Email" = TRUE}
* constraints = {"Email" = {}}
* )
*/
class Email extends String implements StringInterface {
......
......@@ -401,10 +401,6 @@ public function getConstraints(DataDefinitionInterface $definition) {
// Add in constraints specified by the data type.
if (isset($type_definition['constraints'])) {
foreach ($type_definition['constraints'] as $name => $options) {
// Annotations do not support empty arrays.
if ($options === TRUE) {
$options = array();
}
$constraints[] = $validation_manager->create($name, $options);
}
}
......
......@@ -54,10 +54,10 @@ public function validate($value, Constraint $constraint) {
}
// @todo: Move those to separate constraint validators.
try {
if ($typed_data instanceof DateTimeInterface && $typed_data->getDateTime()->hasErrors()) {
if ($typed_data instanceof DateTimeInterface && $typed_data->getDateTime() && $typed_data->getDateTime()->hasErrors()) {
$valid = FALSE;
}
if ($typed_data instanceof DurationInterface && !($typed_data->getDuration() instanceof \DateInterval)) {
if ($typed_data instanceof DurationInterface && $typed_data->getDuration() && !($typed_data->getDuration() instanceof \DateInterval)) {
$valid = FALSE;
}
}
......
......@@ -148,13 +148,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Refresh'))
->setDescription(t('How often to check for new feed items, in seconds.'));
// @todo Convert to a "timestamp" field in https://drupal.org/node/2145103.
$fields['checked'] = FieldDefinition::create('integer')
$fields['checked'] = FieldDefinition::create('timestamp')
->setLabel(t('Checked'))
->setDescription(t('Last time feed was checked for new items, as Unix timestamp.'));
// @todo Convert to a "timestamp" field in https://drupal.org/node/2145103.
$fields['queued'] = FieldDefinition::create('integer')
$fields['queued'] = FieldDefinition::create('timestamp')
->setLabel(t('Queued'))
->setDescription(t('Time when this feed was queued for refresh, 0 if not queued.'));
......@@ -178,8 +176,9 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Etag'))
->setDescription(t('Entity tag HTTP response header, used for validating cache.'));
// @todo Convert to a "changed" field in https://drupal.org/node/2145103.
$fields['modified'] = FieldDefinition::create('integer')
// This is updated by the fetcher and not when the feed is saved, therefore
// it's a timestamp and not a changed field.
$fields['modified'] = FieldDefinition::create('timestamp')
->setLabel(t('Modified'))
->setDescription(t('When the feed was last modified, as a Unix timestamp.'));
......
......@@ -47,17 +47,6 @@ public function label() {
return $this->get('title')->value;
}
/**
* {@inheritdoc}
*/
public function postCreate(EntityStorageControllerInterface $storage_controller) {
parent::postCreate($storage_controller);
if ($this->getPostedTime() === NULL) {
$this->setPostedTime(REQUEST_TIME);
}
}
/**
* {@inheritdoc}
*/
......@@ -93,8 +82,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Description'))
->setDescription(t('The body of the feed item.'));
// @todo Convert to a "timestamp" field in https://drupal.org/node/2145103.
$fields['timestamp'] = FieldDefinition::create('integer')
$fields['timestamp'] = FieldDefinition::create('created')
->setLabel(t('Posted timestamp'))
->setDescription(t('Posted date of the feed item, as a Unix timestamp.'));
......
......@@ -98,16 +98,6 @@ public function getTheme() {
return $this->theme;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageControllerInterface $storage_controller) {
parent::preSave($storage_controller);
// Before saving the custom block, set changed time.
$this->set('changed', REQUEST_TIME);
}
/**
* {@inheritdoc}
*/
......@@ -197,11 +187,9 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Revision log message'))
->setDescription(t('The revision log message.'));
// @todo Convert to a "changed" field in https://drupal.org/node/2145103.
$fields['changed'] = FieldDefinition::create('integer')
$fields['changed'] = FieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the custom block was last edited.'))
->setPropertyConstraints('value', array('EntityChanged' => array()));
->setDescription(t('The time that the custom block was last edited.'));
return $fields;
}
......
......@@ -133,12 +133,6 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
} while (!\Drupal::lock()->acquire($lock_name));
$this->threadLock = $lock_name;
}
if (is_null($this->getCreatedTime())) {
$this->setCreatedTime(REQUEST_TIME);
}
if (is_null($this->getChangedTime())) {
$this->set('changed', $this->getCreatedTime());
}
// We test the value with '===' because we need to modify anonymous
// users as well.
if ($this->getOwnerId() === \Drupal::currentUser()->id() && \Drupal::currentUser()->isAuthenticated()) {
......@@ -276,16 +270,13 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t("The comment author's hostname."))
->setSetting('max_length', 128);
// @todo Convert to a "created" field in https://drupal.org/node/2145103.
$fields['created'] = FieldDefinition::create('integer')
$fields['created'] = FieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the comment was created.'));
// @todo Convert to a "changed" field in https://drupal.org/node/2145103.
$fields['changed'] = FieldDefinition::create('integer')
$fields['changed'] = FieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the comment was last edited.'))
->setPropertyConstraints('value', array('EntityChanged' => array()));
->setDescription(t('The time that the comment was last edited.'));
$fields['status'] = FieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
......
......@@ -45,7 +45,7 @@ public function access(Route $route, Request $request, AccountInterface $account
if ($entity = $request->attributes->get($entity_type)) {
$route_requirements = $route->getRequirements();
$operation = $route_requirements['_access_content_translation_manage'];
$controller = content_translation_controller($entity_type);
$controller = content_translation_controller($entity_type, $account);
// Load translation.
$translations = $entity->getTranslationLanguages();
......
......@@ -7,6 +7,7 @@
namespace Drupal\edit\Form;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Entity\EntityChangedInterface;
......@@ -226,17 +227,16 @@ protected function simplify(array &$form, array &$form_state) {
/**
* Finds the field name for the field carrying the changed timestamp, if any.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
*
* @return string|null
* The name of the field found or NULL if not found.
*/
protected function getChangedFieldName(EntityInterface $entity) {
foreach ($entity as $field_name => $field) {
$constraints = $field->getItemDefinition()->getConstraints();
if (isset($constraints['ComplexData']['value']['EntityChanged'])) {
return $field_name;
protected function getChangedFieldName(ContentEntityInterface $entity) {
foreach ($entity->getFieldDefinitions() as $field) {
if ($field->getType() == 'changed') {
return $field->getName();
}
}
}
......
......@@ -202,19 +202,7 @@ public static function preCreate(EntityStorageControllerInterface $storage_contr
public function preSave(EntityStorageControllerInterface $storage_controller) {
parent::preSave($storage_controller);
$this->changed->value = REQUEST_TIME;
if (empty($this->created->value)) {
$this->created->value = REQUEST_TIME;
}
$this->setSize(filesize($this->getFileUri()));
if (!isset($this->langcode->value)) {
// Default the file's language code to none, because files are language
// neutral more often than language dependent. Until we have better
// flexible settings.
// @todo See http://drupal.org/node/258785 and followups.
$this->langcode = Language::LANGCODE_NOT_SPECIFIED;
}
}
/**
......@@ -281,11 +269,11 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Status'))
->setDescription(t('The status of the file, temporary (0) and permanent (1).'));
$fields['created'] = FieldDefinition::create('integer')
$fields['created'] = FieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The timestamp that the file was created.'));
$fields['changed'] = FieldDefinition::create('integer')
$fields['changed'] = FieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The timestamp that the file was last changed.'));
......
......@@ -75,27 +75,6 @@ public function getRevisionId() {
return $this->get('vid')->value;
}
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageControllerInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
// @todo Handle this through property defaults.
if (empty($values['created'])) {
$values['created'] = REQUEST_TIME;
}
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageControllerInterface $storage_controller) {
parent::preSave($storage_controller);
// Before saving the node, set changed and revision times.
$this->changed->value = REQUEST_TIME;
}
/**
* {@inheritdoc}
*/
......@@ -410,16 +389,13 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the node is published.'));
// @todo Convert to a "created" field in https://drupal.org/node/2145103.
$fields['created'] = FieldDefinition::create('integer')
$fields['created'] = FieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the node was created.'));
// @todo Convert to a "changed" field in https://drupal.org/node/2145103.
$fields['changed'] = FieldDefinition::create('integer')
$fields['changed'] = FieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the node was last edited.'))
->setPropertyConstraints('value', array('EntityChanged' => array()));
->setDescription(t('The time that the node was last edited.'));
$fields['promote'] = FieldDefinition::create('boolean')
->setLabel(t('Promote'))
......@@ -429,8 +405,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Sticky'))
->setDescription(t('A boolean indicating whether the node should be displayed at the top of lists in which it appears.'));
// @todo Convert to a "timestamp" field in https://drupal.org/node/2145103.
$fields['revision_timestamp'] = FieldDefinition::create('integer')
$fields['revision_timestamp'] = FieldDefinition::create('timestamp')
->setLabel(t('Revision timestamp'))
->setDescription(t('The time that the current revision was created.'))
->setQueryable(FALSE);
......
......@@ -171,16 +171,6 @@ public static function postDelete(EntityStorageControllerInterface $storage_cont
}
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageControllerInterface $storage_controller) {
parent::preSave($storage_controller);
// Before saving the term, set changed time.
$this->changed->value = REQUEST_TIME;
}
/**
* {@inheritdoc}
*/
......@@ -243,10 +233,9 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setSetting('default_value', 0)
->setConstraints(array('TermParent' => array()));
$fields['changed'] = FieldDefinition::create('integer')
$fields['changed'] = FieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the term was last edited.'))
->setPropertyConstraints('value', array('EntityChanged' => array()));
->setDescription(t('The time that the term was last edited.'));
return $fields;
}
......
......@@ -73,9 +73,6 @@ public function isNew() {
static function preCreate(EntityStorageControllerInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
if (!isset($values['created'])) {
$values['created'] = REQUEST_TIME;
}
// Users always have the authenticated user role.
$values['roles'][] = DRUPAL_AUTHENTICATED_RID;
}
......@@ -489,19 +486,16 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t('Whether the user is active (1) or blocked (0).'))
->setSetting('default_value', 1);
// @todo Convert to a "created" field in https://drupal.org/node/2145103.
$fields['created'] = FieldDefinition::create('integer')
$fields['created'] = FieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the user was created.'));
// @todo Convert to a "timestamp" field in https://drupal.org/node/2145103.
$fields['access'] = FieldDefinition::create('integer')
$fields['access'] = FieldDefinition::create('timestamp')
->setLabel(t('Last access'))
->setDescription(t('The time that the user last accessed the site.'))
->setSetting('default_value', 0);
// @todo Convert to a "timestamp" field in https://drupal.org/node/2145103.
$fields['login'] = FieldDefinition::create('integer')
$fields['login'] = FieldDefinition::create('timestamp')
->setLabel(t('Last login'))
->setDescription(t('The time that the user last logged in.'))
->setSetting('default_value', 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment