Skip to content

Commit 82d79cf

Browse files
keveeMarcy Sutton
authored and
Marcy Sutton
committed
Docs: Recipe for sourcing from Drupal (#17378)
* Added sourcing from Drupal * Copy edit * Copy editing * Removed shell formatting * Removed slash * Added example page * Lint * Better structure for sample page Co-Authored-By: Marcy Sutton <[email protected]> * Unneeded phrase Co-Authored-By: Marcy Sutton <[email protected]> * Linting
1 parent 97f328c commit 82d79cf

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

docs/docs/recipes.md

+98
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,104 @@ export default ({ pageContext: { allPokemon } }) => (
994994
- More on using unstructured data in [Using Gatsby without GraphQL](/docs/using-gatsby-without-graphql/)
995995
- When and how to [query data with GraphQL](/docs/querying-with-graphql/) for more complex Gatsby sites
996996

997+
### Sourcing content from Drupal
998+
999+
#### Prerequisites
1000+
1001+
- A [Gatsby site](/docs/quick-start)
1002+
- A [Drupal](http://drupal.org) site
1003+
- The [JSON:API module](https://www.drupal.org/project/jsonapi) installed and enabled on the Drupal site
1004+
1005+
#### Directions
1006+
1007+
1. Install the `gatsby-source-drupal` plugin.
1008+
1009+
```
1010+
npm install --save gatsby-source-drupal
1011+
```
1012+
1013+
2. Edit your `gatsby-config.js` file to enable the plugin and configure it.
1014+
1015+
```javascript:title=gatsby-config.js
1016+
module.exports = {
1017+
plugins: [
1018+
{
1019+
resolve: `gatsby-source-drupal`,
1020+
options: {
1021+
baseUrl: `https://your-website/`,
1022+
apiBase: `api`, // optional, defaults to `jsonapi`
1023+
},
1024+
},
1025+
],
1026+
}
1027+
```
1028+
1029+
3. Start the development server with `gatsby develop`, and open the GraphiQL explorer at `http://localhost:8000/___graphql`. Under the Explorer tab, you should see new node types, such as `allBlockBlock` for Drupal blocks, and one for every content type in your Drupal site. For example, if you have a "Page" content type, it will be available as `allNodePage`. To query all "Page" nodes for their title and body, use a query like:
1030+
1031+
```graphql
1032+
{
1033+
allNodePage {
1034+
edges {
1035+
node {
1036+
title
1037+
body {
1038+
value
1039+
}
1040+
}
1041+
}
1042+
}
1043+
}
1044+
```
1045+
1046+
4. To use your Drupal data, create a new page in your Gatsby site at `src/pages/drupal.js`. This page will list all Drupal "Page" nodes.
1047+
1048+
_**Note:** the exact GraphQL schema will depend on your how Drupal instance is structured._
1049+
1050+
```jsx:title=src/pages/drupal.js
1051+
import React from "react"
1052+
import { graphql } from "gatsby"
1053+
1054+
const DrupalPage = ({ data }) => (
1055+
<div>
1056+
<h1>Drupal pages</h1>
1057+
<ul>
1058+
{data.allNodePage.edges.map(({ node, index }) => (
1059+
<li key={index}>
1060+
<h2>{node.title}</h2>
1061+
<div>
1062+
{node.body.value}
1063+
</div>
1064+
</li>
1065+
))}
1066+
</ul>
1067+
</div>
1068+
)
1069+
1070+
export default DrupalPage
1071+
1072+
export const query = graphql`
1073+
{
1074+
allNodePage {
1075+
edges {
1076+
node {
1077+
title
1078+
body {
1079+
value
1080+
}
1081+
}
1082+
}
1083+
}
1084+
}
1085+
```
1086+
1087+
5. With the development server running, you can view the new page by visiting `http://localhost:8000/drupal`.
1088+
1089+
#### Additional Resources
1090+
1091+
- [Using Decoupled Drupal with Gatsby](/blog/2018-08-13-using-decoupled-drupal-with-gatsby/)
1092+
- [More on sourcing from Drupal](/docs/sourcing-from-drupal)
1093+
- [Tutorial: Programmatically create pages from data](/tutorial/part-seven/)
1094+
9971095
## 6. Querying data
9981096

9991097
### Querying data with a Page Query

0 commit comments

Comments
 (0)