1

With the JSON API module, I am trying to access paragraphs. In the JSON object is does not allow me to access the nested paragraphs.

How would I go about accessing the paragraphs with the JSON API?

*****Update with Solution*****

  1. In the api call you need to have the correct URL format: /jsonapi/{entity_type_id}/{bundle_id}[/{entity_id}]?_format=api_json. The key here is to add this at the end of your the URL for the api call: ?include={name of paragraph}. For example: /jsonapi/node/page?_format=api_json&include=field_testing_json_api

  2. Once you have the correct API call, you need to go to the Drupal admin dashboard and go to people/permissions and under the Paragraphs Type Permissions section you need to grant access to view content to the specific paragraph you want the user to access.

2

2 steps required :)

  1. create the paragraph(s) by doing a POST request, and receiving the revision ID of the created paragraph,
  2. update the parent entity by assigning the paragraphs' revision ids to the paragraph field (entity revision reference field).

Step 1.

jsonbody = {
    'data': {
        'type': 'paragraph--study_history',
        'attributes': {
            'parent_id': localStorage.getItem('uid'),
            'parent_type': 'user',
            'parent_field_name': 'field_study_history'
        },
        'relationships': {
            'field_school_ref': {
                'data': {
                    'type': 'node--school',
                    'id': this.reg_selectedUni['id']
                }
            }
        }
    }
};

return this.api.apiPost('/jsonapi/paragraph/study_history', {}, jsonbody)

We are using our custom service api (ApiService.ts) which has the apiPost function that does exactly what you'd imagine :) So the logic above should be easy to adapt to your own code.

Note that if you want to add more fields (text fields for example, you add them to the attributes section).


Step 2. Here’s how we update the user, PATCH it to have the created “study history” paragraphs.

const jsonbody = {
    'data': {
        'type': 'user--user',
        'id': uuid,
        'relationships': {
            'field_study_history': {
                'data': [
                    {
                        'type': 'paragraph--study_history',
                        'id': schoolhist_info['data']['id'],
                        'meta': {
                            'target_revision_id': schoolhist_info['data']['attributes']['revision_id'],
                        }
                    },
                    {
                        'type': 'paragraph--study_history',
                        'id': unihist_info['data']['id'],
                        'meta': {
                            'target_revision_id': unihist_info['data']['attributes']['revision_id'],
                        }
                    }
                ]
            }
        }
    }
};
return this.api.apiPatch(this.endpoint.getUsers + '/' + uuid, {}, jsonbody);

Where this.endpoint.getUsers is /jsonapi/user/user. This should be enough to get it to work, else comment and I can update.

  • 1
    Yes, I can also access the the type, id, and meta with the json api module. What I can't access and what I don't see in your answer are the paragraphs fields(for example title and body). How would I access the paragraphs fields? – Che Figueroa Dec 7 '18 at 3:49
  • Added the first part that should answer your need exactly, sorry, misread at first :) – LittleTiger Dec 7 '18 at 6:53
0

Try using http://yourhost.local/jsonapi/paragraph/myparagraph

It is navigable through your browser http://yourhost.local/jsonapi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.