[Social API] Creating a Social Auth implementer #2 - Routes and Network Plugin

Submitted by gvso on Tue, 07/19/2016 - 16:31

In the last post of this series, we started declaring our module information (social_auth_google.info.yml) and its dependencies through a composer.json file. In this post, we will move on and start working on the routing, settings form, and Settings Manager.

Routes

Now that we have let Drupal about our module and its dependencies, it is time to define our routes. In the case of our Social Auth Google module, we'll need the following social_auth_google.routing.yml file

First, we define the route for our settings form. Then, we create a route for our method that redirects to Google Accounts for authentication. Last, we add another route which will be used as a callback for Google after the user successfully log in into his account and grant permissions.

For Google Login, the site builder needs to create a project in https://console.developers.google.com/ and provides some data defined in this routing file. More information can be found in this documentation page.

Settings Form

We need to declare our settings form, so site builders can add their Google project information. For this module, they should specify their Client ID and Client Secret. This is an example of how the form file looks like.

This settings form definition may change in coming weeks in order to provide more settings options. However, it won't be so different. You can refer to this issue to get updates

Settings manager

Now that we have defined the settings form which stores the data the site builder provides, we should be able to access that data. 

To achieve this, we should create settings file in src/Settings. In the case of Google, we need to get the Client ID and Client Secret, so we should retrieve that information from the Drupal Configuration Manager:

With this, we are able to get the Client ID (client_id) and Client Secret (client_secret) from social_auth_google.settings

But how is this class able to know it needs to get those keys from social_auth_google.settings? You have to specify it in a Network Plugin definition!

We will be looking more in detail about Network Plugins in the following post.