Primitive TypedData fields are not cast properly

Created on 27 September 2014, about 11 years ago
Updated 3 October 2025, about 1 month ago

Problem/Motivation

I would expect integer fields such as timestamps to be cast to an int but instead a string is returned. For example:

/** @var \Drupal\aggregator\Entity\Feed $feed */
var_dump($feed->getLastCheckedTime());

// Actual: string '1411796607 ' (length=11)
// Expected: int 1411796607

Proposed resolution

I'd like to know if this is expected or whether there it's an issue? Or maybe, a link to an existing issue?

Additional info

tim.plunkett pointed out that FieldItemBase::__get() calls getValue() rather than getCastedValue().

  /**
   * {@inheritdoc}
   */
  public function __get($name) {
    // There is either a property object or a plain value - possibly for a
    // not-defined property. If we have a plain value, directly return it.
    if (isset($this->values[$name])) {
      return $this->values[$name];
    }
    elseif (isset($this->properties[$name])) {
      return $this->properties[$name]->getValue();
    }
  }
Feature request
Status

Postponed: needs info

Version

11.0 🔥

Component

typed data system

Created by

🇦🇺Australia benjy

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • DrupalWTF

    Worse Than Failure. Approximates the unpleasant remark made by Drupal developers when they first encounter a particular (mis)feature.

  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024