Paragraphs weight delta

Created on 20 July 2023, 11 months ago
Updated 25 July 2023, 11 months ago

Is it possible for a field with multiple paragraphs in it to be able to get the position of these paragraphs individually? I'm building an API and the order of these paragraphs should be visible in the queries.

✨ Feature request
Status

Closed: works as designed

Version

2.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium CedricL

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @CedricL
  • Assigned to AlMunnings
  • πŸ‡¦πŸ‡ΊAustralia AlMunnings Melbourne

    Yeah lets do it. I’ll try get it done tomorrow and into dev.

  • Status changed to Postponed: needs info 11 months ago
  • πŸ‡¦πŸ‡ΊAustralia AlMunnings Melbourne

    OOooohkay!
    Hopefully this is enough information for you:

    I've updated the docs to help with your query:
    https://drupal-graphql-compose.github.io/documentation/#/extending/custo...

    This is possible using hooks in a custom module on your end.

    I'm hesitant to add it to the schema, as the data doesn't really exist and isn't a schema requiremen... The return order of the data is the same as the delta. So you use case might be a little unique in the need for it to be in the data.

    What you want though, is 100% possible, just using a few hooks.

    
    /**
     * @file
     * My drupal module that extends graphql_compose.
     */
    
    declare(strict_types=1);
    
    use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
    use Drupal\Core\Entity\EntityTypeInterface;
    use Drupal\Core\Field\BaseFieldDefinition;
    use Drupal\paragraphs\ParagraphInterface;
    
    /**
     * Implements hook_entity_base_field_info().
     *
     * Add a weight field to paragraphs.
     */
    function mymodule_entity_base_field_info(EntityTypeInterface $entity_type): ?array {
      if ($entity_type->id() === 'paragraph') {
        $fields = [];
        $fields['weight'] = BaseFieldDefinition::create('integer')
          ->setLabel(t('Weight'))
          ->setDescription(t('The delta weight of this paragraph within a fielded context.'))
          ->setComputed(TRUE)
          ->setDefaultValue(0);
        return $fields;
      }
    }
    
    /**
     * Implements hook_graphql_compose_field_results_alter().
     *
     * Modify the entity results as they are given to the schema.
     *
     * You could also compute this somewhere else, but for this example
     * we'll do it here.
     *
     * The delta is the position of the paragraph in the field.
     */
    function mymodule_graphql_compose_field_results_alter(array &$results, array $context, RefinableCacheableDependencyInterface $metadata): void {
      foreach ($results as $delta => $result) {
        if ($result instanceof ParagraphInterface) {
          $result->set('weight', $delta ?? 0);
        }
      }
    }
    
    /**
     * Implements hook_graphql_compose_entity_base_fields_alter().
     *
     * Add the new "weight" field to the interface.
     */
    function mymodule_graphql_compose_entity_base_fields_alter(array &$fields, string $entity_type_id): void {
      if ($entity_type_id === 'paragraph') {
        // You could use any entity type base_field annotation in the array.
        // @see https://drupal-graphql-compose.github.io/documentation/#/extending/entity_type
        $fields['weight'] = [];
      }
    }
    
    

    Does this answer the feature request?

  • Status changed to Closed: works as designed 11 months ago
  • πŸ‡§πŸ‡ͺBelgium CedricL

    #3 does the trick. Thanks for the help!

Production build 0.69.0 2024