Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The main method for extending Drupal's functionality is through modules. That is, downloadable packages of code that add specific abilities to your site. But some tricks don't require an entire module. Small passages of PHP code called Snippets by the Drupal community will do the job just fine. We'll show you where to find such snippets, how to implement them on your own site and some traps to watch out for. The place to go for snippets is, as with everything else in Drupal, drupal.org. Once you're on the Drupal site, go to Documentation, scroll down and go to Beyond the basics.
Beyond the basics is of the book type, so you see you can page back and forth through it. We're going to go directly to the chapter on Snippets. There are two main types or snippets, the first one is for themes and this is the biggest area. If you click on it, you see numerous tricks that you can perform on your own site. You could of course click on any of these that look interesting to you or page through them as this is a book. Let's scroll to the bottom and then go back up, so we see all of the snippets. The other kind of snippets is for content- that is, to change small bits of your site as we did on our site for our endorsements block.
In addition, there are SQL snippets, which let you change things in the database. There are so many of these and they're implemented through a different method. You can learn how to implement SQL snippets by clicking on this link, How to use an SQL snippet. But we're only going to focus on the PHP ones. Finally, some snippets happen outside of the Drupal interface. That is, it's PHP which is placed on your server for one reason or another. These are also contained in the PHP snippets chapter. If you click on it and scroll down, you'll see an example of this PHP snippet that happens on the server, this Remote posting and administration.
We're going to show you how to implement one of these PHP snippets. We'll go back up and enter Theme snippets. The one that we're going to implement is called Add an "Edit" Tab to Every Block, we scroll down to find it and there it is. Click on it, when you get to this page, read the instructions of how to implement it. In this case, it gives us a link to an earlier post, which we'll want to read. But let's pretend that we don't at this point. We look through and see here's the code that you'd include in your theme's block.tpl.php file and now we know the file we're going to affect.
Before we do anything else, we're going to find that file and make a backup of it, in case anything goes wrong with this snippet. We go back to our finder and hide everything else and then go to our Drupal installation folder. This will be the folder where you first put Drupal when you started out. We're using a theme that came installed with Drupal. So, our theme will be in the Themes folder. If you're using a theme that wasn't installed with Drupal, yours will be in the Sites folder either in All or the Default folder. But for us, we're going into Themes and then into bluemarine, which is our theme.
There's the block.tpl.php file. We'll highlight it, copy it either by hitting Command+C on the Mac or Ctrl+C on a Windows machine and just paste it somewhere else. I'm going to put it on the desktop. And then I'm just going to add a little bit of text here, so it's clear that this is the backup, call it BACKUP. There, now we're ready to work on this file. We open it up in a Text Editor. If this is the first time you've opened this file, you may get a warning because you downloaded this file from the internet. Because we know where it came from and we trust it, we're going to say Open. So, now we've the file open that we want to affect. We also have a backup made of that file for safekeeping.
Now, we go back to that snippet. We'll copy the text by hitting Command+C on the Mac or Ctrl+C on a Windows machine, go back to our text file and then click in it and since it didn't mention where we've to place it, I'm just going to put it anywhere, I'll put it at the end of the file by hitting Command+V on a Mac or Ctrl+V on a Windows machine and save it and close it. Let's see how that affected our site. We go back, we reload the page and now we see a little Edit box here, right underneath the endorsements. We also see what would be edited. Wait, I thought that would add the edit capability to all of my blocks. Let's go back and take a look. Aha! Maybe there are some tips in here.
Indeed, if we go back to the earlier conversation, we see that it only affects custom blocks. This just highlights why you've to read all the documentation when you use a snippet. But in this case, it looks like the snippet did exactly what we wanted. Let's try it out. Click on Edit and there we're, we can edit that particular block. I've decided I didn't like the change to my site, so I'm going to go back to the earlier version where we weren't able to edit those endorsements. I go to the finder, hide everything, throw away that block.tpl.php that we just changed. On the Mac you do that by dragging to the trash, on the PC by right clicking on the mouse and selecting Delete.
Now, I put that backup back into that folder. I drag it in and change the name back to what it was before, delete the extra PHP and Backup and let's go back to our site to make sure that took. I reload the site and indeed the Edit box disappeared. I've to give you a few caveats though when you use snippets. First of all, there is no central testing authority for testing snippets, where as the Drupal core project itself goes through a lot of quality control, there really isn't any for snippets except for those people who have used it and then commented on it. That leads us to the second part, read the documentation very well and be sure you understand it and read all of the comments.
Very often, somebody will submit a snippet and then the comment will say, you know there's a bug in it, I discovered it this way and then somebody will contribute a patch to it, so it works correctly. So, do read all of the comments. Next, make sure that you understand the code as best as you can. I'm not a programmer myself, but if you look through it enough times, you can start to see the logic of what's happening. Finally, always backup the effected files first. I can't stress that enough. If something goes wrong with the snippet, you want to be able to rollback to your situation before you applied it. I love Drupal Snippets, of course I like that they improve my sites, but just as importantly, they help me learn PHP and my SQL. I've come to understand how Drupal works from the inside out because I can see how programmers use some of its internal variables.
And I feel empowered by seeing how one can just reach into the code and make big changes with very small tweaks.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.