Simple drupal-mode.el
Last updated on
9 February 2023
This is a simple major mode for editing PHP files according to Drupal's coding standard. It extends php-mode and provides basic indentation. However, the (advanced) Drupal mode maintained by arnested is much better.
Save the following code to the file ~/.emacs.d/drupal-mode.el
;;; drupal-mode.el --- major mode for Drupal coding
;;;###autoload
(define-derived-mode drupal-mode php-mode "Drupal"
"Major mode for Drupal coding.\n\n\\{drupal-mode-map}"
(setq c-basic-offset 2)
(setq indent-tabs-mode nil)
(setq fill-column 78)
(setq show-trailing-whitespace t)
(add-hook 'before-save-hook 'delete-trailing-whitespace)
(c-set-offset 'case-label '+)
(c-set-offset 'arglist-close 0)
(c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
(c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values
(run-mode-hooks 'drupal-mode-hook)
)
(provide 'drupal-mode)
Then, in your ~/.emacs setup file associations. This example opens Drupal-specific file extensions with drupal-mode and *.php and *.inc files as php-mode. You can always explicitly switch to Drupal mode with M-x drupal-mode
(add-to-list 'auto-mode-alist '("\\.\\(module\\|test\\|install\\|theme\\)$" . drupal-mode))
(add-to-list 'auto-mode-alist '("\\.\\(php\\|inc\\)$" . php-mode))
(add-to-list 'auto-mode-alist '("\\.info" . conf-windows-mode))
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.