public function MappingEditForm::automaticTermsGenerator in GatherContent 8.3
Generate automatically terms for local field from GatherContent options.
Parameters
\Drupal\field\Entity\FieldConfig $handlerSettings: Field config for local field.
array $localOptions: Array of remote options.
string $langcode: The language of the generated term.
1 call to MappingEditForm::automaticTermsGenerator()
- MappingEditForm::submitForm in src/
Form/ MappingEditForm.php - This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…
File
- src/
Form/ MappingEditForm.php, line 1006
Class
- MappingEditForm
- Class MappingEditForm.
Namespace
Drupal\gathercontent\FormCode
public function automaticTermsGenerator(FieldConfig $handlerSettings, array $localOptions, $langcode) {
$settings = $handlerSettings
->getSetting('handler_settings');
/** @var \Drupal\taxonomy\Entity\Term[] $terms */
if (!empty($settings['auto_create_bundle'])) {
$vid = $settings['auto_create_bundle'];
}
else {
$vid = reset($settings['target_bundles']);
}
// Check if field exists.
$this
->gcOptionIdsFieldExists($vid);
foreach ($localOptions as $id => $localOption) {
$query = \Drupal::entityQuery('taxonomy_term');
$group = $query
->orConditionGroup()
->condition('gathercontent_option_ids', $id)
->condition('name', $localOption);
$term_ids = $query
->condition($group)
->condition('vid', $vid)
->condition('langcode', $langcode)
->execute();
$term_id = array_shift($term_ids);
if (!empty($term_id)) {
$term = Term::load($term_id);
if ($langcode === LanguageInterface::LANGCODE_NOT_SPECIFIED) {
if ($term
->label() !== $localOption) {
$term
->setName($localOption);
}
if (!in_array($id, $term
->get('gathercontent_option_ids')
->getValue())) {
$term->gathercontent_option_ids
->appendItem($id);
}
}
else {
if ($term
->getTranslation($langcode)
->label() !== $localOption) {
$term
->getTranslation($langcode)
->setName($localOption);
}
if (!in_array($id, $term
->getTranslation($langcode)->gathercontent_option_ids
->getValue())) {
$term
->getTranslation($langcode)->gathercontent_option_ids
->appendItem($id);
}
}
$term
->save();
$this->erImported++;
}
else {
$term_values = [
'vid' => $vid,
'langcode' => $langcode,
];
$term = Term::create($term_values);
$term
->setName($localOption);
$term
->set('gathercontent_option_ids', $id);
$term
->save();
$this->erImported++;
}
}
}