View source
<?php
namespace Drupal\metatag_import_export_csv\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
class MetaTagDownloadForm extends FormBase {
public function getFormId() {
return 'metatag_import_export_download';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['entity_type'] = [
'#title' => $this
->t('Entity type'),
'#type' => 'select',
'#required' => TRUE,
'#options' => $this
->getEntityTypes(),
'#default_value' => isset($this
->getEntityTypes()['node']) ? 'node' : '',
'#ajax' => [
'callback' => '::getBundlesList',
'event' => 'change',
],
];
if ($entity_type = $form_state
->getValue('entity_type')) {
$bundle_options = $this
->getBundles($entity_type);
}
elseif (!empty($form['entity_type']['#default_value'])) {
$bundle_options = $this
->getBundles($form['entity_type']['#default_value']);
}
$form['bundles'] = [
'#title' => $this
->t('Bundle'),
'#type' => 'select',
'#multiple' => "multiple",
'#required' => TRUE,
'#validated' => TRUE,
'#options' => $bundle_options,
];
$form['tags'] = [
'#title' => $this
->t('Tags'),
'#type' => 'select',
'#required' => TRUE,
'#multiple' => "multiple",
'#size' => 30,
'#options' => $this
->getTags(),
];
$form['delimiter'] = [
'#title' => $this
->t('Delimiter'),
'#type' => 'textfield',
'#required' => TRUE,
'#description' => t('Enter the delimiter for CSV.'),
'#default_value' => ',',
'#maxlength' => 2,
];
$form['submit'] = [
'#type' => 'submit',
'#required' => TRUE,
'#value' => $this
->t("Download"),
];
return $form;
}
public function getEntityTypes() {
$entity_types = [];
$entity_type_manager = \Drupal::service('entity_type.manager');
foreach ($entity_type_manager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type
->getGroup() != 'content') {
continue;
}
$entity_types[$entity_type_id] = $entity_type
->getLabel();
}
natcasesort($entity_types);
return $entity_types;
}
public function getBundlesList($form, FormStateInterface &$form_state) {
$ajax_response = new AjaxResponse();
$entity_type = $form_state
->getValue('entity_type');
$form['bundles']['#options'] = $this
->getBundles($entity_type);
$ajax_response
->addCommand(new ReplaceCommand('.form-item-bundles', $form['bundles']));
return $ajax_response;
}
public function getBundles($entity_type_id) {
$bundles = [];
$bundleList = \Drupal::service('entity_type.bundle.info')
->getBundleInfo($entity_type_id);
foreach ($bundleList as $keys => $values) {
$bundles[$keys] = (string) $values['label'];
}
return $bundles;
}
public function getTags() {
$tagList = [];
$metatag_manager = \Drupal::service('metatag.manager');
$tagGroupList = $metatag_manager
->sortedGroupsWithTags();
foreach ($tagGroupList as $keys => $values) {
$tagList[$keys] = $values['tags'];
foreach ($values['tags'] as $tkeys => $tvalues) {
$tagList[$keys][$tkeys] = $tvalues['label'];
}
}
return $tagList;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_type = $form_state
->getValue('entity_type');
$bundles = $form_state
->getValue('bundles');
$tags = $form_state
->getValue('tags');
$delimiter = $form_state
->getValue('delimiter');
$this
->metatagImportExportCsvDownload($entity_type, $bundles, $tags, $delimiter);
}
public function metatagImportExportCsvDownload($entity_type, $bundles, $tags, $delimiter) {
$query = \Drupal::entityQuery($entity_type);
$entity_type_definition = \Drupal::service('entity_type.manager')
->getDefinition($entity_type);
if ($bundle_key = $entity_type_definition
->getKey('bundle')) {
$query
->condition($bundle_key, $bundles, 'IN');
}
switch ($entity_type) {
case 'node':
$query
->sort('created', 'ASC');
break;
}
$nidlist = $query
->execute();
$nidlist = array_values($nidlist);
$total_count = count($nidlist);
$operations = [];
for ($i = 0; $i < $total_count; $i++) {
$operations[] = [
'\\Drupal\\metatag_import_export_csv\\MetatagExport::generateExportCsv',
[
$nidlist[$i],
$entity_type,
$tags,
$delimiter,
],
];
}
$batch = [
'operations' => $operations,
'finished' => '\\Drupal\\metatag_import_export_csv\\MetatagExport::downloadCsv',
'title' => $this
->t('Metatags export'),
'init_message' => $this
->t('Export process is starting.'),
'progress_message' => $this
->t('Processed @current out of @total.'),
'error_message' => $this
->t('Batch has encountered an error.'),
];
batch_set($batch);
}
}