هل هناك طريقة لتغيير ترتيب تنفيذ hook_form_alter في Drupal 7 بدون تغيير وزن الوحدة أو القرصنة Drupal Core؟
أحاول تغيير العنصر المضاف في translation_form_node_form_alter من وحدة الترجمة. عند تصحيح الأخطاء في النموذج ، لا يمكنني العثور على العنصر ، لذا أفترض أنه يتم تنفيذ ربطاتي قبل تنفيذ التصحيح في وحدة الترجمة.
لا أعتقد ذلك. translation_form_node_form_alter()
تنفذ hook_form_BASE_FORM_ID_alter()
التي أعتقد أنها تسمى بعدhook_form_alter()
، لذا فإن تغيير وزن الوحدة لن يكون كافيًا. أعتقد أن خياريك هما استخدام hook_form_BASE_FORM_ID_alter()
والتأكد من أن لديك وزن وحدة مرتفع بما يكفي ، أو استخدام hook_form_FORM_ID_alter()
(إن أمكن).
الجدير بالذكر أيضًا ، هناك drupal 7 API جديدة تسمى hook_module_implements_alter () والتي تتيح لك تغيير ترتيب التنفيذ لخطاف معين WIHOUT بتغيير جدول أوزان الوحدة.
نموذج لتعليمة برمجية من مستندات API توضح مدى سهولة القيام بذلك:
<?php
function hook_module_implements_alter(&$implementations, $hook) {
if ($hook == 'rdf_mapping') {
// Move my_module_rdf_mapping() to the end of the list. module_implements()
// iterates through $implementations with a foreach loop which PHP iterates
// in the order that the items were added, so to move an item to the end of
// the array, we remove it and then add it.
$group = $implementations['my_module'];
unset($implementations['my_module']);
$implementations['my_module'] = $group;
}
}
?>
إليك كيفية التأكد من استدعاء hook_form_alter الخاص بك بعد وحدات أخرى hook_form_alter:
/**
* Implements hook_form_alter().
*/
function my_module_form_alter(&$form, &$form_state, $form_id) {
// do your stuff
}
/**
* Implements hook_module_implements_alter().
*
* Make sure that our form alter is called AFTER the same hook provided in xxx
*/
function my_module_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter') {
// Move my_module_rdf_mapping() to the end of the list. module_implements()
// iterates through $implementations with a foreach loop which PHP iterates
// in the order that the items were added, so to move an item to the end of
// the array, we remove it and then add it.
$group = $implementations['my_module'];
unset($implementations['my_module']);
$implementations['my_module'] = $group;
}
}
يعمل هذا أيضًا عندما توفر الوحدة النمطية الأخرى ربطًا من نوع form_alter في الشكل: hook_form_FORM_ID_alter. (يشرحون ذلك في الوثائق: hook_module_implements_alter ).
أعلم أن هذا المنشور مشابه تمامًا لمشاركة wiifm ، ولكن اعتقدت أنه مفيد مع مثال مع hook_form_alter