Tag Archives: Drupal

Drupal and Amazon SES

Drupal and Amazon SES SMTP server

Hello everyone!

Today I would like to write a bit on configuring SMTP Amazon SES with Drupal. We set up our Drupal installation on Amazon EC2 server. We wanted to change default mail() method in Drupal to SMTP server. As we already used Amazon we wanted to try SES. Here is the short info how we manage to run and configure it.

Setup Amazon SES account for Drupal

I assume that You already have Amazon account for purpose of this tutorial. If You don't have one, you need to sign up in order to use Amazon services first.

After You log in go and visit this page. This is simple dashboard that shows the statistics for Your SMTP server. SES has some limits and You can check them on this page as well.

Drupal - Amazon SES console

Let's set up SMTP account for Drupal. On the left side of Dashboard page you should see sidebar menu. Go to SMTP Settings. Under this page You will have access to data that are required for SMTP access (Server name, port)

Drupal - Amazon SES settings for SMTP

The only thing that we don't have are SMTP credentials - username and password. Click on "Create My SMTP Credentials button". It will take us to IAM page where You can create new user account. You can leave the default name or provide Your own. Click create button. After that You can download the credentials. But keep in mind that there are not the credentials that You'll be using in Drupal!

Drupal - Amazon SES - create user

After new user is created we need to setup his credentials for Drupal purposes. Go to IAM user page and click on Your created user.

Under Security credentials there are Sign-In credentials. Click on Manage Password. You can choose between password generated by IAM or Your own. Pick one of the option and remember the password. That's the one You'll be using in Drupal.

Drupal - Amazon SES - manage password

 

Setup From: account on SES for Drupal

We already have the user credentials for connecting with SMTP server in Drupal. There is one more thing we need to care about. In order to be able to set From: field we need to authorize the email. Go to SES console or click this link. There are two ways of verifying sender. You can authorize specific emails or whole domain.

If You want to authorize specific email (we will use this option) click Verify a new Email Address. Type Your email and click Verify this Email Address. You will receive email with activation link. After activation You should see the status verified.

Drupal - Amazon SES - verify sender email

If You want to authenticate whole domain You need to setup DKIM. You can read more about this process here.

 

Setup Your Drupal installation for Amazon SES

Now this is the final step. We need to configure Drupal for Amazon SES. First, we need a module that can change Drupal's native mail() function to SMTP protocol. In our example we will use SMTP Authentication Support module. It's really nice, easy to implement module for SMTP also with support for Drupal 8. Download and install this module. Go to configuration page (Menu Toolbar -> Configuration -> System -> SMTP Authentication Support).

What fields You need to set:

  • Turn this module on or off - set to ON
  • SMTP server - get the information from SMTP settings page on Amazon
  • SMTP port - 25, 465 or 587 (also listed on SMTP settings page)
  • Use encrypted protocol - Use TLS
  • Username - created username on Amazon SES
  • Password - password for this user (not authentication keys!)
  • E-mail from address - email that You verified

Drupal - SMTP settings for Amazon SES

Rest of the fields are optional. Really useful fields are:

  • E-mail address to send a test e-mail to - You can test if SMTP is working
  • Enable debugging - use it only for test, disable on production enviroment. If the setting doesn't work You will have a debugging messages with information what was wrong.

 

 

Basically that's it. After that You can start sending emails from Drupal using Amazon SES, using forms etc. This module will overwrite default Drupal functionality so You don't have to bother to change anything else. No need to change Your old code for sending emails etc.

Render block programmatically using Drupal 7

Render block programmatically using Drupal 7 - tutorialHi, folks:)

Render block programmatically - popular topic from google. Today I will tell you a little bit about Drupal Views and how to do that from code. Sometimes we need to render block in our template. For example you created block using Views. You want to place it in the template. In Drupal 7 there are couple of ways to solve this problem. I will show you the 4 methods that will help you render block programmatically. All was checked in Drupal 7.

How to render block programmatically?

Method 1

Let's assume, that you know your view name and block id. If you don't know you can learn how to check that here. In our case view name is sidebar and display id is article_ad. Simplest usage here:

print views_embed_view('sidebar', 'article_ad');

This code will return html markup for our block. Function views_embed_view can take few parameters.

  • name - view name
  • display_id - display id of view we want to render, by default it's 'default'
  • arguments - if you have any parameters you want to pass to block you want to render from code, like contextual filters, you can pass it there via arguments.

Note, that this function does not render block title. If you look to the code of this function you will find out that it's using views_get_view function. It provides more functionality, we will be talking about this approach later (method 4).

Method 2

In second method we will render block programmatically by module_invoke function. Let's look at the example below:

$block = module_invoke('views', 'block_view', 'sidebar-article_ad');
print render($block);

There are 3 parameters, but you can specify more. Just pass more parameters like in previous example to module_invoke function. First two parameters are constant for getting blocks. You have to specify 'views' as a module and 'block_view' as a hook. Next parameter is combination of name and display_id of our block. If you are not sure of the combination, or combination you have provided doesn't work, check it in your database.

