public function TaxonomiesController::exportTaxonomies in Structure Sync 8
Same name and namespace in other branches
- 2.x src/Controller/TaxonomiesController.php \Drupal\structure_sync\Controller\TaxonomiesController::exportTaxonomies()
Function to export taxonomy terms.
File
- src/
Controller/ TaxonomiesController.php, line 37
Class
- TaxonomiesController
- Controller for syncing taxonomy terms.
Namespace
Drupal\structure_sync\ControllerCode
public function exportTaxonomies(array $form = NULL, FormStateInterface $form_state = NULL) {
StructureSyncHelper::logMessage('Taxonomies export started');
if (is_object($form_state) && $form_state
->hasValue('export_voc_list')) {
$vocabulary_list = $form_state
->getValue('export_voc_list');
$vocabulary_list = array_filter($vocabulary_list, 'is_string');
}
// Get a list of all vocabularies (their machine names).
if (!isset($vocabulary_list)) {
$vocabulary_list = [];
$vocabularies = $this->entityTypeManager
->getStorage('taxonomy_vocabulary')
->loadMultiple();
foreach ($vocabularies as $vocabulary) {
$vocabulary_list[] = $vocabulary
->id();
}
}
if (!count($vocabulary_list)) {
StructureSyncHelper::logMessage('No vocabularies available', 'warning');
drupal_set_message($this
->t('No vocabularies selected/available'), 'warning');
return;
}
// Clear the (previous) taxonomies data in the config.
$this->config
->clear('taxonomies')
->save();
// Get all taxonomies from each (previously retrieved) vocabulary.
foreach ($vocabulary_list as $vocabulary) {
$query = StructureSyncHelper::getEntityQuery('taxonomy_term');
$query
->condition('vid', $vocabulary);
$tids = $query
->execute();
$controller = $this->entityTypeManager
->getStorage('taxonomy_term');
$entities = $controller
->loadMultiple($tids);
$parents = [];
foreach ($tids as $tid) {
$parent = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadParents($tid);
$parent = reset($parent);
if (is_object($parent)) {
$parents[$tid] = $parent
->id();
}
}
// Build array of taxonomy terms and associated field values.
$taxonomies = [];
foreach ($entities as $entity) {
$entity_properties = [
'vid' => $vocabulary,
'tid' => $entity
->id(),
'langcode' => $entity->langcode->value,
'name' => $entity->name->value,
'description__value' => $entity
->get('description')->value,
'description__format' => $entity
->get('description')->format,
'weight' => $entity->weight->value,
'parent' => isset($parents[$entity
->id()]) ? $parents[$entity
->id()] : '0',
'uuid' => $entity
->uuid(),
];
// Identify and build array of any custom fields attached to terms.
$entity_fields = [];
$entity_field_names = [];
$all_term_fields = $entity
->getFields();
foreach ($all_term_fields as $field_name => $field) {
$is_custom_field = 'field_' === substr($field_name, 0, 6);
if ($is_custom_field) {
$entity_field_names[] = $field_name;
}
}
if ($entity_field_names) {
foreach ($entity_field_names as $field_name) {
$field_definition = $entity->{$field_name}
->getFieldDefinition();
$is_entity_reference = 'entity_reference' === $field_definition
->getType();
$is_term_reference = 'default:taxonomy_term' === $field_definition
->getSetting('handler');
if (!$is_entity_reference && !$is_term_reference) {
$entity_fields[$field_name] = $entity->{$field_name}
->getValue();
}
else {
$entity_reference_field_value = $entity->{$field_name}
->getValue();
foreach ($entity_reference_field_value as $field_item) {
$target_term_entity = StructureSyncHelper::getEntityManager()
->getStorage('taxonomy_term')
->load($field_item['target_id']);
if ($target_term_entity) {
$entity_fields[$field_name][] = [
'name' => $target_term_entity
->getName(),
'vid' => $target_term_entity
->getVocabularyId(),
];
}
}
}
}
}
$taxonomies[] = $entity_properties + $entity_fields;
}
// Save the retrieved taxonomies to the config.
$this->config
->set('taxonomies.' . $vocabulary, $taxonomies)
->save();
if (array_key_exists('drush', $form) && $form['drush'] === TRUE) {
drush_log('Exported ' . $vocabulary, 'ok');
}
StructureSyncHelper::logMessage('Exported ' . $vocabulary);
}
drupal_set_message($this
->t('The taxonomies have been successfully exported.'));
StructureSyncHelper::logMessage('Taxonomies exported');
}