You are here

protected function ContentEntityTypeResolver::resolveFields in GraphQL 8.2

Same name and namespace in other branches
  1. 8 src/TypeResolver/ContentEntityTypeResolver.php \Drupal\graphql\TypeResolver\ContentEntityTypeResolver::resolveFields()

Helper function to resolve the list of available fields for a type.

Parameters

\Drupal\Core\TypedData\ComplexDataDefinitionInterface $type: The typed data type to resolve the field list for.

Return value

array The list of fields for the given type.

Overrides TypedDataTypeResolver::resolveFields

1 call to ContentEntityTypeResolver::resolveFields()
ContentEntityTypeResolver::getEntityTypeFieldMap in src/TypeResolver/ContentEntityTypeResolver.php
Helper function to retrieve the field schema definitions for an entity.

File

src/TypeResolver/ContentEntityTypeResolver.php, line 190

Class

ContentEntityTypeResolver
Resolves the schema for content entities.

Namespace

Drupal\graphql\TypeResolver

Code

protected function resolveFields(ComplexDataDefinitionInterface $type) {

  /** @var \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface $type */
  $propertyDefinitions = $type
    ->getPropertyDefinitions();
  $entityTypeId = $type
    ->getEntityTypeId();
  $defaultFields = [];
  if (empty($type
    ->getBundles())) {
    $enumName = StringHelper::formatTypeName("entity:view:modes:{$entityTypeId}");
    $enumValues = $this->entityManager
      ->getViewModes($entityTypeId);
    $defaultFields['rendered:output'] = [
      'type' => Type::stringType(),
      'args' => !empty($enumValues) ? [
        'viewMode' => [
          'type' => new EnumType($enumName, $enumValues),
        ],
      ] : [],
      'resolve' => [
        __CLASS__,
        'getRenderedOutput',
      ],
      'resolveData' => [
        'type' => $entityTypeId,
      ],
    ];
  }
  $typeFields = array_reduce(array_keys($propertyDefinitions), function ($previous, $propertyKey) use ($propertyDefinitions) {
    $propertyDefinition = $propertyDefinitions[$propertyKey];
    $sanitizedPropertyKey = $propertyKey;

    // Remove the 'field_' prefix for configured fields.
    if ($propertyDefinition instanceof FieldDefinitionInterface && strpos($propertyKey, 'field_') === 0) {
      $sanitizedPropertyKey = substr($propertyKey, 6);
    }
    if ($resolvedProperty = $this
      ->resolveFieldFromProperty($propertyKey, $propertyDefinition)) {
      return $previous + [
        $sanitizedPropertyKey => $resolvedProperty,
      ];
    }
    return $previous;
  }, $defaultFields);
  return $typeFields;
}