You are here

public function PublicationDateItem::preSave in Publication Date 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldType/PublicationDateItem.php \Drupal\publication_date\Plugin\Field\FieldType\PublicationDateItem::preSave()

Defines custom presave behavior for field values.

This method is called during the process of saving an entity, just before values are written into storage. When storing a new entity, its identifier will not be available yet. This should be used to massage item property values or perform any other operation that needs to happen before values are stored. For instance this is the proper phase to auto-create a new entity for an entity reference field item, because this way it will be possible to store the referenced entity identifier.

Overrides ChangedItem::preSave

File

src/Plugin/Field/FieldType/PublicationDateItem.php, line 62
Contains \Drupal\publication_date\Plugin\Field\FieldType\PublicationDateItem.

Class

PublicationDateItem
Defines the 'published_at' entity field type.

Namespace

Drupal\publication_date\Plugin\Field\FieldType

Code

public function preSave() {

  // If no publication date has been set and the entity is unpublished then
  // store the default publication date.
  if (!$this
    ->isPublished() && !$this->value) {
    $this->value = PUBLICATION_DATE_DEFAULT;
  }
  elseif ($this
    ->isPublished() && $this->value == PUBLICATION_DATE_DEFAULT) {
    $this->value = \Drupal::time()
      ->getRequestTime();
  }

  // Set the timestamp to request time if it is not set.
  if (!$this->value) {
    $this->value = \Drupal::time()
      ->getRequestTime();
  }
}