View source
<?php
namespace Drupal\cms_content_sync_migrate_acquia_content_hub\Form;
use Drupal\acquia_contenthub\EntityManager;
use Drupal\cms_content_sync\Entity\Pool;
use Drupal\cms_content_sync\Form\PoolForm;
use EdgeBox\SyncCore\Interfaces\IApplicationInterface;
use Drupal\cms_content_sync_migrate_acquia_content_hub\CreateStatusEntities;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MigrationBase extends FormBase {
protected $acquiaEntityManager;
protected $entityTypeBundleInfo;
protected $fieldTypePluginManager;
protected $configFactory;
protected $moduleHandler;
protected $entityTypeManager;
public static function getTermsFromFilter($tags) {
if (empty($tags)) {
return [];
}
$uuids = explode(',', $tags);
$tags = [];
foreach ($uuids as $uuid) {
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'uuid' => $uuid,
]);
if (!count($terms)) {
\Drupal::messenger()
->addMessage('Term ' . $uuid . ' could not be loaded and has been ignored.', 'warning');
continue;
}
$term = reset($terms);
$tags[] = $term;
}
return $tags;
}
public static function createPools($pools, $backend_url, $authentication_type) {
foreach ($pools as $machine_name => $name) {
Pool::createPool($name, $machine_name, $backend_url, $authentication_type);
}
\Drupal::messenger()
->addMessage('Content Sync Pools have been created.');
}
public function getFormId() {
return 'cms_content_sync_migrate_acquia_content_hub.migration_base';
}
public function __construct(EntityManager $acquia_entity_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, FieldTypePluginManagerInterface $field_type_plugin_manager, ConfigFactoryInterface $config_factory, ModuleHandler $module_handler, EntityTypeManager $entity_type_manager) {
$this->acquiaEntityManager = $acquia_entity_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->fieldTypePluginManager = $field_type_plugin_manager;
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('acquia_contenthub.entity_manager'), $container
->get('entity_type.bundle.info'), $container
->get('plugin.manager.field.field_type'), $container
->get('config.factory'), $container
->get('module_handler'), $container
->get('entity_type.manager'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['backend_url'] = [
'#type' => 'url',
'#title' => $this
->t('Sync Core URL'),
'#required' => TRUE,
];
$auth_options = [
IApplicationInterface::AUTHENTICATION_TYPE_COOKIE => $this
->t("Standard (Cookie)"),
];
if ($this->moduleHandler
->moduleExists('basic_auth')) {
$auth_options[IApplicationInterface::AUTHENTICATION_TYPE_BASIC_AUTH] = $this
->t("Basic Auth");
}
$form['authentication_type'] = [
'#type' => 'select',
'#title' => $this
->t('Authentication'),
'#description' => $this
->t(PoolForm::AUTHENTICATION_TYPE_DESCRIPTION),
'#options' => $auth_options,
'#required' => TRUE,
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Create configuration'),
'#button_type' => 'primary',
];
return $form;
}
const DEFAULT_POOL_MACHINE_NAME = 'content';
const DEFAULT_POOL = [
self::DEFAULT_POOL_MACHINE_NAME => 'Content',
];
public function submitForm(array &$form, FormStateInterface $form_state) {
$content_hub_filter = '';
if (isset($this->content_hub_filter)) {
$content_hub_filter = $this->content_hub_filter;
}
if (!isset($this->migrationType)) {
return;
}
MigratePush::createPools(self::DEFAULT_POOL, $form_state
->getValue('backend_url'), $form_state
->getValue('authentication_type'));
if ($this->migrationType == 'push') {
$flow = MigratePush::createFlow(MigrationBase::DEFAULT_POOL_MACHINE_NAME, $form_state
->getValue('node_push_behavior'), $form_state
->getValue('pull_updates_behavior'));
}
else {
$flow = MigratePull::createFlow(MigrationBase::DEFAULT_POOL_MACHINE_NAME, $form_state
->getValue('node_push_behavior'), $form_state
->getValue('pull_updates_behavior'), $content_hub_filter);
}
$create_status_entities = new CreateStatusEntities();
$operations = $create_status_entities
->prepare($flow['flow_id'], $flow['flow_configuration'], MigrationBase::DEFAULT_POOL_MACHINE_NAME, $flow['type'], $content_hub_filter ? $content_hub_filter->tags : '');
$batch = [
'title' => t('Creating status entities'),
'operations' => $operations,
];
batch_set($batch);
$route_paramenters = [
'cms_content_sync_flow' => $flow['flow_id'],
];
$form_state
->setRedirect('entity.cms_content_sync_flow.edit_form', $route_paramenters);
}
}