You are here

public function EntityCreated::resolve in GraphQL 8.4

Resolver.

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

string|null $format:

Return value

string|null

File

src/Plugin/GraphQL/DataProducer/Entity/EntityCreated.php, line 40

Class

EntityCreated
Returns the created time of an entity if it supports it.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer\Entity

Code

public function resolve(EntityInterface $entity, $format = NULL) {

  // `getCreatedTime` is on NodeInterface which feels weird, since there
  // is a generic `EntityInterface`. Checking for method existence for now.
  if (method_exists($entity, 'getCreatedTime')) {
    $datetime = new \DateTime();
    $datetime
      ->setTimestamp($entity
      ->getCreatedTime());
    return $datetime
      ->format($format ?? \DateTime::ISO8601);
  }
  return NULL;
}