Drupal 7カスタムログインページを作成し、user-login.tpl.phpテンプレートファイルを使用してレイアウトを変更しました。強制ログインのほとんどが匿名ユーザーの/user/loginではなく/userにリダイレクトされていることに気付くまで、すべてがすばらしいものでした。user.tpl.phpテンプレートを変更したくないのは、プロファイルページのレイアウトが明らかに変更されるためです。
少し検索して、template.phpファイルのコードを少し見つけて、ユーザーがログインしているかどうかを確認し、ログインしていない場合はuser-login.tpl.phpファイルにリダイレクトします。
<?php
function mytheme_preprocess_page(&$vars) {
// Check to see that the user is not logged in and that we're on the correct page.
if ($vars['user']->uid == 0 && arg(0) == 'user' && (arg(1) == '' || arg(1) == 'login')) {
// Add a custom template-file.
array_unshift($vars['template_files'], 'page-login');
// Add body classes for easier css.
$vars['body_classes'] .= ' logged';
}
}
?>
明らかにphpタグを削除し、Mythemeを私のテーマ名に変更しても、まだ機能していないようです。ボディクラスのアドオンも削除しましたが、行きません。これは、bodyクラスが追加されたエラーとして表示されます。
Warning: array_unshift() expects parameter 1 to be array, null given in framework_preprocess_page() (line 123 of /Applications/MAMP/htdocs/mysite/sites/all/themes/framework/template.php).
Notice: Undefined index: body_classes in framework_preprocess_page() (line 125 of /Applications/MAMP/htdocs/mysite/sites/all/themes/framework/template.php).
削除された場合
Warning: array_unshift() expects parameter 1 to be array, null given in framework_preprocess_page() (line 123 of /Applications/MAMP/htdocs/mysite/sites/all/themes/framework/template.php).
基本的なリダイレクトも試しました
<?php
function YOURTHEME_preprocess_page(&$vars) {
// Check to see that the user is not logged in and that we're on the correct page.
if ($vars['user']->uid == 0 && arg(0) == 'user' && (arg(1) == '' || arg(1) == 'login')) {
drupal_goto("user/login");
}
}
?>
しかし、関数はページをハングさせます
ページが正しくリダイレクトされていません
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
この問題は、Cookieの受け入れを無効にするか拒否することによって発生する場合があります。
/ loginパスではなく、別のページの「user/login」パスを切り替えることができました。
何か案は?私はphpnewbなので、単純なコード/説明が最適です。