Pages

Saturday, February 22, 2014

Colorbox Pop-ups with Drupal Images and Videos

A lot of sites like to show media inside pop-ups. Using pop-ups can make it easier for visitors to quicky browse through multiple images or videos.
In this tutorial, we're going to show you how to use the Colorbox pop-up with Drupal.
We'll see how to display both image and video fields inside a pop-up window.

Installation

Install these two modules:
Now we need to get the actual Colorbox files:
  • Download the Colorbox plugin from https://github.com/jackmoore/colorbox/archive/1.x.zip
  • Unpack those files and rename the folder to "colorbox"
  • Upload those files to your "sites/all/libraries" directory. Make sure the path to the plugin file becomes: "sites/all/libraries/colorbox/jquery.colorbox-min.js"
Now configure Colorbox:
  • Go to Configuration > Colorbox
  • Check the box "enable colorbox load"

Colorbox with Image Fields

  • Go to Structure > Content types > Manage fields
  • Add an image field
  • Click the Manage Display tab
  • Set the format the image to Colorbox.
media_1390491220707.png
  • Click Save.
  • You'll now see some formatting options for the colorbox on this field:
media_1390491203540.png
  • Click the small cog icon and those images will show:
media_1390491243285.png
  • Go and create a content item using your new image field. In this example, I've uploaded 4 beach photos:
media_1390492110401.png
  • Click any image and you'll get the videos showing inside a colorbox. If this doesn't happen the first time for you, clear Drupal's cache.
media_1390492183128.png

Colorbox with Video Fields

Now we'll show you to display video inside a pop-up. We're going to use the Video Embed Field.
  • Install the Video Embed Field module.
  • Go to Structure > Content types > Manage fields
  • Add a Video Embed field
  • Click Manage Display
  • Set the Format to Thumbnail Preview w/Colorbox:
media_1390494073507.png
  • Go and create a content item using your new video field:
media_1390493566607.png
  • Drupal will automatically create a thumbnail for you from the video:
media_1390493598392.png
  • Click the thumbnail and you'll get the video inside a pop-up:
media_1390493616726.png

Should I Re-use Existing Drupal Fields?

Drupal gives you the ability to re-use fields. If you have an "Image" field, you could choose to use that same field on every content type on your site.
However, it's not always clear whether re-using fields is a good idea. Sometimes it is, sometimes it isn't.
Here's an overview of the advantages and disadvantages to consider before re-using Drupal fields.

General advice on re-using fields

You can choose the "Add existing field" feature whenever you go to Structure > Content types > Manage fields:
media_1392150781397.png
The Drupal.org documentation does not recommend that you re-use fields:
"It is recommended to create new fields, rather than reusing existing ones, unless you have a clear reason to do so".
That's also the general consensus amongst Drupal professionals that I talk with. There's nothing inherently wrong with re-using fields, and it can be a useful approach in some situations, but it can also cause more problems than it solves.

Advantage: simplicity and time-saving

The Drupal Field UI documentation says this:
"reusing fields can save you time over defining new fields."
That's true, but the time-savings are very small. A more compelling advantage is that re-using fields can sometimes make site administration simpler. Web Initiative sum this up nicely:
"Reuse of fields can also reduce the system’s complexity. Instead of creating and maintaining 10 different fields, Drupal admins maintain only two fields and their documentation. Database administrators only need to improve performance of two extra tables. KISS is always a good principle."
It definitely would be easier to apply permissions, setting and design elements to one re-used field rather than 10 unique fields.

Advantage: some content works well with re-used fields

Back to the Drupal Field UI documentation again:
"reusing fields also allows you to display, filter, group, and sort content together by field across content types. For example, the contributed Views module allows you to create lists and tables of content. So if you use the same field on multiple content types, you can create a View containing all of those content types together displaying that field, sorted by that field, and/or filtered by that field."
One comment writer on the Drupal.org documentation makes the same point about Views. They point out that Views can combe content in sophisticated ways. So, if you have multiple different content types, with different date fields, then Views can combine them into a single view. However, they also point out that Views isn't so sophisticated with sorting. So, if you have multiple different content types, with different date fields, then Views will struggle to sort the content on all those different date fields..

Disadvantage: Re-used fields are inflexible

Brandon Williams on Twitter summed this up nicely:
"at first it's a good idea, but give it a few weeks, reqs change, you end up creating separate ones anyway"
To a large degree, if you choose re-used fields, you are limiting the changes you can easily make to your data later.

Disadvantage: Re-used fields make data harder to export or migrate

Re-using fields could become an issue when you need to export your data or when you need to migrate to a new version of Drupal or another platform.
Each Drupal field has it's own database table, as shown below. Extracting that data can be tough. The Features module (the most common way to export Drupal data) struggled for a long time with shared fields, although current versions can handle them more effectively.
media_1392150946256.png
Whenever you start to build dependencies between codebases or database tables, you add complexity to your site.

Advantage or disadvantage? Performance

