public function ContentImportSelectForm::buildForm in GatherContent 8.4
Same name and namespace in other branches
- 8.5 gathercontent_ui/src/Form/ContentImportSelectForm.php \Drupal\gathercontent_ui\Form\ContentImportSelectForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- gathercontent_ui/
src/ Form/ ContentImportSelectForm.php, line 62
Class
- ContentImportSelectForm
- Class ContentImportSelectForm.
Namespace
Drupal\gathercontent_ui\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
if (empty($this->step)) {
$this->step = 1;
}
// Step 1 - select project + select nodes.
// Step 2 - confirm screen.
if ($this->step === 1) {
$created_mapping_ids = Mapping::loadMultiple();
$projects = [];
$mapping_array = [];
$content_types = [];
foreach ($created_mapping_ids as $mapping) {
/** @var \Drupal\gathercontent\Entity\Mapping $mapping */
if ($mapping
->hasMapping()) {
if (!array_key_exists($mapping
->getGathercontentTemplateId(), $content_types)) {
$content_types[$mapping
->getGathercontentTemplateId()] = $mapping
->getContentType();
}
$projects[$mapping
->getGathercontentProjectId()] = $mapping
->getGathercontentProject();
$mapping_array[$mapping
->getGathercontentTemplateId()] = [
'gc_template' => $mapping
->getGathercontentTemplate(),
'ct' => $mapping
->getContentTypeName(),
];
}
}
$form['project'] = [
'#type' => 'select',
'#title' => t('Select project'),
'#options' => $projects,
'#empty_option' => t('- Select -'),
'#required' => TRUE,
'#ajax' => [
'callback' => '::getContentTable',
'wrapper' => 'edit-import',
'method' => 'replace',
'effect' => 'fade',
],
'#default_value' => !empty($this->projectId) ? $this->projectId : 0,
'#description' => t('You can only see projects with mapped templates in the dropdown.'),
];
$form['import'] = [
'#prefix' => '<div id="edit-import">',
'#suffix' => '</div>',
];
if (($form_state
->hasValue('project') || !empty($this->projectId)) && !empty($form_state
->getValue('project'))) {
$form['import']['filter'] = [
'#type' => 'markup',
'#markup' => '<div class="gc-table--filter-wrapper clearfix"></div>',
'#weight' => 0,
];
$form['import']['counter'] = [
'#type' => 'markup',
'#markup' => '<div class="gc-table--counter"></div>',
'#weight' => 1,
];
$project_id = $form_state
->hasValue('project') ? $form_state
->getValue('project') : $this->projectId;
/** @var \Cheppers\GatherContent\DataTypes\Item[] $content */
$content = $this->client
->itemsGet($project_id);
$import_config = $this
->configFactory()
->get('gathercontent.import');
$form['import']['items'] = [
'#tree' => TRUE,
'#type' => 'table',
'#header' => [
'selected' => [
'class' => [
'select-all',
],
'data' => '',
],
'status' => $this
->t('Status'),
'title' => $this
->t('Item Name'),
'updated' => $this
->t('Last updated in GatherContent'),
'template' => $this
->t('GatherContent Template Name'),
'drupal_status' => $this
->t('Import published'),
'menu' => $this
->t('Menu'),
],
'#empty' => $this
->t('No content available.'),
'#weight' => 2,
'#attributes' => [
'class' => [
'tablesorter-enabled',
],
],
'#attached' => [
'library' => [
'gathercontent_ui/tablesorter-mottie',
'gathercontent_ui/filter',
],
'drupalSettings' => [
'gatherContent' => [
'tableSorterOptionOverrides' => [
'headers' => [
'0' => [
'sorter' => FALSE,
],
'5' => [
'sorter' => FALSE,
],
],
],
],
],
],
];
foreach ($content as $item) {
// If template is not empty, we have mapped template and item
// isn't synced yet.
if (!is_null($item->templateId) && $item->templateId != 'null' && isset($mapping_array[$item->templateId])) {
$node_type = NodeType::load($content_types[$item->templateId]);
$selected_boxes = $node_type
->getThirdPartySetting('menu_ui', 'available_menus', [
'main',
]);
$available_menus = [];
foreach ($selected_boxes as $selected_box) {
$available_menus[$selected_box] = $selected_box;
}
$this->items[$item->id] = [
'color' => $item->status->color,
'label' => $item->status->name,
'template' => $mapping_array[$item->templateId]['gc_template'],
'title' => $item->name,
];
$form['import']['items'][$item->id] = [
'#tree' => TRUE,
'selected' => [
'#type' => 'checkbox',
'#title' => $this
->t('Selected'),
'#title_display' => 'invisible',
'#default_value' => !empty($this->nodes[$item->id]),
'#attributes' => [
'class' => [
'gathercontent-select-import-items',
],
],
],
'status' => [
'#wrapper_attributes' => [
'class' => [
'gc-item',
'status-item',
],
],
'color' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => ' ',
'#attributes' => [
'style' => 'width:20px; height: 20px; float: left; margin-right: 5px; background: ' . $item->status->color,
],
],
'label' => [
'#plain_text' => $item->status->name,
],
],
'title' => [
'data' => [
'#type' => 'markup',
'#markup' => $item->name,
],
'#wrapper_attributes' => [
'class' => [
'gc-item',
'gc-item--name',
],
],
],
'updated' => [
'data' => [
'#markup' => date('F d, Y - H:i', strtotime($item->updatedAt->date)),
],
'#wrapper_attributes' => [
'class' => [
'gc-item',
'gc-item-date',
],
],
'#attributes' => [
'data-date' => date('Y-m-d.H:i:s', strtotime($item->updatedAt->date)),
],
],
'template' => [
'data' => [
'#markup' => $mapping_array[$item->templateId]['gc_template'],
],
'#wrapper_attributes' => [
'class' => [
'template-name-item',
],
],
],
'drupal_status' => [
'#type' => 'checkbox',
'#title' => $this
->t('Publish'),
'#title_display' => 'invisible',
'#default_value' => isset($this->drupalStatus[$item->id]) ? $this->drupalStatus[$item->id] : $import_config
->get('node_default_status'),
'#states' => [
'disabled' => [
':input[name="items[' . $item->id . '][selected]"]' => [
'checked' => FALSE,
],
],
],
],
'menu' => [
'#type' => 'select',
'#default_value' => $node_type
->getThirdPartySetting('menu_ui', 'parent'),
'#empty_option' => $this
->t("- Don't create menu item -"),
'#empty_value' => 0,
'#options' => [
-1 => t("Parent being imported"),
] + \Drupal::service('menu.parent_form_selector')
->getParentSelectOptions('', $available_menus),
'#title' => t('Menu'),
'#title_display' => 'invisible',
'#states' => [
'disabled' => [
':input[name="items[' . $item->id . '][selected]"]' => [
'checked' => FALSE,
],
],
],
],
];
}
}
$form['import']['actions']['#type'] = 'actions';
$form['import']['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Next'),
'#button_type' => 'primary',
'#weight' => 10,
];
$form['import']['actions']['back'] = [
'#type' => 'submit',
'#value' => $this
->t('Back'),
'#weight' => 11,
];
}
}
elseif ($this->step === 2) {
$form['title'] = [
'form_title' => [
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => \Drupal::translation()
->formatPlural(count($this->nodes), 'Confirm import selection (@count item)', 'Confirm import selection (@count items)'),
],
'form_description' => [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('Please review your import selection before importing.'),
],
];
$header = [
'status' => t('Status'),
'title' => t('Item name'),
'template' => t('GatherContent Template'),
];
$options = [];
foreach ($this->nodes as $node) {
$options[$node] = [
'status' => [
'data' => [
'color' => [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => ' ',
'#attributes' => [
'style' => 'width:20px; height: 20px; float: left; margin-right: 5px; background: ' . $this->items[$node]['color'],
],
],
'label' => [
'#plain_text' => $this->items[$node]['label'],
],
],
],
'title' => $this->items[$node]['title'],
'template' => $this->items[$node]['template'],
];
}
$form['table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $options,
];
$options = [];
/** @var \Cheppers\GatherContent\DataTypes\Status[] $statuses */
$statuses = $this->client
->projectStatusesGet($this->projectId);
foreach ($statuses as $status) {
$options[$status->id] = $status->name;
}
$form['status'] = [
'#type' => 'select',
'#options' => $options,
'#title' => t('After successful import change status to:'),
'#empty_option' => t("- Don't change status -"),
];
$import_config = $this
->configFactory()
->get('gathercontent.import');
$form['node_update_method'] = [
'#type' => 'radios',
'#required' => TRUE,
'#title' => $this
->t('Content update method'),
'#default_value' => $import_config
->get('node_update_method'),
'#options' => [
NodeUpdateMethod::ALWAYS_CREATE => $this
->t('Always create new Content'),
NodeUpdateMethod::UPDATE_IF_NOT_CHANGED => $this
->t('Create new Content if it has changed since the last import'),
NodeUpdateMethod::ALWAYS_UPDATE => $this
->t('Always update existing Content'),
],
];
$form['node_create_new_revision'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Create new revision'),
'#default_value' => $import_config
->get('node_create_new_revision'),
'#states' => [
'visible' => [
':input[name="node_update_method"]' => [
'value' => NodeUpdateMethod::ALWAYS_UPDATE,
],
],
],
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Import'),
'#button_type' => 'primary',
'#weight' => 10,
];
$form['actions']['back'] = [
'#type' => 'submit',
'#value' => $this
->t('Back'),
'#weight' => 11,
];
}
return $form;
}