Skip to content
Snippets Groups Projects
Commit 464f5ec9 authored by Lasha badashvili's avatar Lasha badashvili
Browse files

Add plugin derivatives

parent 63df214c
No related branches found
No related tags found
No related merge requests found
......@@ -3,4 +3,3 @@ type: module
description: 'Import content from CSV.'
package: CSV
core_version_requirement: ^10 || ^11
configure: csv_importer.settings
csv_importer.form:
title: CSV importer
description: 'Import data from CSV.'
parent: system.admin_config_development
route_name: csv_importer.form
csv_importer.import:
title: Import CSV
route_name: csv_importer.form
base_route: system.admin_content
weight: 10
csv_importer.form:
path: '/admin/config/development/csv-importer'
path: '/admin/content/csv-importer'
defaults:
_form: '\Drupal\csv_importer\Form\ImporterForm'
_title: 'Import CSV'
......
......@@ -130,7 +130,6 @@ class ImporterForm extends FormBase {
];
if ($entity_type = $form_state->getValue('entity_type')) {
if ($options = $this->getEntityTypeBundleOptions($entity_type)) {
$form['importer']['entity_type_bundle'] = [
'#type' => 'select',
......@@ -139,24 +138,6 @@ class ImporterForm extends FormBase {
'#required' => TRUE,
'#weight' => 5,
];
}
$options = $this->getEntityTypeImporterOptions($entity_type);
$form['importer']['plugin_id'] = [
'#type' => 'hidden',
'#value' => key($options),
];
if (count($options) > 1) {
$form['importer']['plugin_id'] = [
'#type' => 'select',
'#title' => $this->t('Select importer'),
'#options' => $options,
'#default_value' => 0,
'#weight' => 10,
];
}
$form['importer']['delimiter'] = [
......@@ -179,7 +160,7 @@ class ImporterForm extends FormBase {
'#title' => $this->t('Select CSV file'),
'#required' => TRUE,
'#autoupload' => TRUE,
'#upload_validators' => ['file_validate_extensions' => ['csv']],
'#upload_validators' => ['FileExtension' => ['extensions' => 'csv']],
'#weight' => 10,
];
......@@ -210,8 +191,8 @@ class ImporterForm extends FormBase {
$options = [];
$plugin_definitions = $this->importer->getDefinitions();
foreach ($plugin_definitions as $definition) {
$entity_type = $definition['entity_type'];
foreach ($plugin_definitions as $id => $definition) {
$entity_type = $definition['entity_type'] ?? '';
if ($this->entityTypeManager->hasDefinition($entity_type)) {
$entity = $this->entityTypeManager->getDefinition($entity_type);
$options[$entity_type] = $entity->getLabel();
......@@ -247,30 +228,6 @@ class ImporterForm extends FormBase {
return $options;
}
/**
* Get entity importer plugin options.
*
* @param string $entity_type
* Entity type.
*
* @return array
* Entity importer plugin options.
*/
protected function getEntityTypeImporterOptions(string $entity_type) {
$plugin_definitions = $this->importer->getDefinitions();
$entity_type_importers = array_keys(array_combine(array_keys($plugin_definitions), array_column($plugin_definitions, 'entity_type')), $entity_type);
if ($entity_type_importers && is_array($entity_type_importers)) {
$plugin_definitions = array_intersect_key($plugin_definitions, array_flip($entity_type_importers));
foreach ($plugin_definitions as $plugin_id => $plugin_defintion) {
$options[$plugin_id] = $plugin_defintion['label'];
}
}
return $options;
}
/**
* Get entity type fields.
*
......@@ -359,7 +316,7 @@ class ImporterForm extends FormBase {
$this->messenger()->addError($this->t('Your CSV has missing required fields: @fields', ['@fields' => $this->renderer->render($render)]));
}
else {
$this->importer->createInstance($form_state->getUserInput()['plugin_id'], [
$this->importer->createInstance('importer:' . $entity_type, [
'csv' => $csv_parse,
'csv_entity' => $this->parser->getCsvEntity($csv),
'entity_type' => $entity_type,
......
<?php
namespace Drupal\csv_importer\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Derives importer(s) for all content entity types.
*/
class ImporterDeriver extends DeriverBase implements ContainerDeriverInterface {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs the ImporterDeriver object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// Load all content entity types.
$entity_types = $this->entityTypeManager->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($entity_type->getGroup() === 'content') {
$this->derivatives[$entity_type_id] = $base_plugin_definition + [
'entity_type' => $entity_type_id,
];
$this->derivatives[$entity_type_id]['admin_label'] = t('Entity Type importer: @label', [
'@label' => $entity_type->getLabel(),
]);
}
}
return $this->derivatives;
}
}
<?php
namespace Drupal\csv_importer\Plugin\Importer;
use Drupal\csv_importer\Plugin\ImporterBase;
/**
* Class to import comments.
*
* @Importer(
* id = "comment_importer",
* entity_type = "comment",
* label = @Translation("Comment importer")
* )
*/
class CommentImporter extends ImporterBase {}
......@@ -8,9 +8,9 @@ use Drupal\csv_importer\Plugin\ImporterBase;
* Class to import nodes.
*
* @Importer(
* id = "node_importer",
* entity_type = "node",
* label = @Translation("Node importer")
* id = "importer",
* label = @Translation("Importer"),
* deriver = "Drupal\csv_importer\Plugin\Derivative\ImporterDeriver"
* )
*/
class NodeImporter extends ImporterBase {}
class Importer extends ImporterBase {}
<?php
namespace Drupal\csv_importer\Plugin\Importer;
use Drupal\csv_importer\Plugin\ImporterBase;
/**
* Class to import menu links.
*
* @Importer(
* id = "menu_link_content_importer",
* entity_type = "menu_link_content",
* label = @Translation("Menu link importer")
* )
*/
class MenuLinkImporter extends ImporterBase {}
<?php
namespace Drupal\csv_importer\Plugin\Importer;
use Drupal\csv_importer\Plugin\ImporterBase;
/**
* Class to import taxonomy terms.
*
* @Importer(
* id = "taxonomy_term_importer",
* entity_type = "taxonomy_term",
* label = @Translation("Taxonomy importer")
* )
*/
class TaxonomyImporter extends ImporterBase {}
<?php
namespace Drupal\csv_importer\Plugin\Importer;
use Drupal\csv_importer\Plugin\ImporterBase;
/**
* Class to import users.
*
* @Importer(
* id = "user_importer",
* entity_type = "user",
* label = @Translation("User importer")
* )
*/
class UserImporter extends ImporterBase {}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment