JsonForm.php in CMS Content Sync 2.1.x
File
src/Form/JsonForm.php
View source
<?php
namespace Drupal\cms_content_sync\Form;
use Drupal\cms_content_sync\Controller\PoolExport;
use Drupal\cms_content_sync\SyncCoreFlowExport;
use Drupal\cms_content_sync\SyncCoreInterface\SyncCoreFactory;
use Drupal\cms_content_sync\SyncCorePoolExport;
use Drupal\cms_content_sync\Entity\Flow;
use Drupal\cms_content_sync\Entity\Pool;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class JsonForm extends FormBase {
public function getFormId() {
return 'json_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attributes']['style'][] = 'display: none;';
$form['json'] = [
'#title' => 'JSON',
'#type' => 'textarea',
];
$form['action'] = [
'#title' => 'Action',
'#type' => 'radios',
'#options' => [
'save-flow' => 'Save Flow',
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Submit',
'#button_type' => 'primary',
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ('save-flow' === $form_state
->getValue('action')) {
$data = json_decode($form_state
->getValue('json'), TRUE);
$values = $data['values'];
$flows = Flow::getAll(TRUE);
$machine_name = $values['machineName'];
$pools = Pool::getAll();
foreach ($data['pools'] as $pool_definition) {
$pool_machine_name = $pool_definition['machineName'];
if (!isset($pools[$pool_machine_name])) {
$pool = Pool::create([
'id' => $pool_machine_name,
'label' => $pool_definition['name'],
]);
$pool
->save();
$pools[$pool_machine_name] = $pool;
}
}
if (isset($flows[$machine_name])) {
$flow = $flows[$machine_name];
}
else {
$flow = Flow::create([
'variant' => Flow::VARIANT_SIMPLE,
'type' => $values['type'],
'id' => $machine_name,
'name' => $values['name'],
]);
}
$flow
->getController()
->setFormValues($values);
$flow
->save();
foreach (Pool::getAll() as $pool) {
if (!PoolExport::validateBaseUrl($pool)) {
throw new \Exception('The site does not have a valid base url. The base url must not contain "localhost" and is not allowed to be an IP address. The base url of the site can be configured at the CMS Content Sync settings page.');
}
$exporter = new SyncCorePoolExport($pool);
$sites = $exporter
->verifySiteId();
if ($sites && count($sites)) {
throw new \Exception('Another site with id ' . array_keys($sites)[0] . ' and base url ' . array_values($sites)[0] . ' already exists for the pool "' . $pool->id . '"');
}
}
$flow
->getController()
->updateEntityTypeVersions();
$exporter = new SyncCoreFlowExport($flow);
$batch = $exporter
->prepareBatch(FALSE);
$batch
->executeAll();
if ($data['migrateNext']) {
\Drupal::request()->query
->remove('destination');
$form_state
->setRedirect('cms_content_sync.syndication', [], [
'query' => [
'flow' => $flow->id,
'startNew' => 'true',
],
]);
}
else {
$form_state
->setRedirect('entity.cms_content_sync_flow.collection');
}
}
}
}
Classes
Name |
Description |
JsonForm |
Content Sync general settings form. |