The Drupal documentation outlines one possible benefit of re-using fields:
"From a performance point of view, you don't want to have a gazillion fields on your Drupal site. If a content type (or other entity) has a lot of fields, saving the content will result in a lot of database writes that might take time. Also, the field configuration itself might take a lot of memory to load from the cache (and that is done pretty often)."
This thread on Stack Overflow has a very relevant discusion on performance. It includes this comment:
"A real problem however is the number of fields you have. Because currently in Drupal 7, the complete field configuration of all fields, no matter if they're loaded or not, is fetched from the cache on every single request. I've seen sites with 250+ fields, where loading and unserializing the field configuration takes 13MB+ memory." (that has been improved with Drupal 7.22)
So, re-using fields could possibly give small performance improvements by letting us have a lower total number of fields.
However, those small improvements may lost elsewhere. This from Web Initiative again:
[fields] extra complexity to a Drupal system. When creating a new field, the field’s definition is added to the field class table and the field’s configuration is added to the field instance table; meanwhile, a new table is added to the Drupal database to store the field data. Database tables add complexity to the system. In addition, queries of nodes will incur JOINexpressions of tables to field data. Multiple JOINs will impact database performance since MySQL responds poorly to queries with multiple JOINs of tables if not properly configured."

Thursday, February 20, 2014

10 Ways to Block Spam Registrations in Drupal

If you're going to use popular software like Drupal, one of the downsides is that you'll need to deal with spam.
A lot of members report that they receive many spam user registrations on their sites.In this blog post, we'll look at 10 of the best ways to avoid getting overwhelmed by spam users.

#1. Core approval features

Drupal has a default feature that allows you to either completely block or moderate all user registrations.
Go to Configuration > Account settings. You can set "Who can register accounts" to a couple of useful settings:
  • Administrators only: this will block all registrations.
  • Visitors, but administrator approval is required: This will require you to manually approve all users.
media_1392315048536.png

#2. E-mail verification

Another Drupal core feature allows you to require e-mail verification before a user account becomes active. This add a significant hurdle for spammers. Go to Configuration > Account settings to enable this feature.
media_1392315191694.png
https://drupal.org/project/user_verify add some more sophisticated options to the email verification process, including the requirement for the user to enter a special token.

#3. Block certain user details

If you have a lot of spam registrations, there's a good chance there will be some patterns in the spam user details. For example, you might have a lot of users signing up as "John Smith" or using .ru email addresses.

https://drupal.org/project/user_restrictions allows you to block both emails and usernames based on certain patterns. Here's an example which blocks .ru domains:
media_1392319168945.png

#4. Captchas

A Captcha presents a visual challenge that is supposed to be difficult for spammers to solve.https://drupal.org/project/captcha provides easy integration with your Drupal registration forms.
media_1392319245875.png
However, Captchas have several problems. They can often be hard for even normal users to see. They are also not good for users with visual disabilities.
https://drupal.org/project/riddler is an interesting variation on a captchas. It allows you to ask a question that will probably stump spambots:
media_1392319431807.png

#5. Honeypots / Secret form fields

One spam-defeating technique that we've found to be very effective is hidden fields. You add an extra input field to every form and then hide it with CSS. Humans never see the field but spambots do and when they fill in the field the form is discarded.
https://drupal.org/project/spamicide is a module that makes it easy to create hidden fields.
This technique is often called a Honeypot and there's a module with the same name:https://drupal.org/project/honeypotClick here to read the modules's author explaining the meaning behind the name "Honeypot".
There are some funny variations on this idea. For example, https://drupal.org/project/simpleantispam add a visible checkbox marked "I'm not a spammer" and a hidden checkbox marked "I'm a spammer":
media_1392321166535.png

#6. Block by location

http://drupal.org/project/geoblocker allows you to black-list or to white-list access to a Drupal site by countries.
It's not the most sophisticated technique because determined spammers will find a way around these restrictions, but it will block a lot of low-level spam attempts.
Certainly it's worth considering if your site is specifically focused on one location and you have little to no interest in overseas users.

#7. Secret codes

If you have a site without a large audience, you could consider giving out a secret code to potential members.
https://drupal.org/project/mothermayi allows you set a secret code that people must enter in order to register successfully.

#8. 3rd party spam tools

There a wide variety of 3rd party systems that try to prevent spam registrations. These are often paid services. Here are some of the most popular:

#9. Delayed roles

https://drupal.org/project/role_delay is an interesting approach. It allows you to slowly give users more permissions over time.
For example, a brand new user might not be able to post comments or forum posts. Over time they can automatically be moved into Drupal user roles with more permissions.

#10. Warning message

https://drupal.org/project/warning made me smile. Instead of providing a sophisticated technical solution, the Warning module simple tells your users that won't tolerate spam. Here's how the module create describes it:
"I wrote this module because my personal blog was listed in do-follow lists and generated up to 30 spam comments per day that were not filtered by Mollom. After adding a simple (default) warning for comment submission about the fact that I do not tolerate unrelated links and the comments are moderated, this number dropped to 3 spam comments on average."

Tuesday, September 24, 2013

Drupal Commerce - Overview

This episode goes over a basic overview of Drupal Commerce and how it can be used to create a Drupal 7 based ecommerce website.
In this episode you will learn:
  • What is Drupal Commerce
  • How Drupal Commerce can be used to build flexible and dynamic ecommerce websites
  • Common Drupal Commerce terminology
DDoD Video: