【问题标题】:Execute JavaScript on node creation of a specific content type在特定内容类型的节点创建上执行 JavaScript
【发布时间】:2012-06-01 20:14:30
【问题描述】:

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

我们将 Drupal 7 内容类型称为“tear”,将 JavaScript 函数称为“doitnow();

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

非常感谢。

尼尔斯

【问题讨论】:

    标签: javascript drupal drupal-7


    【解决方案1】:

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

    You can download the whole module here.

    你必须把这个模块放到sites/all/modules/testModule目录下。

    好的,这是模块的代码:

    testModule.info:

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

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

    【讨论】:

    • +1。不知道覆盖的东西。非常直接和令人讨厌。
    • @Sk8erPeter,非常感谢!完美运行。只有当我将以下内容添加到 doitnow 函数中时,它才不再起作用:FB.api('/me/shareatear:share', 'post', { tear: document.URL }, function(response) { if (! response || response.error) { alert('发生错误'); } else { alert('done.' + response.id); } });
    • 跟命名空间有关系吗?
    • @Nils:不客气,很高兴我能帮上忙。请在浏览器中使用Ctrl+Shift+I(或Chrome 中的F12)打开开发者面板,然后点击Console 标签。当doitnow() 被调用时,此面板上会显示什么样的错误消息?也许这样我们就可以调试 JS 错误了。
    • @AyeshKarunaratne:谢谢,你也可以实现hook_overlay_parent_initialize(),并在那里添加一些代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多