- Issue created by @lazzyvn
- 🇦🇺Australia almunnings Melbourne, 🇦🇺
Your array function
'fields' => fn() => $subFields,
Needs to have array keys. The key is the field name.
You're returning:
[ 0 => ['type'..., 'description'], 1 => ['type'..., 'description'], ]
You need to return something like.
[ $name => ['type'..., 'description'], ]
Try this:
$types[$field->getTypeSdl()] = new ObjectType([ 'name' => $field->getTypeSdl(), 'description' => (string) $this->t('A data field is a specialty field provided by the CMS.'), 'fields' => function () use ($field) { $columns = array_keys($field->getFieldDefinition()->getSetting('columns')); $subFields = []; foreach ($columns as $column) { $subFields[$column] = [ 'type' => static::type($field->getSubfieldTypeSdl($column)), 'description' => (string) $this->t('The @field value of the data field', ['@field' => $column]), ]; } return $subFields; }, ]);
This makes it appear in the schema.
You'll then need to do a bit of work in your `DataFieldItem` class to return the correct value.
- 🇫🇷France lazzyvn paris
thanks i change to
$subFields = array_combine($columns, array_map(function ($name) use ($field) { return [ 'type' => static::type($field->getSubfieldTypeSdl($name)), 'description' => (string) $this->t('The @field value of the data field', ['@field' => $name]), ]; }, $columns));
Error gone.
I got values brut, Now i need to convert id of entity reference, file to object, uri id to link, it must do logic in resolveFieldItempublic function resolveFieldItem(FieldItemInterface $item, FieldContext $context) { $columns = array_keys($this->getFieldDefinition()->getSetting('columns')); $subfields = []; foreach ($columns as $subfield) { $subfields[$subfield] = $this->getSubField($subfield, $item, $context) ?: NULL; } return $subfields; }
do you have any service or helper to convert to graphql compose format?
- 🇫🇷France lazzyvn paris
Hello,
I tried the data field with the subfield as the entity reference. It only shows the nid but I want all the node's field value like jsonapi. i try converting object node to array.
In explorer it shows NULL in subfield, what did I do wrong?protected function getSubField(string $subfield, FieldItemInterface $item, FieldContext $context) { $value = $item->{$subfield}; // Attempt to load the plugin for the field type. $plugin = $this->getSubfieldPlugin($subfield); if (!$plugin) { return $value; } $storageSettings = $item->getFieldDefinition()->getSetting('columns')[$subfield]; $fieldSettings = $item->getFieldDefinition()->getSetting('field_settings')[$subfield]; if ($storageSettings["type"] == 'entity_reference' && is_numeric($value)) { $entity = \Drupal::entityTypeManager()->getStorage($fieldSettings["entity_reference_type"])->load($value); $value = []; foreach ($entity->getFields() as $name => $field) { $value[$name] = $field->getString(); } return $value; }
- 🇫🇷France lazzyvn paris
Please support datafield with entity
I try but it doesn't work smothly
https://git.drupalcode.org/project/datafield/-/blob/2.x/src/Plugin/Graph...protected function getSubField(string $subfield, FieldItemInterface $item, FieldContext $context) { $value = $item->{$subfield}; // Attempt to load the plugin for the field type. $plugin = $this->getSubfieldPlugin($subfield); if (!$plugin) { return $value; } $storageSettings = $item->getFieldDefinition()->getSetting('columns')[$subfield]; $fieldSettings = $item->getFieldDefinition()->getSetting('field_settings')[$subfield]; if ($storageSettings["type"] == 'entity_reference' && is_numeric($value)) { $entity = \Drupal::entityTypeManager()->getStorage($fieldSettings["entity_reference_type"])->load($value); $item->set('entity', $entity); $value = $entity->uuid(); } if ($storageSettings["type"] == 'file' && is_numeric($value)) { $entity = \Drupal::entityTypeManager()->getStorage('file')->load($value); $item->set('entity', $entity); } // Check if it has a resolver we can hijack. $class = new \ReflectionClass($plugin['class']); if (!$class->implementsInterface(FieldProducerItemInterface::class)) { return $value; } // Create an instance of the graphql plugin. $instance = $this->gqlFieldTypeManager->createInstance($plugin['id'], []); // Clone the current item into a new object for safety. $clone = clone $item; // Generically set the value. Relies on magic method __set(). $clone->value = $value; // Call the plugin resolver on the subfield. return $instance->resolveFieldItem($clone, $context); }