<small id='Hf1xp'></small><noframes id='Hf1xp'>

  • <tfoot id='Hf1xp'></tfoot>

    <i id='Hf1xp'><tr id='Hf1xp'><dt id='Hf1xp'><q id='Hf1xp'><span id='Hf1xp'><b id='Hf1xp'><form id='Hf1xp'><ins id='Hf1xp'></ins><ul id='Hf1xp'></ul><sub id='Hf1xp'></sub></form><legend id='Hf1xp'></legend><bdo id='Hf1xp'><pre id='Hf1xp'><center id='Hf1xp'></center></pre></bdo></b><th id='Hf1xp'></th></span></q></dt></tr></i><div id='Hf1xp'><tfoot id='Hf1xp'></tfoot><dl id='Hf1xp'><fieldset id='Hf1xp'></fieldset></dl></div>

    <legend id='Hf1xp'><style id='Hf1xp'><dir id='Hf1xp'><q id='Hf1xp'></q></dir></style></legend>
    • <bdo id='Hf1xp'></bdo><ul id='Hf1xp'></ul>

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

        Execute JavaScript on node creation of a specific content type(在特定内容类型的节点创建上执行 JavaScript)

          <i id='O51WI'><tr id='O51WI'><dt id='O51WI'><q id='O51WI'><span id='O51WI'><b id='O51WI'><form id='O51WI'><ins id='O51WI'></ins><ul id='O51WI'></ul><sub id='O51WI'></sub></form><legend id='O51WI'></legend><bdo id='O51WI'><pre id='O51WI'><center id='O51WI'></center></pre></bdo></b><th id='O51WI'></th></span></q></dt></tr></i><div id='O51WI'><tfoot id='O51WI'></tfoot><dl id='O51WI'><fieldset id='O51WI'></fieldset></dl></div>

          <tfoot id='O51WI'></tfoot>
          1. <small id='O51WI'></small><noframes id='O51WI'>

              <tbody id='O51WI'></tbody>
            <legend id='O51WI'><style id='O51WI'><dir id='O51WI'><q id='O51WI'></q></dir></style></legend>

              • <bdo id='O51WI'></bdo><ul id='O51WI'></ul>

                  本文介绍了在特定内容类型的节点创建上执行 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 内容类型称为tear",JavaScript 函数称为doitnow();"

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

                  任何想法如何做到这一点?

                  Any ideas how to do this?

                  非常感谢.

                  尼尔斯

                  推荐答案

                  你必须创建自己的模块并实现 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:

                  /**
                   * Implements hook_node_insert()
                   * 
                   * @see http://api.drupal.org/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 http://api.drupal.org/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 http://api.drupal.org/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:

                  (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:

                  (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);
                  

                  如果有什么不清楚的地方问一下.

                  Ask if something isn't quite clear.

                  这篇关于在特定内容类型的节点创建上执行 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                • <legend id='JLIOh'><style id='JLIOh'><dir id='JLIOh'><q id='JLIOh'></q></dir></style></legend>
                  <i id='JLIOh'><tr id='JLIOh'><dt id='JLIOh'><q id='JLIOh'><span id='JLIOh'><b id='JLIOh'><form id='JLIOh'><ins id='JLIOh'></ins><ul id='JLIOh'></ul><sub id='JLIOh'></sub></form><legend id='JLIOh'></legend><bdo id='JLIOh'><pre id='JLIOh'><center id='JLIOh'></center></pre></bdo></b><th id='JLIOh'></th></span></q></dt></tr></i><div id='JLIOh'><tfoot id='JLIOh'></tfoot><dl id='JLIOh'><fieldset id='JLIOh'></fieldset></dl></div>

                  • <small id='JLIOh'></small><noframes id='JLIOh'>

                        <bdo id='JLIOh'></bdo><ul id='JLIOh'></ul>
                        <tfoot id='JLIOh'></tfoot>

                            <tbody id='JLIOh'></tbody>