You are here

public function ExifContent::handleDateField in Exif 8

Same name and namespace in other branches
  1. 8.2 src/ExifContent.php \Drupal\exif\ExifContent::handleDateField()

Handle date field.

Parameters

int $index: The index to set the new value.

\Drupal\Core\Field\FieldItemListInterface $field: The field to update.

string $exif_section: The exif section where value has been retrieved.

string $exif_name: The exif label where value has been retrieved.

string $exif_sanitized_value: The exif value to update.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to ExifContent::handleDateField()
ExifContent::handleField in src/ExifContent.php
Handle field by delegating to specific type handler.

File

src/ExifContent.php, line 580

Class

ExifContent
Class ExifContent make link between drupal content and file content.

Namespace

Drupal\exif

Code

public function handleDateField($index, FieldItemListInterface &$field, $exif_section, $exif_name, $exif_sanitized_value) {
  if ($exif_name == 'filedatetime') {
    $format = 'atom';
  }
  else {
    $format = 'exif';
  }
  $dateFormatStorage = Drupal::getContainer()
    ->get('entity.manager')
    ->getStorage('date_format');
  if ($dateFormatStorage instanceof EntityStorageInterface) {

    // Load format for parsing information from image.
    $dateFormat = $dateFormatStorage
      ->load($format);
    if ($dateFormat instanceof DateFormat) {

      // Exif internal format do not handle timezone :(
      // Using website timezone instead or default storage if none is defined.
      // TODO : drupal_get_user_timezone();
      // Parse string to date following chosen format.
      $date_datetime = DrupalDateTime::createFromFormat($dateFormat
        ->getPattern(), $exif_sanitized_value);

      // Load storage format.
      $storage_format = $field
        ->getFieldDefinition()
        ->getSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;

      // Format date to string for storage.
      $value = $date_datetime
        ->format($storage_format);

      // Store value.
      $field
        ->offsetSet($index, $value);
    }
  }
}