In Drupal database there is table called block. In delta column you will find all blocks, that you can use. You can find your block executing query like this

SELECT * FROM block WHERE delta LIKE '%article_ad%'

Find your block in the query results and pass it as 3rd parameter to module_invoke.

Method 3

In 3rd method we will render block programmatically by block_load function. Take a look at example below.

As You can see, there's 'sidebar-article_ad' from previous method. Rest is just copy and paste code.

$block = block_load('views', 'sidebar-article_ad');
print render(_block_get_renderable_array(_block_render_blocks(array($block))));

OK, so here is the method I don't really like. It's hard to pass custom contextual filter value. By default it will take arguments from $_GET. If you know how to do that please write it in comments below:) Any solution will be appreciated:)

So there are two parameters, 'views' and combination of view name and display_id from previous examples.

Method 4

OK, so here is probably most advanced example if you want to render block programmatically. Here you can customize almost every View aspect.

$view = views_get_view('sidebar');
print $view->preview('article_ad');

We are using views_get_view function which returns view object. Take a look at the documentation here:

https://api.drupal.org/api/views/includes!view.inc/class/view/7

You will find there many useful methods, for customization of output, filters and basically everything:P

Getting data from block

OK, now we know how to get html code of your block. But hmmm, what if I need the raw data from my block after passing all contextual filters etc? I don't need html, I will not parse this code via regex or something... I'm not that stupid:P

Not a problem, just use views_get_view_result function. Example usage:

$block_data = views_get_view_result('sidebar', 'article_ad');
print_r($block_data);

It will return you all the data connected with your block. Function takes 2 parameters view name and display_id(block id). After that you can specify additional parameters that will be passed as arguments, like in previous examples.

Drupal 7 Ajax and Views

Drupal Views AjaxHello everybody!

Today I want to share some technique that we are using in our Drupal projects a lot. Sometimes we need button like load more, which will send custom ajax. Default drupal mechanism for ajax is not perfect. What if you will want to modify the result? So I will show you simple example of how to use custom Drupal views ajax. We are using Drupal 7 with Views module.

Using drupal views ajax

Let's look at the snippet below. This basic piece of code will help you with drupal views ajax.

$.ajax({
    url: Drupal.settings.basePath + 'views/ajax',
    type: 'post',
    data: {
        view_name: 'view_name',
        view_display_id: 'view_display_id'
    },
    dataType: 'json',
    success: function (response) {
        if (response[1] !== undefined) {
            var viewHtml = response[1].data;
            //...
            Drupal.attachBehaviors();
        }
    }
});

First of all, it's based on jQuery. If you want to use it in different framework, you can use similar parameters, it should work without any problems:)

OK, so let's analyze this snippet. There are few important lines of code:


First is the URL. You can leave it as it is. Drupal.settings.basePath returns base path to your website like http://mywebsite.com/. Than we are using default drupal views ajax mechanism (views/ajax). You can send it as GET or POST request. It is up to you which one you want to use.

data: {
        view_name: 'view_name',
        view_display_id: 'view_display_id'
    },

This is important part. In order to get data from our view, you have to know it's name and display_id. This informations are available in admin panel (look at the screenshot below)

Click Structure -> Views -> {Name of your view} -> Edit

Drupal Views screenshot

 

In our case we will use Question List view. After that pick the page/block you want to get via ajax.

Drupal Views page screenshot

In our case we are using DMP Q&A page. Our machine name of this page is page_4. If it's the only page click on it again and notice the URL change. In our case URL looks like this:

URL for Drupal Views Ajax

 

 

The highlighted parts are the informations we need.

  • questions_list is our view_name
  • page_4 is our view_display_id

This are two most important parameters. You can use more parameters, but it depends on the view and what you want to do with it. For example if you want to load more result you can use per_page parameter and page with stands for numbers of item displayed per page and number of page (first page is 0, second is 1). But remember, if you want to use pagination like that you have to turn it on in view in pager section. If it'll be turned off it won't work. You can use filters values etc. Everything depends on you view configuration.


In success function we handle response from server.

First we have to check if there's any result. Drupal will send you response of 2 elements array. Stuff that we need is in [1] element. If there is no [1] element, it means that probably you are passing wrong data. Check it again! In viewHtml we have html data from our view. You can append it, modify it, play as you like.

It's good to use

Drupal.attachBehaviors();

after changing your html code. It will attach certain javascript behaviors  to the new ajaxed content.

Practical example of Drupal Views Ajax

Practical example of drupal ajax views for fetching second page of questions here (using Questions List and DMP Q&A):

$.ajax({
    url: Drupal.settings.basePath + 'views/ajax',
    type: 'post',
    data: {
        view_name: 'questions_list',
        view_display_id: 'page_4',
        per_page: 12,
        page: 1
    },
    dataType: 'json',
    success: function (response) {
        if (response[1] !== undefined) {
            var viewHtml = response[1].data;
            $('#question-container').append(viewHtml);
            Drupal.attachBehaviors();
        }
    }
});

 

Any questions? Or maybe you have better solution?