以编程方式创建一个术语?


32

我正在尝试向词汇表中添加很多术语(〜200个),但是我找不到为Drupal 8更新的任何导入模块,而且看来在Drupal 7中执行此操作的功能在Drupal 8.那么,有人能指出我这样做的正确方向吗?

entity_create如注释中所建议的那样,我尝试使用以下代码来实现:

$term_create = entity_create('taxonomy_term', array('name' => 'test', 'vocabulary_name' => 'client'));

但是我得到了这个错误:

Drupal\Core\Entity\EntityStorageException: Missing bundle for entity type taxonomy_term in Drupal\Core\Entity\FieldableEntityStorageControllerBase->create() (line 65 of core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php).

有任何想法吗?


1
一个术语是一个实体,所以...entity_create()
克莱夫

我尝试使用以下代码执行此操作:$term_create = entity_create('taxonomy_term', array('name' => 'test', 'vocabulary_name' => 'client'));,但出现错误Drupal\Core\Entity\EntityStorageException: Missing bundle for entity type taxonomy_term in Drupal\Core\Entity\FieldableEntityStorageControllerBase->create() (line 65 of core/lib/Drupal/Core/Entity/FieldableEntityStorageControllerBase.php).-有任何想法吗?
Samsquanch 2014年

尝试vid代替vocabulary_name。貌似列仍然是vidtaxonomy_term_data的,但它现在的翻译名称,而不是ID
克莱夫

实体数据不应源自SQL表,请参见下文。

Answers:


42

您知道您需要分类法模块中的某些内容,因此首先需要查找Drupal\taxonomy\Entity-或相应的目录-您将在其中找到Term该类。现在看一下注释,它上面写着@ContentEntityType

*   entity_keys = {
*     "id" = "tid",
*     "bundle" = "vid",
*     "label" = "name",
*     "uuid" = "uuid"
*   },

所以,你想要的是

$term = Term::create([
  'name' => 'test', 
  'vid' => 'client',
])->save();

因为label实体密钥是name并且bundle实体密钥是vid。我添加了一个->save()电话,并假定您也想保存它。


可以在drupal8.ovh/en/tutoriels/55/…上找到更多选项。
科兰

1
$term = \Drupal\taxonomy\Entity\Term::create(array( 'name' => 'whatever', 'vid' => 'tags', )); $term->save();给我致命错误:调用未定义方法Drupal \ taxonomy \ Entity \ Term :: getType
alberto56 '16

15

此时,您应该以另一种方式添加术语(与答案相比)首先,在文件中,您应该编写

使用Drupal \ taxonomy \ Entity \ Term;

因为Term类在Drupal \ taxonomy \ Entity中列出。而且您不需要将taxonomy_term参数传递给

条款::创建

因为只需要一个参数(带有值的数组)(在分类法模块中为此方法列出的代码下方)

public function create(array $values = array()) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = array(0);
  }
  $entity = parent::create($values);
  return $entity;
}

所以最后一个例子是

use Drupal\taxonomy\Entity\Term;
$categories_vocabulary = 'blog_categories'; // Vocabulary machine name
$categories = ['test 1', 'test 2', 'test 3', 'test 4']; // List of test terms
foreach ($categories as $category) {
  $term = Term::create(array(
    'parent' => array(),
    'name' => $category,
    'vid' => $categories_vocabulary,
  ))->save();
}

3
您可能想知道的一些事情。$ term最有可能等于1,因为它Entity::save()返回一个int。常数SAVED_NEWSAVED_UPDATED取决于执行的操作。但是,如果要删除->save()和添加$term->save();,则将看到$term已更新,并使用保存到数据库的信息进行了更新。您现在可以执行的示例$tid = $term->tid->value;
Redneck将军城,2016年

7
Term::create([
 'name' => ''Lama',
 'vid' => $vocabulary_id,
]);

其他答案使用entity_create(),虽然有效,但效果并不理想。


6

entityTypeManager()

$term = [
  'name'     => $name,
  'vid'      => $vocabulary,
  'langcode' => $language,
];

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->create($term);

2

您可能想看看devel / devel_generate如何做到这一点。

devel_generate

$values['name'] = devel_generate_word(mt_rand(2, $maxlength));
$values['description'] = "description of " . $values['name'];
$values['format'] = filter_fallback_format();
$values['weight'] = mt_rand(0, 10);
$values['langcode'] = LANGUAGE_NOT_SPECIFIED;
$term = entity_create('taxonomy_term', $values);

2

在创建术语之前,最好检查一下它是否存在,这是代码:

use Drupal\taxonomy\Entity\Term;

if ($terms = taxonomy_term_load_multiple_by_name($term_value, 'vocabulary')) {
  // Only use the first term returned; there should only be one anyways if we do this right.
  $term = reset($terms);
} else {
  $term = Term::create([
    'name' => $term_value,
    'vid' => 'vocabulary',
  ]);
  $term->save();
}
$tid = $term->id();

资料来源:https : //www.btmash.com/article/2016-04-26/saving-and-retrieving-taxonomy-terms-programmatically-drupal-8

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.