Basado en Headless Drupal — Build a Drupal 8 API with a ReactJS Front-End
In order to begining you can choose which option to follow:
- Just Clone or download the repo called
durable-drupal-cms-client-01
(This contains a React App ready to use) - Skip and follow only the steps according
2. Create Drupal API
section - Using the repo you don't need to do
3. Integrate and display data
section - Go to
4. Drupal headless running
section.
Or
Please follow the steps in order to create React app:
First of all we are going to create a React App. It’s where the decoupled Drupal website originates. We will create a simple welcome page.
Create a simple basic folder structure with nothing extra, like the image below:
Use a cdn to add React to my index.html file:
<script src="https://unpkg.com/react@15/dist/react.js"></script> <script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
Now add a
index.html
:
Create a file app.js
inside js folder and add “React is awesome!” example:
ReactDOM.render(
, document.getElementById('container') );
Also include babel to transform JSX syntax (from the previous step) into ECMAScript and include app.js file inside index.html
:
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script> <script type="text/babel" src="js/app.js"></script>
Important Now, if you try to open index.html file in your Chrome browser (in Firefox it seems to work without advantage) you should have the XMLHttpRequest error. That means you need to use a HTTP server to make it work. The simplest way to start a new server is to use PHP's 5.4 and later built-in web server. You need to run this command inside of our React app directory using terminal:
php -S localhost:8000
Now if you go to http://localhost:8000/ page you should see “React is awesome!”. It means that React works and we’re ready to go.
Create a new Drupal installation. For your convenience it's recommended to choose a standard Drupal installation over the minimal.
Now add a new content type called "Blog". Let’s create with following fields:
- Title
- Body
- Image
Create a few nodes of "Blogs" content type. And you can see:
Go to admin/modules
and enable all modules under the web services:
- Create a new view on
admin/structure/views/add
calledBlogs API
in order to create a required Web Service. - Add a filter for this view by content type "Blogs"
- Click the "Provide a REST export" checkbox and enter REST export path. For example:
api/blogs
. Please see the image below:
Now, if you go to api/blogs page you should see JSON data:
We can chose the axios client for the server request instead of jQuery because we need only AJAX features and we don't want to load the whole library (jQuery). So add this into index.html:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
- Here is the full content of
app.js
file. We will display all the blogs nodes: - Render into DOM config. Please config
App source
with yourjson data
path from your drupal site. According this example we calledapi/blogs
. Like the image below:
- Note: drupalheadlessexample replace by your drupal site name
class App extends React.Component {
constructor() {
super();
// Set up initial state
this.state = {
data: []
}
}
// after a component is rendered for the first time call the componentDidMount() method
componentDidMount() {
var th = this;
this.serverRequest = axios.get(this.props.source)
.then(function(blog) {
th.setState({
data: blog.data
});
})
}
// call the componentWillUnMount() method before a component is unmounted from the DOM
componentWillUnmount() {
this.serverRequest.abort();
}
render() {
var titles = [];
this.state.data.forEach(item => {
titles.push(<h3 className="blogs">{item.title[0].value}</h3> );
});
return (
<div className="container">
<div className="row">
<h1 className="title">Blogs:</h1>
{titles}
</div>
</div>
);
}
}
// render into DOM
ReactDOM.render(
<App source="http://localhost/drupalheadlessexample/api/blogs" />,
document.getElementById('container')
);
In order to avoid cross-domain request like: “No 'Access-Control-Allow-Origin' header is present on the requested resource” install the following chrome extension
- Go to
http://localhost:8000/
- And you should see something like this: