diff --git a/core/lib/Drupal/Core/Batch/BatchBuilder.php b/core/lib/Drupal/Core/Batch/BatchBuilder.php index dce669874d83594ac07f012277ede6bc93a021ff..91550e716c4ef9100837b50baa95f423bca52aa9 100644 --- a/core/lib/Drupal/Core/Batch/BatchBuilder.php +++ b/core/lib/Drupal/Core/Batch/BatchBuilder.php @@ -205,12 +205,18 @@ public function setErrorMessage($message) { * The path should be relative to base_path(), and thus should be built using * drupal_get_path(). Defaults to {module_name}.module. * + * The file needs to be set before using ::addOperation(), + * ::setFinishCallback(), or any other function that uses callbacks from the + * file. This is so that PHP knows about the included functions. + * * @param string $filename * The path to the file. * * @return $this */ public function setFile($filename) { + include_once $filename; + $this->file = $filename; return $this; } diff --git a/core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php b/core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php index 4e273b2910925964008842d63d82129a68203bd2..e0ac9089c5f2e424dd166fa548e1d31b68b7d898 100644 --- a/core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Batch/BatchBuilderTest.php @@ -113,11 +113,20 @@ public function testSetErrorMessage() { * @covers ::setFile */ public function testSetFile() { + $filename = dirname(__DIR__, 6) . '/core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc'; + $this->assertIsNotCallable('_batch_test_callback_1'); + $this->assertIsNotCallable('_batch_test_finished_1'); + $batch = (new BatchBuilder()) - ->setFile('filename.php') + ->setFile($filename) + ->setFinishCallback('_batch_test_finished_1') + ->addOperation('_batch_test_callback_1', []) ->toArray(); - - $this->assertEquals('filename.php', $batch['file']); + $this->assertEquals($filename, $batch['file']); + $this->assertEquals([['_batch_test_callback_1', []]], $batch['operations']); + $this->assertEquals('_batch_test_finished_1', $batch['finished']); + $this->assertIsCallable('_batch_test_callback_1'); + $this->assertIsCallable('_batch_test_finished_1'); } /**