ContentSelectForm.php in WordPress Migrate 8.3
File
wordpress_migrate_ui/src/Form/ContentSelectForm.php
View source
<?php
namespace Drupal\wordpress_migrate_ui\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentSelectForm extends FormBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getFormId() {
return 'wordpress_migrate_content_select_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
unset($cached_values['post']);
unset($cached_values['page']);
$form_state
->setTemporaryValue('wizard', $cached_values);
$form['overview'] = [
'#markup' => $this
->t('WordPress blogs contain two primary kinds of content, blog posts and pages. Here you may choose what types of Drupal nodes to create from each of those content types, or omit one or both from the import entirely.'),
];
$node_types = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$options = [
'' => $this
->t('Do not import'),
];
foreach ($node_types as $node_type => $info) {
$options[$node_type] = $info
->get('name');
}
$form['blog_post_type'] = [
'#type' => 'select',
'#title' => $this
->t('Import WordPress blog posts as'),
'#options' => $options,
];
$form['page_type'] = [
'#type' => 'select',
'#title' => $this
->t('Import WordPress pages as'),
'#options' => $options,
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getValue('blog_post_type') && $form_state
->getValue('page_type')) {
$form_state
->setErrorByName('', $this
->t('Please select at least one content type to import.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$cached_values['post']['type'] = $form_state
->getValue('blog_post_type');
$cached_values['page']['type'] = $form_state
->getValue('page_type');
$form_state
->setTemporaryValue('wizard', $cached_values);
}
}