在同一内容类型drupal中从一个字段复制到另一个字段

时间:2014-03-05 22:53:34

标签: drupal drupal-7

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

这可以通过模块实现,也可以通过SQL实现。我不知道该怎么做,我几周都试图这样做而且我被卡住了:(请帮帮忙!

非常感谢!

1 个答案:

答案 0 :(得分: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中将值添加为field_authors中的新node_reference,您需要执行以下操作:

<?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_authors字段中添加field_first_author中的值作为新节点引用

相关问题