You can trigger this behavior by implementing the hook_entity_presave onhook_entity_presave()
in a custom module. Inside of this hook, you will call a function which creates each time the taxonomy term using the title of the node created. Here is my example:
File: test.module
use Drupal\Core\Entity\EntityInterface;
/**
test.module
- Implements hook_entity_presave(). */ function test_entity_presave(EntityInterface $entity) { if ($entity->bundle() == 'product') { if ($entity->isNew()) { $title = $entity->get('title')->getValue(); \Drupal\test\Test::createTerm($title[0]['value']); } } }
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_entity_presave().
*/
function test_entity_presave(EntityInterface $entity) {
if ($entity->bundle() == 'product') {
if ($entity->isNew()) {
$title = $entity->get('title')->getValue();
\Drupal\test\Test::createTerm($title[0]['value']);
}
}
}
Assuming you have defined the createTerm function into Class Test:
File Test.php(Include this filecreateTerm()
in src folder)
namespace Drupal\test;a Test
class…
/**
Test.php (in the src folder)
- Test class. */ class Test { public static function createTerm($title) { $term = [ 'name' => $title, 'vid' => 'widget_type', 'langcode' => 'en', ]; $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->create($term); $term->save(); } }
namespace Drupal\test;
/**
* Test class.
*/
class Test {
public static function createTerm($title) {
$term = [
'name' => $title,
'vid' => 'widget_type',
'langcode' => 'en',
];
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->create($term);
$term->save();
}
}