>本文演示了如何构建Drupal模块,使用户使用jQuery Ajax“喜欢”帖子,要求保存异步数据。
>
密钥功能:
-
通过jQuery ajax。
- > likes的数据库存储(每个用户,每个帖子)。 >
- 实时类似于计数更新。
- clear ui表示/不同的状态。
在您的Drupal安装中创建
- 文件夹。
- 内部
likepost
,创建sites/all/modules/custom
: >
-
likepost
likepost.info
create
<code>name = likepost description = Allows users to like Drupal posts. core = 7.x</code>实现下面包括以进行完整性,但核心功能将在后面添加)
-
likepost.module
hook_help
创建 来定义数据库架构:
/** * @file * Main module file. */ /** * Implements hook_help(). */ function likepost_help($path, $arg) { if ($path == 'admin/help#likepost') { $output = '<h3 id="t-About">' . t('About') . '</h3>'; $output .= '<p>' . t('This module allows users to like posts in Drupal.') . '</p>'; return $output; } }
- >安装模块。 将创建
likepost.install
表。 >
<?php /** * Implements hook_schema(). */ function likepost_schema() { $schema['likepost_table_for_likes'] = array( 'description' => t('Stores post likes.'), 'fields' => array( 'userid' => array( 'type' => 'int', 'not null' => TRUE, 'default' => 0, 'description' => t('User ID'), ), 'nodeid' => array( 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'description' => t('Node ID'), ), ), 'primary key' => array('userid', 'nodeid'), ); return $schema; }
likepost_table_for_likes
>将以下函数添加到
>
>显示“喜欢”按钮(akepost.module):
likepost.module
/** * Implements hook_menu(). */ function likepost_menu() { $items['likepost/like/%'] = array( 'title' => 'Like', 'page callback' => 'likepost_like', 'page arguments' => array(2), 'access arguments' => array('access content'), 'type' => MENU_SUGGESTED_ITEM, ); return $items; } function likepost_like($nodeid) { $nodeid = (int) $nodeid; global $user; $like = likepost_get_like($nodeid, $user->uid); if ($like) { db_delete('likepost_table_for_likes') ->condition('userid', $user->uid) ->condition('nodeid', $nodeid) ->execute(); $like = 0; } else { db_insert('likepost_table_for_likes') ->fields(array( 'userid' => $user->uid, 'nodeid' => $nodeid, )) ->execute(); $like = 1; } $total_count = likepost_get_total_like($nodeid); drupal_json_output(array( 'like_status' => $like, 'total_count' => $total_count, )); } function likepost_get_total_like($nid) { $total_count = db_query('SELECT COUNT(*) FROM {likepost_table_for_likes} WHERE nodeid = :nodeid', array(':nodeid' => $nid))->fetchField(); return (int) $total_count; } function likepost_get_like($nodeid, $userid) { $like = db_query('SELECT COUNT(*) FROM {likepost_table_for_likes} WHERE nodeid = :nodeid AND userid = :userid', array(':nodeid' => $nodeid, ':userid' => $userid))->fetchField(); return (int) $like; }>样式(akepost.css):
jQuery(akepost.js):
/** * Implements hook_node_view(). */ function likepost_node_view($node, $view_mode) { if ($view_mode == 'full') { $node->content['likepost_display'] = array( '#markup' => display_like_post_details($node->nid), '#weight' => 100, ); $node->content['#attached']['js'][] = array( 'data' => drupal_get_path('module', 'likepost') . '/likepost.js', ); $node->content['#attached']['css'][] = array( 'data' => drupal_get_path('module', 'likepost') . '/likepost.css', ); } } function display_like_post_details($nid) { global $user; $totalLike = likepost_get_total_like($nid); $hasCurrentUserLiked = likepost_get_like($nid, $user->uid); return theme('like_post', array( 'nid' => $nid, 'totalLike' => $totalLike, 'hasCurrentUserLiked' => $hasCurrentUserLiked, )); } /** * Implements hook_theme(). */ function likepost_theme() { $themes = array( 'like_post' => array( 'arguments' => array('nid', 'totalLike', 'hasCurrentUserLiked'), ), ); return $themes; } function theme_like_post($arguments) { $nid = $arguments['nid']; $totalLike = $arguments['totalLike']; $hasCurrentUserLiked = $arguments['hasCurrentUserLiked']; global $base_url; $output = '<div>'; $output .= 'Total number of likes on the post are '; $output .= '<div class="total_count">' . $totalLike . '</div>'; $linkText = ($hasCurrentUserLiked == 0) ? 'Like' : 'Unlike'; $output .= l($linkText, $base_url . '/likepost/like/' . $nid, array('attributes' => array('class' => 'like-link'))); $output .= '</div>'; return $output; }
记住进行更改后清除Drupal的缓存。 这种详细的解释应允许对喜欢模块的更强大,可理解的实现。 屏幕截图是占位符;您将在Drupal站点中看到实际的模块输出。
以上是Drupal进行社交:在Drupal中建立一个'喜欢”模块的详细内容。更多信息请关注PHP中文网其他相关文章!

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

WebStorm Mac版
好用的JavaScript开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。