At the latest Rhein-Main region Drupal meetup, and then at the recent DrupalCamp in Essen, I gave presentations about installing and configuring the latest version of the Eclipse IDE (Eclipse Indigo / 3.7.x) for Drupal development, with coverage of commonly-used plug-ins, and identified some features which enhance a Drupal development workflow. This article is related to that presentation and covers some tips for the PDT (PHP Development Toolkit), productivity shortcuts, and configuration requirements for coding in accordance with Drupal standards. The Aptana Studio (as an Eclipse plugin) is not covered in great depth; much of it differs from PDT and PDT-supporting plugins, so it may be covered in a separate post.
Disclaimer: I am not an expert Eclipse power user, nor am I a Drupal development expert, however I have taken the time to look at the latest developments and to experiment, so while this material might be especially helpful for people new to Drupal development who are just starting to use Eclipse and configuring it for the first time, I hope that even experienced Eclipse users may find some useful tips in this article. Before I started my internship for Cocomore, back in October 2010, I think I’d used Eclipse for one course in Java development, but it had been some years, and since then I’d used NetBeans in other courses, but whenever possible I still used my editor of choice, TextMate, which I still find most useful for those times when I just want to make a few simple edits. But there are times when a full-fledged IDE is the right tool for the job and when that’s the case, it helps to have your IDE properly configured and to be proficient in its use. Writing this guide has helped improve my knowledge of Eclipse and I hope reading it will help yours.
This article is long enough I could have broken it into separate pages in a Drupal “book”, but I’ll provide some quick links for those who just want to jump to a particular topic. So the following is a brief outline of this article:
Some Pros and Cons of Eclipse
Installing Eclipse
Eclipse Plugins useful for Drupal Development
Uninstalling Eclipse plugins
Configuration of Eclipse for Drupal Coding Standards
Drupal templates for Eclipse
Creating custom templates
Keyboard shortcuts for a better workflow
We may first want to talk about the pros and cons of Eclipse. Among the benefits of using Eclipse are that it is free and open source, it’s a Java application which has builds for all major platforms (Mac, Linux, Windows), has powerful debugging features, is a full-featured IDE with syntax highlighting and error flagging, and that it has a very active developer community, which means that there are loads of plug-ins to extend the features of the basic Eclipse environment, and there are even Drupal templates available to help streamline your development workflow.
Some of the benefits also include the drawbacks: complex Java applications like Eclipse tend to consume a rather high amount of resources; since it is not a native application, its interface and some of the ways that it works may be a bit less polished than, for instance, a native Mac application. Because of the great number of plug-ins available, and the complexity of the system, even without considering plug-ins, Eclipse has a steep learning curve. Finally, some commercial IDE’s may be better, especially if you already own and use them, in which case there might be little reason to experiment with Eclipse.
This article was written using the latest version of Eclipse, so-called Indigo or version 3.7.x, but I have also used earlier versions and much of the material should apply to other Eclipse versions, both past (and likely) future versions. To best follow along with this article, please download the latest version of Eclipse Classic appropriate for your operating system. There are some bundled versions (Eclipse plus PHP plugins) available, but at least at this time, neither the Eclipse package nor the PHP Developer Tools in recent bundles are very up-to-date, so you’re probably best off just getting all of the most recent stable releases separately.
I’m using Mac OS X, (10.6.x, aka “Snow Leopard”), so chose to download the 64-bit version of Eclipse classic for OS X, which should also be appropriate for Macs already running OS X “Lion” (10.7.x). If you have a suitable system, the 64-bit builds should be your best choice. In any case, installation is simple. You merely download a gzip tar ball, unzip it, and put the resulting directory under applications, or wherever makes most sense on your system. This probably goes without saying, but of course you do need a Java virtual machine (JRE or JDK). If you think you might be doing some Java development, you’ll want to install the JDK, otherwise at least a JRE. Modern versions of Mac OS X already have a suitable Java runtime environment, by default, so if you are on a Mac, you should probably be okay. Please note that Linux users may want to use the Oracle JDK if they plan to use the latest Aptana studio as the OpenJDK is apparently not supported. If you are on Windows or Linux, you may want to look over the installation notes for Eclipse and/or download the Oracle JDK or JRE.
When you first start Eclipse, you’ll be asked to select a location for your workspace. This is where Eclipse stores your projects and preferences. Note that your source code does not need to be in this location, and indeed, if you do put source code in this directory, e.g. a Drupal installation, it will not allow you to create a new project from it. It’s also a good idea to create projects with the directory structure which includes the Drupal root level; that way, core functions will be recognized within your code, and documentation and auto completion hints will show up. When the IDE opens, you’ll see a welcome page. You may want to take a little time to browse the overview or tutorials sections before going to the workbench view.
There are a number of Eclipse plugins which you might find useful for your PHP/Drupal development workflow. Following are some of the plugins you are most likely to use. If you already know how to install plugins into Eclipse, the list below provides the general installation info for several plugins; but I will also provide more detailed information about others. Note: Trying to browse to the links given for “Location” will result in an error; those URLs point to the repository Eclipse uses to install and update plugin software. If an “Info” link is provided, that is the place to read more about the plugin.
The first plug-in you will probably want to install is the PDT (PHP Developer Toolkit). To install it, simply go to the Help menu, and at the bottom, you should see the option to “Install new software…”. If you select from the list of available software sites, you’ll see downloads for Eclipse.org releases for Indigo, and among the available downloads, in the programming languages section, you’ll see the PHP development tools are available for download, however these are not the latest version, at least not at this time of this writing.
Instead copy the linked URL (do not follow this link as it is only used for the repository and does not contain HTML) into the field labeled “work with”, and click on the “Add…” button. The “Add Repository” dialog will prompt you for the a name and location. I just used “PDT” as the name; the “Location” has already been filled in with the URL added in the “Work with” field. I installed the full development tools all-in-one SDK, but at the very least, you’ll probably want the “PHP Development Tools (PDT) SDK Feature”. Select the components you want, and continue to the next step, where it should confirm which packages will be installed. At the next screen, you’ll need to accept the terms of the license agreements before you’ll be able to click on the finish button and complete installing your software.
You should see a progress bar as the installation is completed, and when it’s done there will be a prompt to restart Eclipse. When you are installing a number of packages in a row, it may be just fine to click on the “Not now” button, to continue installing other packages, but since the proper installation of the PDT is critical for some of the next steps, restarting now is probably a good idea.
The Drupal for Eclipse plug-in, available from XTND.US, is a useful extension to the PDT. Note that if you read through this guide and decide that you want to use the Aptana Studio plug-in for Eclipse for your PHP code, you might skip installing most other plugins. It’s possible to use both the Aptana Studio 3 plugin and PDT, but you may encounter some conflicts and things can get more confusing. In any case the XTND.US Drupal Eclipse plugin is only for PDT and it doesn't do much more than associate the PDT-provided PHP Editor to files with the .module
, .test
, .install
, .engine
, and .theme
file extensions so that these files are recognized as being PHP code. In my experience, the repository where this plugin is located is sometimes not available, so you may get an error when attempting to add this plugin.
From the Git perspective, you can create a new repository, clone a repository, or add an existing already-cloned repository. In the illustration, I chose to open a repository I already had in my file system. Since the .git
file won’t show up, assuming you are using Mac or Linux, when browsing the main repository directory, you should check the “Look for nested repositories” checkbox, then click on the “Search” button, and the.git file should show up in the results below, where you can select the checkbox next to it and finally click on the “OK” button.The Git Repository Exploring perspective might be especially useful for those of us who are not already proficient with Git from the command-line, especially if you don’t already have another GUI for Git.
At Cocomore, all of our projects are in Subversion. If you also need to be able to check out and commit to SVN repositories, you will want to add a supporting plugin to Eclipse. I should point out that for Git support and SVN support, all the Eclipse plugins do is add a front-end. You will need to install the appropriate SVN and/or Git client(s) for your operating system, just as you needed Java (a JVM) in order to run Eclipse, itself. There are two plugins which are commonly used for SVN support. The default one, Subversive, which is from Eclipse, is not the more popular of the two and I strongly recommend using Subclipse. At this time, if you look at the Eclipse Marketplace website, you’ll see that the latest version of Subversive is about two years old, whereas Subclipse’s latest release is July of last year — and you will also see that it has about triple the ratings. Subclipse is from Tigris, the developers of Subversion, so this also lends some credibility to their version. In any case, your experience may differ, but I accidentally got Subversive when I first set up Eclipse, soon after I started my internship with Cocomore, and couldn't get things properly configured for our "svnexternals" repository until I replaced it with Subclipse, at which point it was just simple. So assuming you take my advice and use Subclipse, you will need to add the repository using the "Work with…" option when you go to Help > Install New Software…
. And you first might want to go to the official Subclipse website to check which version to use with the actual Subversion client you have installed. I’m using Subversion 1.6, so used the corresponding plug-in. After installing, you’ll find new preferences for SVN and should be able to create new projects by checking them out from SVN, compare diffs of versions, commit new or edited files, etc.
Sometimes you experiment with a plugin, only to find that it doesn’t do what you want it to do or that it conflicts with other plugins you’re more interested in keeping. Luckily uninstalling plugins is simple, but the interface for uninstalling plugins is not as easily found as for installing them. It’s actually the same interface. Simply open the Help > Install New Software…
window and look for the link near the bottom “What is already installed?”. Clicking that opens a new window showing installed plugins. If you select a plugin, you will see the option to uninstall it becomes active. This is useful if you want to optimize your Eclipse installation to trim extra cruft you aren’t using or if you accidentally install a full package when you only wanted one component. Unfortunately, some plugins, such as the Aptana Studio 3 plugin, can only be installed as a whole package, not by sub-components. You can also uninstall plugins to troubleshoot issues. It will be easier to reinstall the package if uninstalling it does not resolve a possible conflict issue since the repository will still be available for easy selection and, even better, any associated preferences are not removed along with the plugin.
The preferences window in Eclipse has many critical panels where you will need to configure settings for proper support of Drupal coding standards. Especially important: you’ll want to make sure that your text file encoding is set to UTF-8 and that the line delimiter is set to “UNIX”. Don’t ask me why these settings are stored under “Workspace”.
General > Editors > Text editors
Perhaps of greatest importance is making sure you use spaces instead of tabs and a tab-width of ‘2’. In the illustration shown above, I also turned on a print margin of 80, since we normally want to keep lines to a maximum of 80 characters long. Getting into the details of Drupal coding standards is outside of the scope of this article, but if you are not familiar with the Drupal coding standards, you’ll want to read them; the Drupal coding standards are well documented. You’ll find similar settings which you need to change in just about every text editor for each language: PHP, CSS, XML, etc; set the tab policy to spaces and the indentation size to two in each.
Other places to fix tabs/spaces/indentation settings:
Preferences > PHP > Code Style > Formatter
Preferences > Web > CSS Files > Editor
Preferences > Web > HTML Files > Editor
Preferences > XML > XML Files > Editor
Note: If you install other language support or add plugins like the Aptana Studio plugin for Eclipse, you will likely have additional editors you need to configure in this way.
Preferences > PHP > Editor > Save Actions
Be sure to set the PHP editor save actions to remove trailing whitespace from all lines.
Another useful thing we can do from the preferences panel is import templates for Drupal hooks. The templates, themselves, can be downloaded from the Eclipse project on Drupal.org which, although listed among modules, is not a module at all, but instead contains an XML file with definitions for templates for each of the Drupal core hooks. Both the Drupal 6 and the Drupal 7 versions extract to the same directory name, “eclipse”, and you can install both of them if you code for both Drupal 6 and Drupal 7. To use them, simply go to the PHP editor templates section in your preferences panel and click on the button labeled “Import” at right. Preferences > PHP > Editor > Templates
Navigate to where you saved your downloaded Drupal Eclipse project, and select the XML file within, and almost instantly you’ll have hundreds of templates for all of the core hooks. To use the new hook templates, all you have to do is start typing the name of a hook, for instance “hook_action
” and you should see an auto-complete helper which displays the signatures for each of the different hooks available starting with whatever you typed in. Replace the selected module_name
part with the short name of your module and hit ? (tab) to continue to the template’s default cursor position. If your module name appears more than once in the hook function, you only need to type it once.
You can also create your own templates. For instance, if you are writing a module which makes heavy use of hooks provided by the Views module, and anticipate writing other such modules, you can create templates for these hooks easily enough, simply by looking up the API, copying the hook function definitions, pasting them in the area for the pattern of your new template, and making a few minor modifications. To see how the patterns are created, it can help to look at an existing template in "Edit mode".
Here, I’ve opened up the template for hook_action_info_alter()
. Note that the "hook_
" part has been replaced with "${module_name}
", a template variable, and all of the dollar signs in the function have been doubled in the template. The ${cursor}
variable is a special placeholder for the cursor insertion point after replacing the ${module_name}
variable. If you click on the button labeled “Insert variable…”, you can experiment and see how other variables (replaced when using the template) can be used. Also pay attention to naming conventions since your name and description will appear in your code hints. Ideally, you should aim to keep non-core API hook code separate from the core API hook code by appropriate naming and descriptions.
The drupalcontrib.org website is a good place to find API documentation for contrib modules. Let’s assume we want to add a hook for a Views API function, like the Drupal 7 version of hook_views_form_submit. All we need to do is click on the “New…” button and copy and paste code and descriptions, then make the appropriate changes, doubling all existing dollar signs, replacing the hook_
with ${module_name}
and put the ${cursor}
in an appropriate position. Of course we might want to add an appropriate comment block above the function part of the template, perhaps indicating that the function implements hook_views_form_submit
and/or that the function is a Views form submit handler.
Preferences > General > Keys
If you hit the same keystroke combination again, i.e. L twice, the preferences panel for keystrokes will pop up. By the way, opening the main preferences window is most easily done with a keyboard shortcut, too: , (i.e. Command + Comma on Mac, or Ctrl-, on Windows and Linux.)
The Keys preferences is where you can get a bit more information, quickly search for the keystroke you are looking for, or assign new keystrokes to commands. By default, the panel opens with all keystrokes sorted by command name. Clicking on the column header labeled “When” or “Category” to see shortcuts sorted by context (editing, navigating, Git, window, …) can make it simpler to see commands related to your current needs. You can also type in a word you are looking for, e.g. in the illustration below, we are looking at all commands related to “move” and can now see that the very handy keystroke for moving selected lines up and down in the document is -↓ / ↑ (alt-↓ / ↑). So holding down the alt/option key while using the up/down arrows can move a block of code around. This can be very handy and it’s worth mentioning a related keystroke: if you add (Ctrl) to this (i.e. -↓ / -↑), the current line (or selected lines) are duplicated above or below, which can be useful for adding a similar line (e.g. initializing an array with key/value pairs). It’s much quicker than selecting the line, copying it, moving up/down, then pasting.
Of course some of the default keystrokes assigned by Eclipse may conflict with keystrokes used by your operating system. If it’s an obscure task you never perform in your OS, of course you can go into the appropriate preferences panel for your operating system and remove the keystroke or reassign it to something more complex (less likely to conflict with any other application-specific keystroke). Or you can reassign the keystrokes (binding) used by Eclipse, quite simply, by typing your desired keystroke combination in the “Binding” field of the Keys preferences panel. And, of course, if there are commands that you commonly use, but which are not assigned to keystrokes, you can find the commands and add a new binding. Very cool!
There are lots of great plugins for Eclipse; this guide only scratches the surface. Do you have any favorite plugins, keyboard shortcuts, or other hot tips for a productive workflow with the Eclipse PDT? We’d love to hear them, so please do share your knowledge in the comments, below.
聯(lián)系客服