Skip to content
Snippets Groups Projects
Commit 1b8024a0 authored by Klaus Purer's avatar Klaus Purer
Browse files

Added a sniff to detect drupal_add_js() usage in hook_page_build().

parent 151ba8fa
No related branches found
No related tags found
No related merge requests found
......@@ -46,7 +46,7 @@ class DrupalPractice_Sniffs_FunctionDefinitions_HookInitCssSniff extends Drupal_
$fileName = substr(basename($phpcsFile->getFilename()), 0, -7);
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] !== ($fileName.'_init')) {
if ($tokens[$stackPtr]['content'] !== ($fileName.'_init') && $tokens[$stackPtr]['content'] !== ($fileName.'_page_build')) {
return;
}
......@@ -67,8 +67,13 @@ class DrupalPractice_Sniffs_FunctionDefinitions_HookInitCssSniff extends Drupal_
if ($opener !== false
&& $tokens[$opener]['code'] === T_OPEN_PARENTHESIS
) {
if ($tokens[$stackPtr]['content'] === ($fileName.'_init')) {
$warning = 'Do not use %s() in hook_init(), move it to your page/form callback or use hook_page_build() instead';
$phpcsFile->addWarning($warning, $string, 'AddFunctionFound', array($tokens[$string]['content']));
} else {
$warning = 'Do not use %s() in hook_page_build(), use #attached on the $page render array instead';
$phpcsFile->addWarning($warning, $string, 'AddFunctionFoundPageBuild', array($tokens[$string]['content']));
}
}
}
......
......@@ -35,7 +35,10 @@ class DrupalPractice_Sniffs_FunctionDefinitions_HookInitCssUnitTest extends Code
*/
public function getWarningList($testFile)
{
return array(7 => 1);
return array(
7 => 1,
14 => 1,
);
}//end getWarningList()
......
......@@ -6,3 +6,10 @@
function test_init() {
drupal_add_css('somefile.css');
}
/**
* Implements hook_page_build().
*/
function test_page_build($page) {
drupal_add_css('somefile.css');
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment