【问题标题】:Copying from one field to another in the same content type drupal在相同内容类型的drupal中从一个字段复制到另一个字段
【发布时间】:2014-03-05 22:53:33
【问题描述】:

我有一个名为“出版物”的内容类型,在该内容类型中我有两个字段,“第一作者”和“作者”。我已经用这两个字段创建了很多内容,并且它们都包含很多信息。 我需要做的是将“第一作者”字段中的所有内容复制或移动到“作者”字段这两个字段都是节点引用,我正在使用 Drupal 7。

这可能是通过模块还是来自 SQL。我不知道该怎么做,几周以来我一直在尝试这样做,但我被卡住了:(请帮忙!

非常感谢!

【问题讨论】:

    标签: drupal drupal-7


    【解决方案1】:

    您应该编写一个模块来执行此操作。

    您将需要一个执行以下操作的函数:

    <?php 
    $type = "publications"; 
    $nodes = node_load_multiple(array(), array('type' => $type)); 
    foreach($nodes as $node){
        $node->field_authors[LANGUAGE_NONE][0]['nid'] = $node->field_first_author[LANGUAGE_NONE][0]['nid']; 
        node_save($node);
    }
    ?>
    

    对于独立脚本,您需要添加以下内容:

    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    

    然后是上面的代码。

    因此,您将拥有一个名为 updateAuthors.php 的 php,其内容如下:

    <?php
        require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
        drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    
        $type = "publications"; 
        $nodes = node_load_multiple(array(), array('type' => $type)); 
        foreach($nodes as $node){
            $node->field_authors[LANGUAGE_NONE][0]['nid'] = $node->field_first_author[LANGUAGE_NONE][0]['nid']; 
            node_save($node);
        }
    ?>
    

    要将 first_author 中的值作为新的 node_reference 添加到 field_authors 中,您需要执行以下操作:

    <?php
        require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
        drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    
        $type = "publications"; 
        $nodes = node_load_multiple(array(), array('type' => $type)); 
        foreach($nodes as $node){
            $firstAuthor = $node->field_first_author[LANGUAGE_NONE][0]['nid']; 
            if ($firstAuthor === false){
                continue;
            }
            $numberOfValuesInFieldAuthors = count($node->field_authors[LANGUAGE_NONE]);
            $node->field_authors[LANGUAGE_NONE][$numberOfValuesInFieldAuthors]['nid'] = $firstAuthor
            node_save($node);
        }
    ?>
    

    这将跳过任何没有第一作者集的节点,对于有第一作者集的节点,将在 field_first_author 中添加值作为 field_authors 字段中的新节点引用

    【讨论】:

    • 作为编写模块的替代方法,您可以简单地在 drupal 根目录中放置一个 php 文件,包含引导程序包含,然后添加上述代码 - 然后您只需要导航到脚本在您的浏览器中。例如。 localhost/migrate-authors.php
    • 非常感谢您的帮助,我是 Drupal 新手,对此感到迷茫。我将如何编写模块?它困难吗?它是如何工作的?代码只提到了 field_authors,它怎么知道要抓取所有 field_first_author 并将它们复制到那里?抱歉所有问题,我真的需要帮助!另一种选择是将php文件放在drupal根目录中,但是包含“引导程序包含”是什么意思,那是什么以及如何包含它?非常感谢您的帮助,希望您能帮助我更多:)
    • 我打错了,赋值运算符右侧引用的字段名称应该是 field_author。我已经更新了上面的答案,其中包含有关加载所有 drupal 功能和模块的 drupal 引导代码
    • 非常感谢,我仍然没有看到如何将“field_first_author”中的字段全部复制到“field_authors”字段中。我应该更改代码,使其显示:“$node->field_authors[LANGUAGE_NONE][0]['nid'] = $node->field_first_author[LANGUAGE_NONE][0]['nid'];”? “field_first_author”字段中已经有一些数据是否重要?同样,这两个字段是参考节点是否重要?再次感谢您的帮助:)
    • 或者应该反过来:"$node-> field_first_author[LANGUAGE_NONE][0]['nid'] = $node-> field_authors[LANGUAGE_NONE][0]['nid '];"?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多