public function SimpleNodeImporterMappingForm::buildForm in Simple Node Importer 8
Builds Flexible Mapping UI.
Overrides FormInterface::buildForm
File
- src/
Form/ SimpleNodeImporterMappingForm.php, line 71
Class
- SimpleNodeImporterMappingForm
- Flexible Mapping Form for the Simple Node Importer.
Namespace
Drupal\simple_node_importer\FormCode
public function buildForm(array $form, FormStateInterface $form_state, $option = NULL, NodeInterface $node = NULL) {
global $base_url;
$type = 'module';
$module = 'simple_node_importer';
$filepath = $base_url . '/' . drupal_get_path($type, $module) . '/css/files/mapping.png';
$fid = $node
->get('field_upload_csv')
->getValue()[0]['target_id'];
$file = File::load($fid);
$uri = $file
->getFileUri();
if (empty($node)) {
$type = 'Simple Node Importer';
$message = 'Node object is not valid.';
$this->logger
->get($type)
->error($message, []);
}
elseif ($this->sessionVariable
->get('file_upload_session') == FALSE) {
$response = new RedirectResponse('/node/add/simple-node');
$response
->send();
}
else {
// Options to be listed in File Column List.
$headers = $this->services
->simpleNodeImporterGetAllColumnHeaders($uri);
$selected_content_type = $node
->get('field_select_content_type')
->getValue()[0]['value'];
$entity_type = $node
->getEntityTypeId();
$type = 'mapping';
$get_field_list = $this->services
->snpGetFieldList($entity_type, $selected_content_type, $type);
// Add HelpText to the mapping form.
$form['helptext'] = [
'#theme' => 'mapping_help_text_info',
'#fields' => [
'filepath' => $filepath,
],
];
// Add theme table to the mapping form.
$form['mapping_form']['#theme'] = 'simple_node_import_table';
// Mapping form.
foreach ($get_field_list as $key => $field) {
// code...
if (method_exists($field
->getLabel(), 'render')) {
$form['mapping_form'][$key] = [
'#type' => 'select',
'#title' => $field
->getLabel()
->render(),
'#options' => $headers,
'#empty_option' => $this
->t('Select'),
'#empty_value' => '',
];
}
else {
$field_name = $field
->getName();
$field_label = $field
->getLabel();
$field_info = FieldStorageConfig::loadByName('node', $field_name);
if (!empty($field_info) && ($field_info
->get('cardinality') == -1 || $field_info
->get('cardinality') > 1)) {
$form['mapping_form'][$key] = [
'#type' => 'select',
'#title' => $field_label,
'#options' => $headers,
'#multiple' => TRUE,
'#required' => $field
->isRequired() ? TRUE : FALSE,
'#empty_option' => $this
->t('Select'),
'#empty_value' => '',
];
}
else {
$form['mapping_form'][$key] = [
'#type' => 'select',
'#title' => $field_label,
'#options' => $headers,
'#required' => $field
->isRequired() ? TRUE : FALSE,
'#empty_option' => $this
->t('Select'),
'#empty_value' => '',
];
}
}
}
// Get the preselected values for form fields.
$form = $this->services
->simpleNodeImporterGetPreSelectedValues($form, $headers);
$form['snp_nid'] = [
'#type' => 'hidden',
'#value' => $node
->id(),
];
$form['import'] = [
'#type' => 'submit',
'#value' => $this
->t('Import'),
'#weight' => 49,
];
$parameters = [
'option' => $option,
'node' => $node
->id(),
];
$this->sessionVariable
->set('parameters', $parameters);
$form['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('cancel'),
'#weight' => 3,
'#submit' => [
'::snpRedirectToCancel',
],
];
return $form;
}
}