Drupal 用户身份验证检查

发布于 2025-01-17 03:46:05 字数 244 浏览 0 评论 0原文

我一直在尝试编写一个函数来检查用户是否经过身份验证,然后调用谷歌分析脚本来生成自定义报告。这是一个很大的麻烦,我相信 Drupal 并没有那么困难。有谁可以帮助或承担该项目吗? 这应该是一件简单而直接的事情,但能够检查用户身份验证并不容易

我尝试使用 php 函数来检查用户是否登录,但我无法从中调用 javascript 函数。

我用 jquery 编写了一个脚本函数,在页面加载时调用脚本,但我无法检查用户是否使用 javascript 登录

I have been trying to write a function to check if user is authenticated and then call a google analytics script for custom report. This has been a big hassle and I believe Drupal is not that difficult. Is there anyone who can help or take up the project?
It should be a simple and straight forward thing to do but being able to check user authentication has not been easy

I've tried to use a php function to check if user is logged in but I was not able to call javascript function from it.

I wrote a script function with jquery calling the script on page load but I was not able to check if user is logged in using javascript

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2025-01-24 03:46:05

也许这不是最好的选择,但是...

  1. 您如何看待从 JS 函数调用自定义模块的控制器,如果...则返回 true
    $currentUser = \Drupal::currentUser(); $uid = $currentUser->id(); 返回大于 0 的数值,如果返回的值为 0,则返回 false代码>(匿名用户)?该控制器只需以 JSON 格式返回当前用户的 UID,然后可以从 JS 中读取。

  2. 另一种尝试是在主题的 .theme 文件中编写 YOURTHEMENAME_preprocess_page() 函数来实现上述检查,并将值发送到模板,以便在某些 DOM 中的“数据属性”中呈现该值元素。渲染后,您可以从 JS 访问该值。

这些方法都不是做这些事情的“Drupal 方式”,但是......希望这些对您有所帮助。

干杯。

Maybe it's not the best option, but...

  1. What do you think about calling, from your JS function, a custom module's controller, that return true if...
    $currentUser = \Drupal::currentUser(); $uid = $currentUser->id(); returns a numeric value higher than 0 or false if the value returned is 0 (anonymous user)? This controller only should had to return the UID of the current user in a JSON format, readable afterwards from your JS.

  2. Another try would be to write a YOURTHEMENAME_preprocess_page() function in your theme's .theme file that implement the above check, and send the value to the template, in order to render the value in a 'data-attribute' in some DOM element. Once rendered, you could access the value from your JS.

None of these approachs are 'Drupal way' of doing the stuff, but... hope these help you.

Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文