Drupal 7 Tips

From Wowww mediawiki

Jump to: navigation, search

This page serves as a location to place smaller Drupal 7 tips and tricks, to prevent the creation of separate pages for smaller subjects. See also Drupal useful links

Note that some of these techniques may also apply on other versions of Drupal, but are specifically written for Drupal 7.

Contents

Code

The module Devel gives Drupal developers a whole set of useful development tools. One of these tools is Krumo, a clear, visual way of presenting arrays.

  • Devel krumos can be called using the functions like dpm() and kpr(). Pass the variable you want to print as the first parameter.
  • Note that the Krumo website is currently marked as unsafe by Google.
  • Drupal 7 (core) also introduces the function debug().
  • More information about debugging in D7 can be found at Drupal.org (Devel), Backpack and Engineered Web.

Database

Drupal 7 comes with a new database API. See the link below for creating dynamic database queries.
Dynamic Database queries (drupal.org)

Fields

Unlike with earlier versions of Drupal, in Drupal 7 developers are discouraged to access values directly in objects.

Default user image

Account settings allows to define a path to an optional default user image. If you put in a relative or external path to the image, the image will not be treated like a normal user image. Instead, you will only be able to use the 'default' image style and can only display the image in the size it was uploaded.

To avoid this, you have to format your path as a URI to the public files location of the Drupal installation (this is often [sites/default/files/]). The link could look like this:

public://user_pictures/default.png

Where public:// is the link to the public files location and everything behind it, forms the path to the image (within public files). Once you've done this, the default image is treated like a user image.

Formatting and text

Drupal has some built-in functions to handle different kind of strings. Like normal text, links and dates.

Formatting dates

Formatting strings

Formatting links

print l(t('link text'), 'link URI', array('attributes' => array('class' => array('class 1', 'class 2'), 'title' => t('link title'))));

Multilingual

Drupal has the ability to use multiple languages for both the users and administrators of a website. For this, the system uses gettext. It is possible to translate strings inside your own project or use and contribute community translations. This can be done through web interfaces or by editing the language (.po) file of a selected project.

In templates and other PHP-files, you can surround a string with t('<insert string here>') to make it translatable.

  • Drupal.org states that the first parameter of t() has to be an English string.
  • More information about t() can be found at Drupal.org.
  • Text strings added through UIs like with Views, have to be written in English to be translatable.

Translating inside Drupal

Strings can be directly translated through the Drupal interface itself.

  • This requires the core module Locale to be activated.
  • Language strings can be found on [admin/config/regional/translate/translate].
  • These changes only apply to your current website.
  • Translations can only be added for installed languages.
  • Languages can be added and removed on [admin/config/regional/language].

Optionally, a language can be assigned to a piece of content by its author.

  • This requires the core module Content translation to be activated.
  • Only installed languages can be assigned to content.

Community translations

If you wish to contribute translations to the community, you can also translate through localize.drupal.org

  • Here you can join a language translators team.
  • You can import and export sets of translatable strings, or provide translations directly through the web interface.
  • Translation suggestions have to be approved by language team admins, take into account that it might take a while before community translations are made public.

You can automate add translations for modules by using the module Localization update.

  • When activated, this module automatically downloads existing translations for modules on installation.
  • It provides an interface which displays downloadable updates for translations.
  • The translation update process is similar to that of updating modules.

Views

Contextual filters

Contextual filters are a part of the Views UI and are similar to arguments in earlier versions of Drupal Views. They are on top of the Advanced section in Views, this is the right column. Note that this column might be collapsed initially, but can be expanded by clicking its header.

Contextual filters are applied to arguments given inside URLs, in PHP these arguments would be like $_GET-variables. Adding these filters is similar to adding normal filters: click add and select one or more subjects to use as filters. With each filter can be configured what to do when a value is in the URL of the view page and what to do when it is not. This way default arguments can be provided, page titles overridden and validation criteria added.

It is important to reorder the contextual filters so they are listed in the same order as they appear in the view's URL. It may be necessary to add placeholders for the filters in the path section of the view display. This section can be found on top of the middle column in the view display form.

Contextual filters can also be applied on displays which do not have a path of its own. When calling a function like views_get_view_result($name, $display_id), every extra parameter added will be treated as arguments for contextual filters. Another option is to add a default value for missing arguments through a filter's option window. One can select from multiple options, including specific values from a page's URL.

Glossary and indices

Views has a glossary option, which is built into contextual filters. Glossary indices are often created as attachments to the view they apply to, containing the same filters and sort criteria as that/those view(s).

Currently, there is no glossary creation example on the wiki. However, since Drupal (7) is shipped with a (disabled) glossary view, it is possible to quickly create one.

References

Using the module References (part of CCK prior to Drupal 7) or Entity reference, references to nodes, taxonomy terms and users can be created. These references are made through fields. On default, there are some basic options available relevant for the type that is referred to. In certain cases, one needs more control over the items that can be referenced to.

For advanced selection lists, you can use the special display type for Views called references. This display allows the user to select and filter items, which can be placed inside a select form tag. It is possible to pass through arguments for contextual filters through the reference field edit window. It is also possible to pull default values from a page's URL and use these to filter a reference list.

To use a references display in a reference field, add it through the field settings section. If usable references are found, they are displayed in a selection menu. The results of the selected reference view display will populate the reference field.

Personal tools