Skip to content

Commit 0cb4bca

Browse files
committed
Added ID suggestions for hook_update_N().
When inserting a hook_update_N() with `drupal-insert-hook` an we will suggest an ID the hook implementation based on previous update hooks and Drupal and module major versions.
1 parent ce9d3a5 commit 0cb4bca

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

drupal-mode.el

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ buffer."
527527
nil nil "hook_"))
528528
'(setq str v1)
529529
'(setq v2 (let ((hook v1)
530-
case-fold-search form-id form-id-placeholder)
530+
case-fold-search form-id form-id-placeholder upadte-id update-id-placeholder update-next-id)
531531
(if (string-match "\\([A-Z][A-Z_]*[A-Z]\\)" hook)
532532
(progn
533533
(setq form-id-placeholder (match-string 1 hook))
@@ -536,7 +536,13 @@ buffer."
536536
nil 'drupal-form-id-history form-id-placeholder))
537537
(setq str (concat hook "() for " form-id))
538538
(replace-regexp-in-string (regexp-quote form-id-placeholder) form-id hook t))
539-
hook)))
539+
(if (string-match "_\\(N\\)\\'" hook)
540+
(progn
541+
(setq update-id-placeholder (match-string 1 hook))
542+
(setq update-id (read-number
543+
(concat "Implements " hook "(): ") (drupal-next-update-id)))
544+
(replace-regexp-in-string (regexp-quote update-id-placeholder) (number-to-string update-id) hook t))
545+
hook))))
540546
(drupal-ensure-newline)
541547
"/**\n"
542548
" * Implements " str "().\n"
@@ -545,6 +551,34 @@ buffer."
545551
" " @ _ "\n"
546552
"}\n")
547553

554+
(defun drupal-next-update-id ()
555+
"Find next update ID for hook_update_N().
556+
See https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_update_N/7."
557+
(let (existing-ids
558+
next-id
559+
(current-id 0)
560+
;; Lowest possible ID based current Drupal major-version and
561+
;; current module major version.
562+
(lowest-possible-id (+ (* (string-to-number (drupal-major-version)) 1000)
563+
(* (string-to-number (drupal-module-major-version :default "1")) 100)
564+
1)))
565+
;; Locate existing ID's in current buffer.
566+
(save-excursion
567+
(goto-char (point-min))
568+
(while (re-search-forward "_update_\\([0-9]+\\)(" nil t)
569+
(add-to-list 'existing-ids (string-to-number (match-string-no-properties 1)))))
570+
;; Find the largest of the existing ID's (current ID).
571+
(when existing-ids
572+
(setq current-id (apply 'max existing-ids)))
573+
;; Bump ID to get next update ID.
574+
(setq next-id (+ 1 current-id))
575+
;; If the next ID doesn't match current Drupal major-version and
576+
;; current module major version use ID based on current versions
577+
;; instead.
578+
(when (< next-id lowest-possible-id)
579+
(setq next-id lowest-possible-id))
580+
next-id))
581+
548582
(define-skeleton drupal-insert-function
549583
"Insert Drupal function skeleton."
550584
nil
@@ -765,6 +799,20 @@ Used in `drupal-insert-hook' and `drupal-insert-function'."
765799
(insert name)
766800
name)))
767801

802+
(defun* drupal-module-major-version (&key version default)
803+
"Return a modules major version number.
804+
If VERSION is not set derive it from the buffer local variable
805+
`drupal-major-version'.
806+
807+
If VERSION (and `drupal-major-version') is nil return DEFAULT."
808+
(when (null version)
809+
(setq version (or drupal-module-version "")))
810+
(let (major-version)
811+
(if (string-match "[0-9x\\.]+-\\([0-9]+\\)\\..*" version)
812+
(setq major-version (match-string-no-properties 1 version))
813+
(setq major-version default))
814+
major-version))
815+
768816
(defun drupal-major-version (&optional version)
769817
"Return major version number of version string.
770818
If major version number is 4 - return both major and minor."

0 commit comments

Comments
 (0)