View source
<?php
namespace Drupal\organigrams\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrganigramsImportItemsForm extends FormBase {
protected $entityTypeManager;
protected $storageController;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->storageController = $entity_type_manager
->getStorage('taxonomy_term');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getFormId() {
return 'organigrams_import_terms_form';
}
public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
if (empty($taxonomy_vocabulary)) {
return [
'#markup' => $this
->t('No vocabulary found.'),
];
}
$form_state
->set([
'taxonomy',
'vocabulary',
], $taxonomy_vocabulary);
$form['organigrams_items_json'] = [
'#type' => 'textarea',
'#title' => $this
->t('Organigram items JSON'),
'#description' => $this
->t('JSON output of organigrams version 7.x and 8.x is supported.'),
'#required' => TRUE,
'#rows' => 20,
];
$form['warning'] = [
'#markup' => '<strong>' . $this
->t('Warning: this will delete all existing organigram items!') . '</strong>',
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$organigram_items = Json::decode($form_state
->getValue('organigrams_items_json'));
if (empty($organigram_items)) {
$form_state
->setErrorByName('organigrams_items_json', $this
->t('No organigram items found in the JSON.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$organigram_items = json_decode($form_state
->getValue('organigrams_items_json'));
$vocabulary = $form_state
->get([
'taxonomy',
'vocabulary',
]);
$existing_tids = \Drupal::entityQuery('taxonomy_term')
->condition('vid', $vocabulary
->id())
->execute();
if (!empty($existing_tids)) {
$entities = $this->storageController
->loadMultiple($existing_tids);
$this->storageController
->delete($entities);
}
$iid_mapping = [];
foreach ($organigram_items as $item) {
$term_array = [
'name' => $item->name,
'vid' => $vocabulary
->id(),
'parent' => $item->parent,
'weight' => $item->weight,
];
if (isset($iid_mapping[$item->parent])) {
$term_array['parent'] = $iid_mapping[$item->parent];
}
$ignore_keys = [
'iid',
'oid',
'rdf_mapping',
'organigrams_machine_name',
'depth',
];
foreach ($item as $key => $value) {
if (in_array($key, $ignore_keys)) {
continue;
}
if ($key == 'position' && in_array($value, [
'l',
'r',
])) {
$value = 's';
}
$term_array['field_o_' . $key] = $value;
}
$term = Term::create($term_array);
$term
->save();
$iid_mapping[$item->iid] = $term
->id();
}
$organigram_link = Link::fromTextAndUrl($this
->t('overview'), Url::fromUserInput('/admin/structure/taxonomy/manage/' . $vocabulary
->id() . '/overview'));
$this
->messenger()
->addMessage($this
->t('Organigram items imported. Go to @overview.', [
'@overview' => $organigram_link
->toString(),
]));
}
}