【问题标题】:How to make link to comment form drupal?如何制作评论表单drupal的链接?
【发布时间】:2015-05-06 03:11:03
【问题描述】:

我需要像这样在drupal中创建一个评论表单的自定义链接:

<a href="http://mypage/comment/reply/NID#comment-form">Reply</a>

如何获取节点NID?

【问题讨论】:

    标签: php html drupal hyperlink drupal-6


    【解决方案1】:

    您可以访问$node 对象。 $node->nid 将返回您的 NID 节点。

    或者

    <?php
     if (arg(0) == 'node' && is_numeric(arg(1))) $nodeid = arg(1);
     echo $nodeid; // show me your nid!
    ?>
    

    编辑(功能更通用):

    <?php
    /**
     * Used to get the current viewed node (works when viewed in page mode).
     * @param array $node_types[optional] A filter on the type of node you want to see.
     * @return object The node or null if not successfull.
     */
    function helper_get_current_node($node_types = array()) {
      // Store the current node id, to avoid doing the URL testing
      // on every call to this function. I didn't store the node itself
      // because I was afraid of data changes during page processing.
      // Normally node_load() already does some static caching and I think
      // it handles cache updates correctly.
      static $nid;
    
      if (!isset($nid)) {
        $arg = arg(); // Get URL splitted.
    
        // What type of URL is it?
        switch ($arg[0]) {
          // Viewing a node or a revision of a node :
          case 'node':
            // If the node id is missing, null or not numeric
            if (!isset($arg[1]) || is_null($arg[1]) || !is_numeric($arg[1])) {
              $nid = false;
            }
            // Look at the 3rd part of the URL ('edit', 'view', 'revisions', ...)
            if (isset($arg[2])) {
              switch ($arg[2]) {
                case 'view':
                  break;
                case 'revisions':
                  // If we are not viewing a revision
                  if (!isset($arg[4]) || $arg[4] != 'view') {
                    $nid = false;
                  }
                  break;
                default: // 'edit', 'delete', etc...
                  $nid = false;
              }
            }
            // If $nid has not been set, it means we where viewing a node.
            if (!isset($nid)) {
              $nid = $arg[1];
            }
            break;
    
          // Commenting a node :
          case 'comment':
            // If the URL just has /comment, or if the node id is missing or not numeric
            if (!isset($arg[1]) || !isset($arg[2]) || !is_numeric($arg[2])) {
              $nid = false;
            }
            // If $nid has not been set to false, it means we should be commenting a node.
            if (!isset($nid)) {
              $nid = $arg[2];
            }
            break;
    
          // URL doesn't start with something relative to node viewing
          default:
            $nid = false;
        }
    
      } // end if $nid is not set.
    
      // Return null if we are not viewing a node.
      if (!$nid) return null;
    
      // Load the node.
      $viewedNode = node_load($nid);
    
      // return null, if node not loaded, if node isn't the desired type
      // or if the user isn't allowed to see this node.
      if (!$viewedNode ||
          !node_access('view', $viewedNode) ||
          (count($node_types) > 0 && array_search($viewedNode->type, $node_types) === false)) {
        return null;
      }
    
      return $viewedNode;
    }
    ?>
    
    • 参考:

    https://www.drupal.org/node/160921

    【讨论】:

    • @user4843525 我用另一个解决方案编辑。您在哪种类型的页面中工作? (查看节点或节点的修订、分类,...?)
    • 它是一个视图中的页脚文本,我做了一个视图类型段来代替默认的论坛主题处理,并想制作回复链接。
    • 好的,我找到了解决方案:回复
    • @user4843525 这是我回答中的第二个代码,看答案
    • @user4843525 如果您认为我的回答可以帮助您记住您可以将其检查为已接受 ;-)
    猜你喜欢
    • 2015-01-24
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多