Build Your Own Mobile App Using Ionic and Drupal 8
Build Your Own Mobile App Using Ionic and Drupal 8
Build Your Own Mobile App Using Ionic and Drupal 8
and Drupal 8
Ever since the release of Drupal 8, the most popular keyword search in the framework is Headless
Drupal also referred as Decoupled Drupal. Headless Drupal is something where you use Drupal
to store data that can be accessed in multiple applications (via REST APIs). The front end
frameworks like Ionic interacts with Drupal using HTTP requests.
Check out how to use this feature of Drupal and Ionic framework to build a hybrid mobile
application.
What is ionic?
In simple terms, Ionic is an open source framework that is built atop Apache Cordova to develop
hybrid mobile apps. Hybrid apps are not developed specific to a platform that means you can
manage a single codebase for different platforms. Lets build a simple movie listing page.
Prerequisites:
Installing Ionic:
To install Ionic you need to install Apache Cordova, nodejs, npm in your system. Run the
following codes.
Drupal Set up :
Enable the following modules for rest API
Enable CORS (Cross Origin Resource Sharing) for accessing the data between
applications. Change the CORS config as per the origins in services.yml file:
<script
src="https://gist.github.com/sravyachalla/b334835faea00094cbef08f85aa805c3.js"></script>
cors.config:
enabled: true
# Specify allowed headers, like 'x-allowed-header'.
allowedHeaders: ['*']
# Specify allowed request methods, specify ['*'] to allow all possible ones.
allowedMethods: ['*']
# Configure requests allowed from specific origins.
allowedOrigins: ['*']
# Sets the Access-Control-Expose-Headers header.
exposedHeaders: true
# Sets the Access-Control-Max-Age header.
maxAge: false
# Sets the Access-Control-Allow-Credentials header.
supportsCredentials: true
A content type for movie :
Step 1 - Create an Ionic menu based app using the below command:
You can choose tabs/blank instead of side menu for your app.
Step 2 - Run the app. Navigate to the app folder.
Ionic serve / ionic serve --lab (This allows you to view your app in both ios and android platforms)
Step 4 - Navigate to app folder. Edit the movie-list.ts from App Folder > src > pages > movie-list.
This is where the view will be requested for data.
<script
src="https://gist.github.com/sravyachalla/8de29da787faab5885337bb45f1bcbf2.js"></script>
The movies variable declared in movie-list.ts can be accessed in the movie-list.html. Below
ion-header (similar to head in HTML), add ion-content (similar to body in HTML).
<script
src="https://gist.github.com/sravyachalla/0fa050098fa39b6efe74eccc74f76a17.js"></script>
<ion-content class="list-avatar-page">
<ion-list>
<ion-item *ngFor="let movie of movies">
<span>{{ movie.title }}</span>
<span>{{ movie.actors }}</span>
<span class="item item-text-wrap"
[innerHTML]="sanitizer.bypassSecurityTrustHtml(movie.field_ratings)"></span>
<ion-thumbnail item-start>
<img [src]="movie.field_poster">
</ion-thumbnail>
<ion-note item-end>{{ movie.field_genre }}</ion-note>
</ion-list>
</ion-content>
The sanitizer.bypassSecurityTrustHtml can be skipped as it is used not to strip the html tags
exposed from views.
Step 6 - To add the page in the menu, edit the app.component.ts file from App Folder > src > app.
Import your page at top and add the page in pages variable.
Step 7 - Now check your app in the browser. You will be able to see the movie list page in
menu. You may see the output similar to the below screenshot. :
You can apply the css for the ratings in app.scss file. The css applied here would be applied
globally. If you want to apply styles to specific page, you can create a .scss file in your page
folder.
You may need to import some app components like HTTP, Headers for HTTP calls if facing
any errors.
<script
src="https://gist.github.com/sravyachalla/ceb55c2b40e62e99a194c964dfa9478b.js"></script>
<script
src="https://gist.github.com/sravyachalla/8bb7fb2ef5596033ebffd73b266501d8.js"></script>
public login() {
let data = JSON.stringify({
name: this.registerCredentials.name,
pass: this.registerCredentials.password,
});
let apiloginUrl = 'http://d8ionic/user/login?_format=json';
let headers = new Headers();
headers.append('Content-Type', 'application/json');
The authenticated actions in Drupal like adding content can be done by passing the CSRF
token stored in the local storage into headers.
<script
src="https://gist.github.com/sravyachalla/5e96208a1af6f30ec829aa23dbf356b4.js"></script>
You can also refer Ionic website to check out more details about deploying the app
In this way, you can use the Ionic framework to build the hybrid mobile apps and use the
decoupling feature of Drupal to store the data. Try Ionic and see how it enhance digital
experience. If you have any suggestions or queries please comment down let me try to
answer.
References
http://www.drupalden.co.uk/headless-drupal-resources
https://www.etondigital.com/how-to-develop-a-hybrid-app/
https://www.annertech.com/blog/create-news-reader-app-drupal-ionic-framework