Tuesday 23 February 2016

Drupal 7 Folder Structure

Basic Directory Structure of a Drupal 7 Project

By following the standard for the directory structure of a Drupal 7 project, you can keep

your project able to update, stay secure, extensible and understandable for others and

yourself - especially when you get again into the project after a certain time. Additionally the

standard is something which saves you from creating a hardly maintainable legacy code-

base, that is no fun to work with.

Core parts of your project

Do not touch the following folders. Except for Drupal core updates or contribution to the Drupal

core. If you think that there's a bug in the Drupal core, don't edit the code first -check the

issue queue first.

 /includes: Helper functions (e.g. image manipulation, password generation etc.)

 /misc: JavaScripts, icon-images for messages etc.

 /modules: the modules from the Drupal core.

 /profiles: the installation profiles from the Drupal core (minimal, standard, testing). Drupal will ask you

which profile you want to install when first installing your Drupal site.

 /scripts: contains various scripts. e.g. to execute the cron, dump the database, generate a password

hash and run the tests.

 /themes: here are the Drupal core themes located. Such as the default Bartik themeand the Seven

theme which is the default one for administration pages.

 /sites: Here comes anything which is not part of the Drupal core. Contains an "all"- and a "default"-folder.

In the "all"-folder you can place your custom and contrib modules and themes. Additionally here's a

"default"-folder where your site configuration resides. After a Drupal site is installed, there will be a

settings.php file in the "default"-folder, where usually the database configuration and other site-specific

configurations are. You're free to place other configuration files in the default folder. The reason for the

naming "all" and "default" is the Drupal multi-site setup.

 [Optional] /sites/my-website-a.com and /sites/my-website-b.com etc.: Drupal is able for multi-site

configuration. If you point your webserver into the Drupal root directory, Drupal can manage to handle

separate modules, themes and configuration by sharing one Drupal core and specific modules or themes

between an number of Drupal websites. You can even share specific database tables (such as the user

table) or 1 database between multple Drupal sites.

 /sites/all: Any modules and themes in this folder will be available to all instances managed by this code

base. So if you are running multisite, the modules and themes will be available to each of the multi sites.

If you have only 1 website instance managed by Drupal, you can place them in this folder.

 /sites/all/modules: Here you can put all your contrib and custom modules. Please notice: don't edit

contrib modules directly on bugs or any other issues. Firstly take a look into the issue queue for the

specific project, create patch or fork the project/module/theme. If you decide to fork a module or theme,

consider to release your fork on Drupal.org by a full project application.

 /sites/all/themes: That place is ment to hold your custom or contrib themes. You cancreate your own

themes or a sub-theme like for the Zen theme. Please notice to keep any PHP logic as most as possible

separate from the theme. Database queries, PHP classes and similiar are better located in a Drupal

module.

 [Optional] /sites/all/modules/custom: Often Drupal users place here their custom modules. The modules

which are not released officially on Drupal.org. Be careful with this folder. Sometimes other projects don't

recognize this sub-folder. But mostly they do (like Drush and the Drupal module management system).

You're most safe, if you even place your custom modules in the /sites/all/modules folder. Then you can

prefix your module names, to easier seperate them from the contrib modules. For example:

"my_client_fancy_field".

 [Optional] /sites/all/modules/contrib: Often Drupal users separate in this sub-folder the contrib-modules.

That means modules which are officially released as a full project on Drupal.org. Also be carefully with

this subfolder. Sometimes other projects do not recognize this sub-folder. Your safer with just using the

/sites/all/modules folder. But don't rename the contrib module folders. That would break their functionality.

You can do this with your custom modules.

 [Optional] /sites/all/modules/features: If you manage your Drupal 7 website configuration by the

features module, it could be a good way to seperate the modules, which will be created by the features

module, in this directory. Learn more aboutconfiguration management in Drupal 7. Please notice that in

Drupal 8 the configuration can be managed by the Drupal core.

 /.htaccess: The directory-level configuration file for your project. It contains default configuration such as

for readable URLs. You can edit this file if you have special requirements.

 /.gitignore: In this file you can specify the files and folders which should be ignored by the Git version

control system. Compared with other OpenSource version control systems, Git has one of the most

advantages for development with others via the internet.

 PHP-constant DRUPAL_ROOT: This constant gets the absolute path to your Drupal root directory (such

as /var/www/my-drupal-site).

 conf_path()-function: Gets the path to the Drupal configuration.

 drupal_get_path()-function: Gets the absolute path to a module or theme.

 path_to_theme()-function: It can point to the active theme or the module handling a themed

implementation.

 drupal_realpath('public://'): Gets the path to your public files folder (user uploads etc.).

 file_create_url(): Use this to get a URL for a file - this will ensure that the URL is correct, even when using

language path prefixes.

 image_style_url(): Use this to get the path to a derived image (using Drupal's image styles)