diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-10-13 16:38:43 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-10-13 16:38:43 +0000 |
commit | fbfa7a4150a40d6df9347692bb6681f45f804c7d (patch) | |
tree | 425db0f6cc7b72b8a9350c5a462fe718e0ed2877 /modules | |
parent | f67e438b4196a6bc79c0da576da0a9e09fedb612 (diff) | |
download | brdo-fbfa7a4150a40d6df9347692bb6681f45f804c7d.tar.gz brdo-fbfa7a4150a40d6df9347692bb6681f45f804c7d.tar.bz2 |
- Patch #593522 by sun: a better and faster drupal_alter().
Diffstat (limited to 'modules')
-rw-r--r-- | modules/field/field.api.php | 36 | ||||
-rw-r--r-- | modules/field/field.attach.inc | 15 | ||||
-rw-r--r-- | modules/simpletest/tests/common.test | 53 | ||||
-rw-r--r-- | modules/simpletest/tests/common_test.module | 32 | ||||
-rw-r--r-- | modules/system/system.module | 6 | ||||
-rw-r--r-- | modules/upload/upload.module | 3 |
6 files changed, 120 insertions, 25 deletions
diff --git a/modules/field/field.api.php b/modules/field/field.api.php index 52673a39c..60e54d5d8 100644 --- a/modules/field/field.api.php +++ b/modules/field/field.api.php @@ -932,15 +932,15 @@ function hook_field_attach_presave($obj_type, $object) { * This hook is invoked while preprocessing the field.tpl.php template file. * * @param $variables - * The variables array is passed by reference and will be populated with field values. - * @param $obj_type - * The type of $object; e.g. 'node' or 'user'. - * @param $object - * The object with fields to render. - * @param $element - * The structured array containing the values ready for rendering. + * The variables array is passed by reference and will be populated with field + * values. + * @param $context + * An associative array containing: + * - obj_type: The type of $object; e.g. 'node' or 'user'. + * - object: The object with fields to render. + * - element: The structured array containing the values ready for rendering. */ -function hook_field_attach_preprocess_alter(&$variables, $obj_type, $object, $element) { +function hook_field_attach_preprocess_alter(&$variables, $context) { } /** @@ -1042,18 +1042,16 @@ function hook_field_attach_delete_revision($obj_type, $object) { * * This hook is invoked after the field module has performed the operation. * - * @param $output - * The structured content array tree for all of $object's fields. - * @param $obj_type - * The type of $object; e.g. 'node' or 'user'. - * @param $object - * The object with fields to render. - * @param $build_mode - * Build mode, e.g. 'full', 'teaser'... - * @param $langcode - * The language in which the field values will be displayed. + * @param &$output + * The structured content array tree for all of $object's fields. + * @param $context + * An associative array containing: + * - obj_type: The type of $object; e.g. 'node' or 'user'. + * - object: The object with fields to render. + * - build_mode: Build mode, e.g. 'full', 'teaser'... + * - langcode: The language in which the field values will be displayed. */ -function hook_field_attach_view_alter($output, $obj_type, $object, $build_mode, $langcode) { +function hook_field_attach_view_alter(&$output, $context) { } /** diff --git a/modules/field/field.attach.inc b/modules/field/field.attach.inc index 01abd6f78..4118a60e1 100644 --- a/modules/field/field.attach.inc +++ b/modules/field/field.attach.inc @@ -1193,7 +1193,13 @@ function field_attach_view($obj_type, $object, $build_mode = 'full', $langcode = $output['#extra_fields'] = field_extra_fields($bundle); // Let other modules make changes after rendering the view. - drupal_alter('field_attach_view', $output, $obj_type, $object, $build_mode, $langcode); + $context = array( + 'obj_type' => $obj_type, + 'object' => $object, + 'build_mode' => $build_mode, + 'langcode' => $langcode, + ); + drupal_alter('field_attach_view', $output, $context); return $output; } @@ -1228,7 +1234,12 @@ function field_attach_preprocess($obj_type, $object, $element, &$variables) { } // Let other modules make changes to the $variables array. - drupal_alter('field_attach_preprocess', $variables, $obj_type, $object, $element); + $context = array( + 'obj_type' => $obj_type, + 'object' => $object, + 'element' => $element, + ); + drupal_alter('field_attach_preprocess', $variables, $context); } /** diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index 53d493210..55b4b4332 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -2,6 +2,59 @@ // $Id$ /** + * @file + * Tests for common.inc functionality. + */ + +/** + * Tests for URL generation functions. + */ +class DrupalAlterTestCase extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'drupal_alter() tests', + 'description' => 'Confirm that alteration of arguments passed to drupal_alter() works correctly.', + 'group' => 'System', + ); + } + + function setUp() { + parent::setUp('common_test'); + } + + function testDrupalAlter() { + $array = array('foo' => 'bar'); + $object = new stdClass; + $object->foo = 'bar'; + + // Verify alteration of a single argument. + $array_copy = $array; + $array_expected = array('foo' => 'Drupal'); + drupal_alter('drupal_alter', $array_copy); + $this->assertEqual($array_copy, $array_expected, t('Single array was altered.')); + + $object_copy = clone $object; + $object_expected = clone $object; + $object_expected->foo = 'Drupal'; + drupal_alter('drupal_alter', $object_copy); + $this->assertEqual($object_copy, $object_expected, t('Single object was altered.')); + + // Verify alteration of multiple arguments. + $array_copy = $array; + $array_expected = array('foo' => 'Drupal'); + $object_copy = clone $object; + $object_expected = clone $object; + $object_expected->foo = 'Drupal'; + $array2_copy = $array; + $array2_expected = array('foo' => 'Drupal'); + drupal_alter('drupal_alter', $array_copy, $object_copy, $array2_copy); + $this->assertEqual($array_copy, $array_expected, t('First argument to drupal_alter() was altered.')); + $this->assertEqual($object_copy, $object_expected, t('Second argument to drupal_alter() was altered.')); + $this->assertEqual($array2_copy, $array2_expected, t('Third argument to drupal_alter() was altered.')); + } +} + +/** * Tests for URL generation functions. */ class CommonURLUnitTest extends DrupalUnitTestCase { diff --git a/modules/simpletest/tests/common_test.module b/modules/simpletest/tests/common_test.module index f7abf51b6..74866f098 100644 --- a/modules/simpletest/tests/common_test.module +++ b/modules/simpletest/tests/common_test.module @@ -10,7 +10,6 @@ * Implement hook_menu(). */ function common_test_menu() { - $items = array(); $items['common-test/drupal_goto'] = array( 'title' => 'Drupal Goto', 'page callback' => 'common_test_drupal_goto_land', @@ -71,6 +70,37 @@ function common_test_drupal_goto_alter(&$args) { } /** + * Implement hook_TYPE_alter(). + */ +function common_test_drupal_alter_alter(&$data, &$arg2 = NULL, &$arg3 = NULL) { + // Alter first argument. + if (is_array($data)) { + $data['foo'] = 'Drupal'; + } + elseif (is_object($data)) { + $data->foo = 'Drupal'; + } + // Alter second argument, if present. + if (isset($arg2)) { + if (is_array($arg2)) { + $arg2['foo'] = 'Drupal'; + } + elseif (is_object($arg2)) { + $arg2->foo = 'Drupal'; + } + } + // Try to alter third argument, if present. + if (isset($arg3)) { + if (is_array($arg3)) { + $arg3['foo'] = 'Drupal'; + } + elseif (is_object($arg3)) { + $arg3->foo = 'Drupal'; + } + } +} + +/** * Implement hook_theme(). */ function common_test_theme() { diff --git a/modules/system/system.module b/modules/system/system.module index 483ea8643..0e932863d 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -1916,7 +1916,8 @@ function _system_rebuild_module_data() { // Invoke hook_system_info_alter() to give installed modules a chance to // modify the data in the .info files if necessary. - drupal_alter('system_info', $modules[$key]->info, $modules[$key], 'module'); + $type = 'module'; + drupal_alter('system_info', $modules[$key]->info, $modules[$key], $type); } // The install profile is required. @@ -2009,7 +2010,8 @@ function _system_rebuild_theme_data() { // Invoke hook_system_info_alter() to give installed modules a chance to // modify the data in the .info files if necessary. - drupal_alter('system_info', $themes[$key]->info, $themes[$key], 'theme'); + $type = 'theme'; + drupal_alter('system_info', $themes[$key]->info, $themes[$key], $type); if (!empty($themes[$key]->info['base theme'])) { $sub_themes[] = $key; diff --git a/modules/upload/upload.module b/modules/upload/upload.module index 21f239e24..51328d97c 100644 --- a/modules/upload/upload.module +++ b/modules/upload/upload.module @@ -692,8 +692,9 @@ function upload_js() { '#tree' => FALSE, '#parents' => array(), ); - drupal_alter('form', $form, array(), 'upload_js'); $form_state = array('submitted' => FALSE, 'programmed' => FALSE); + $form_id = 'upload_js'; + drupal_alter('form', $form, $form_state, $form_id); $form = form_builder('upload_js', $form, $form_state); $output = theme('status_messages') . drupal_render($form); |