class MetaTagDownloadForm in Metatag Import Export CSV 8
Defines a form that configures forms module settings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\metatag_import_export_csv\Form\MetaTagDownloadForm
Expanded class hierarchy of MetaTagDownloadForm
1 string reference to 'MetaTagDownloadForm'
File
- src/
Form/ MetaTagDownloadForm.php, line 13
Namespace
Drupal\metatag_import_export_csv\FormView source
class MetaTagDownloadForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'metatag_import_export_download';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Returns entity types.
*/
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;
}
/**
* Ajax callback for entity type field.
*/
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;
}
/**
* Returns bundles.
*/
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;
}
/**
* Returns tags supported by metatag module.
*/
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;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* Gets list of all entity and passes to batch.
*/
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');
}
// TODO: Generalise this.
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
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. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
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. | |
MetaTagDownloadForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
MetaTagDownloadForm:: |
public | function | Returns bundles. | |
MetaTagDownloadForm:: |
public | function | Ajax callback for entity type field. | |
MetaTagDownloadForm:: |
public | function | Returns entity types. | |
MetaTagDownloadForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MetaTagDownloadForm:: |
public | function | Returns tags supported by metatag module. | |
MetaTagDownloadForm:: |
public | function | Gets list of all entity and passes to batch. | |
MetaTagDownloadForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
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. |