在特定内容类型的节点创建时执行JavaScript

编程入门 行业动态 更新时间:2024-10-17 09:51:20
本文介绍了在特定内容类型的节点创建时执行JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只想在一个特定内容类型的节点创建和节点编辑上执行一个JavaScript函数。这只是一个小功能调用。

I would like to execute a JavaScript function only on node creation and node edit of one specific content type. It is only one small function call.

我们来调用Drupal 7内容类型

Let's call the Drupal 7 content type "tear", and the JavaScript function "doitnow();"

任何想法如何做?

非常感谢。

Nils

推荐答案

您必须创建自己的模块并实现 hook_node_insert(), hook_node_update()和 hook_node_view()并采取适当的步骤。您还必须使用 Drupal行为创建一些JavaScript代码,准备好了在节点创建/更新后,您必须使用临时会话变量,因为这样可以检查hook_node_view中该变量的存在,并添加告知新的撕裂内容的JS设置。

You have to create your own module and implement hook_node_insert(), hook_node_update() and hook_node_view() and take the appropriate steps. You also have to create some JavaScript codes with Drupal behavior, and you're ready. You have to use a temporary session variable after node creation/update, because this way you can check the existance of this variable in hook_node_view, and add the JS setting which "tells" a new "tear" content has been added.

您必须将此模块放到 sites / all / modules / testModule 目录

You have to put this module to the sites/all/modules/testModule directory.

确定,以下是该模块的代码:

OK, here's the code of the module:

name = Test Customization Module description = Customizing stuffs on the site... core = 7.x package = My modules scripts[] = js/testModule.behaviors.js

testModule.module :

testModule.module:

/** * Implements hook_node_insert() * * @see api.drupal/api/drupal/modules!node!node.api.php/function/hook_node_insert/7 * @param object $node */ function testModule_node_insert($node) { switch ($node->type) { case 'tear': $_SESSION['tear_just_inserted'] = microtime(); break; } } /** * Implements hook_node_update() * * @see api.drupal/api/drupal/modules!node!node.api.php/function/hook_node_update/7 * @param object $node */ function testModule_node_update($node) { switch ($node->type) { case 'tear': $_SESSION['tear_just_inserted'] = microtime(); break; } } /** * Implements hook_node_view(). */ function testModule_node_view($node, $view_mode, $langcode) { switch ($node->type) { case 'tear': // we call doitnow() if it's needed (if "tear" content was added before) if (isset($_SESSION['tear_just_inserted'])) { drupal_add_js(array('testModule' => array('tear_just_added' => TRUE)), 'setting'); unset($_SESSION['tear_just_inserted']); } break; } } /** * Implements hook_overlay_child_initialize() * @see api.drupal/api/drupal/modules!overlay!overlay.api.php/function/hook_overlay_child_initialize/7 */ function testModule_overlay_child_initialize() { // Add our custom JavaScript: we don't want the node/add/tear to appear on the overlay, because this way the JS setting doesn't get added properly drupal_add_js(drupal_get_path('module', 'testModule') . '/js/testModule.overlay.behaviors.js'); }

js / testModule.behaviors.js

js/testModule.behaviors.js:

(function ($) { Drupal.behaviors.testModule = { doitnow: function(){ alert("A new \"tear\" content has just been added!"); }, attach: function (context, settings) { try{ if(settings.testModule.tear_just_added){ this.doitnow(); } } catch(ex){} } }; })(jQuery);

js / testModule.overlay.behaviors.js :

js/testModule.overlay.behaviors.js:

(function ($) { // Disable overlay for creating the "tear" node. Drupal.behaviors.testModule = { attach: function (context, settings) { var $tear_link = $('a[href*="node/add/tear"]'), tear_link_href = $tear_link.attr('href'); if( $tear_link.length > 0 ) { // we want to avoid opening the content adding for "tear" content type in the overlay // so we open it in the current window $tear_link.unbind(); $tear_link.click(function(e){ window.open(tear_link_href,'_self',false); e.stopPropagation(); }) } }; } })(jQuery);

询问是否有些不清楚。

更多推荐

在特定内容类型的节点创建时执行JavaScript

本文发布于:2023-07-17 02:50:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128653.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   类型   内容   在特定   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!