class ContentSelectForm in Drupal-to-Drupal data migration 8.3
Simple wizard step form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\migrate_d2d_ui\Form\DrupalMigrateForm
- class \Drupal\migrate_d2d_ui\Form\ContentSelectForm
- class \Drupal\migrate_d2d_ui\Form\DrupalMigrateForm
Expanded class hierarchy of ContentSelectForm
File
- migrate_d2d_ui/
src/ Form/ ContentSelectForm.php, line 12
Namespace
Drupal\migrate_d2d_ui\FormView source
class ContentSelectForm extends DrupalMigrateForm {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'migrate_d2d_content_select_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Start clean in case we came here via Previous.
$cached_values = $form_state
->getTemporaryValue('wizard');
$form_state
->setTemporaryValue('wizard', $cached_values);
$type_count = $this
->connection($form_state)
->select('node_type', 't')
->fields('t', [
'type',
'name',
])
->countQuery()
->execute()
->fetchField();
if (!$type_count) {
$form['description'] = [
'#markup' => $this
->t('There is no node data to be migrated from the source database.'),
];
return $form;
}
$form['#tree'] = TRUE;
$form['description'] = [
'#markup' => $this
->t('For each content type on the source site, choose the destination site content type to import its content. You may also choose not to import a given content type, or to create a content type based on the source configuration.'),
];
$base_options = [
'-1' => t('--Do not import--'),
'0' => t('--Create content type--'),
];
$node_options = [];
$local_types = NodeType::loadMultiple();
foreach ($local_types as $type => $info) {
$node_options[$type] = $info
->get('name');
}
asort($node_options);
$result = $this
->connection($form_state)
->select('node_type', 't')
->fields('t', [
'type',
'name',
])
->orderBy('name')
->execute();
foreach ($result as $row) {
$options = $base_options + $node_options;
// If we have a match on type name, default the mapping to that match
// and remove the option to create a new type of that name.
if (isset($node_options[$row->type])) {
$default_value = $row->type;
unset($options['0']);
}
else {
$default_value = '-1';
}
$node_counts[$row->type] = $this
->connection($form_state)
->select('node', 'n')
->condition('type', $row->type)
->countQuery()
->execute()
->fetchField();
// But, always default to do-not-import if there are no nodes.
if ($node_counts[$row->type] == 0) {
$default_value = '-1';
}
if ($node_counts[$row->type] > 0) {
$title = $this
->t('@name (@count)', [
'@name' => $row->name,
'@count' => $this
->getStringTranslation()
->formatPlural($node_counts[$row->type], '1 node', '@count nodes'),
]);
$form['content_types'][$row->type] = array(
'#type' => 'select',
'#title' => $title,
'#options' => $options,
'#default_value' => $default_value,
);
}
}
// Build select list from destination formats.
$base_options = [
'-1' => t('--Do not import--'),
'0' => t('--Create format--'),
];
// Destination formats
$format_options = [];
foreach (filter_formats() as $format_id => $format) {
$format_options[$format_id] = $format
->get('name');
}
if ($cached_values['version'] == '7') {
$table = 'filter_format';
}
else {
$table = 'filter_formats';
}
$result = $this
->connection($form_state)
->select($table, 'f')
->fields('f', [
'format',
'name',
])
->execute();
$form['format_overview'] = [
'#markup' => $this
->t('For each text format on the legacy site, choose whether to ignore that format, or to assign a different format to content with that legacy format.'),
];
foreach ($result as $row) {
$options = $base_options + $format_options;
// If we have a match on format name, default the mapping to that match.
if ($match = $this
->caseArraySearch($row->name, $format_options)) {
$default_value = $match;
unset($options['0']);
}
else {
$default_value = '-1';
}
$form['formats'][$row->format] = [
'#type' => 'select',
'#title' => $row->name,
'#options' => $options,
'#default_value' => $default_value,
];
}
return $form;
}
/**
* Case-insensitively search for a value in an array.
*
* @param $value
* @param $array
*/
protected function caseArraySearch($needle, $haystack) {
foreach ($haystack as $key => $value) {
if (!strcasecmp($needle, $value)) {
return $key;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$cached_values['content_types'] = $form_state
->getValue('content_types');
$cached_values['formats'] = $form_state
->getValue('formats');
$form_state
->setTemporaryValue('wizard', $cached_values);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentSelectForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ContentSelectForm:: |
protected | function | Case-insensitively search for a value in an array. | |
ContentSelectForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ContentSelectForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ContentSelectForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DrupalMigrateForm:: |
protected | property | Cached database connection. | |
DrupalMigrateForm:: |
protected | function | Gets the database connection for the source Drupal database. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |