metatags_quick-4.0.x-dev/modules/metatags_quick_import/metatags_quick_import.admin.inc
modules/metatags_quick_import/metatags_quick_import.admin.inc
<?php
use Drupal\Core\Url;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
/**
* @file upgrade path from the D6 nodewords module
* @author maxiorel <http://drupal.org/user/49016>
* @author valthebald <http://drupal.org/user/239562>
*/
function metatags_quick_upgrade() {
if (Database::getConnection()->schema()->tableExists('nodewords')) {
$moduleHandler = \Drupal::moduleHandler();
$moduleHandler->loadInclude('metatags_quick', 'inc', 'known_tags');
$form['description'] = array(
'#type' => 'markup',
'#markup' => '<p>' . t('This utility will convert existing nodewords to the Drupal 7 fields.') . '</p>'
);
$form['create_fields'] = array(
'#title' => t('Create fields'),
'#description' => t('Create fields if they do not exist.'),
'#type' => 'checkbox',
'#return_value' => '1',
'#default_value' => '1',
);
$fields = \Drupal::state()->get('nodewords_edit', array());
$options = (array_keys(_metatags_quick_known_fields()));
$form['fields_select'] = array(
'#title' => t('Fields to import'),
'#description' => t('Field list is taken from nodewords settings.'),
'#type' => 'checkboxes',
'#required' => TRUE,
'#options' => $options,
'#default_value' => $fields,
);
$form['batch_size'] = array(
'#type' => 'textfield',
'#default_value' => '100',
'#description' => t('How many nodes to process during one iteration'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Convert'),
);
}
else {
$form['description'] = array(
'#markup' => t('Nodewords table not found. Please check your installation'),
);
}
return $form;
}
function metatags_quick_upgrade_submit(array $form, FormStateInterface $form_state) {
// Step 1: Create fields if they don't exist.
$createFields = $form_state->getValue('create_fields');
if ($createFields) {
// Note: we upgrade only node data!
$entityFieldManager = \Drupal::service('entity_type.manager')->getFieldStorageDefinitions('node');
$nodeBundles = \Drupal::service('entity_type.manager')->getStorage('node_type')->loadMultiple();
if (empty($nodeBundles)) {
$form_state->setErrorByName('create_fields', t('No content types found. <a href=":link">Define them first</a>.', array(':link' => Url::fromRoute('node.type_add')->toString())));
return;
}
// Do something with the node bundles and field storage definitions.
$moduleHandler = \Drupal::service('module_handler');
$moduleHandler->loadInclude('metatags_quick', 'inc', 'known_tags');
foreach ($form_state->getValue('fields_select') as $field_name => $value) {
if (!$value) {
continue;
}
foreach ($nodeBundles as $bundle_name => $bundle) {
_metatags_quick_field_attach('node', $bundle_name, $field_name);
}
}
$fields_to_import = array();
foreach($form_state['values']['fields_select'] as $field_name => $value) {
if ($value) {
$fields_to_import[] = $field_name;
}
}
$batch = metatags_quick_upgrade_batch($form_state['values']['batch_size'], $fields_to_import);
$_SESSION['nodewords_iterations'] = count($batch['operations']);
batch_set($batch);
}
function metatags_quick_upgrade_finished($success, $results, $operations) {
if ($success) {
\Drupal::messenger()->addStatus(t('@node_count results processed in @requests iterations.', array('@node_count' => $_SESSION['nodewords_upgrade_total'], '@requests' => $_SESSION['nodewords_iterations'])));
}
else {
$error_operation = reset($operations);
\Drupal::messenger()->addStatus(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
}
unset($_SESSION['nodewords_iterations'], $_SESSION['nodewords_upgrade_total']);
}
function metatags_quick_upgrade_batch($batch_size, $fields_to_import) {
if ($batch_size < 2) {
$batch_size = 1;
}
$num_operations = metatags_quick_get_node_count();
$_SESSION['nodewords_upgrade_total'] = $num_operations;
$_SESSION['nodewords_upgrade_processed'] = 0;
\Drupal::messenger()->addStatus(t('Converting metatags for @num nodes', array('@num' => $num_operations)));
$operations = array();
// TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
// You will need to use `\Drupal\core\Database\Database::getConnection()` if you do not yet have access to the container here.
$nid_result = \Drupal::database()->select('node', 'n')
->fields('n', array('nid'))
->execute();
$nids = array();
foreach ($nid_result as $node) {
$nids[] = $node->nid;
if (count($nids) >= $batch_size) {
$operations[] = array('metatags_quick_convert_metatags', array($nids, $fields_to_import));
$nids = array();
}
}
if (count($nids)) {
$operations[] = array('metatags_quick_convert_metatags', array($nids, $fields_to_import));
}
$batch = array(
'operations' => $operations,
'progress_message' => t('Completed :nodes_completed of :nodes_total', array(
':nodes_completed' => $_SESSION['nodewords_upgrade_processed'], ':nodes_total' => $_SESSION['nodewords_upgrade_total'])),
'finished' => 'metatags_quick_upgrade_finished',
'file' => \Drupal::service('extension.list.module')->getPath('metatags_quick') . '/metatags_quick_import.admin.inc',
);
return $batch;
}
function metatags_quick_convert_metatags($nids, $fields_to_import) {
$processed_nodes = Node::loadMultiple($nids);
// TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
// You will need to use `\Drupal\core\Database\Database::getConnection()` if you do not yet have access to the container here.
$select = \Drupal::database()->select('nodewords', 'n')
->fields('n', array('id', 'name', 'content'))
->condition('n.name', $fields_to_import, 'IN')
->condition('n.id', $nids, 'IN')
->execute();
foreach ($select as $row) {
$nodewords_data[$row->id][$row->name] = unserialize($row->content);
}
foreach ($processed_nodes as $node) {
$_SESSION['nodewords_upgrade_total']++;
$node_changed = FALSE;
foreach ($fields_to_import as $field) {
if (!empty($nodewords_data[$node->nid][$field])
&& !empty($nodewords_data[$node->nid][$field]['value'])) {
$node_changed = TRUE;
$meta_field_name = "meta_$field";
$langcode = empty($node->language) ? LANGUAGE_NONE : $node->language;
if (is_array($nodewords_data[$node->nid][$field]['value'])) {
$meta_field_value = implode(',', array_filter($nodewords_data[$node->nid][$field]['value']));
}
else {
$meta_field_value = $nodewords_data[$node->nid][$field]['value'];
}
$node->{$meta_field_name}[$langcode][0]['metatags_quick'] = substr($meta_field_value, 0, 255);
}
}
if ($node_changed) {
$node->save();
}
}
}
function metatags_quick_get_node_count() {
$query = 'SELECT count(*) as count FROM {node}';
// TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
// You will need to use `\Drupal\core\Database\Database::getConnection()` if you do not yet have access to the container here.
$result = \Drupal::database()->query($query);
foreach ($result as $record) {
$pocet = $record->count;
}
return $pocet;
}
}
