class JsonForm in CMS Content Sync 2.1.x
Content Sync general settings form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\cms_content_sync\Form\JsonForm
Expanded class hierarchy of JsonForm
File
- src/
Form/ JsonForm.php, line 17
Namespace
Drupal\cms_content_sync\FormView source
class JsonForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'json_form';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
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'];
// Create any new Pools that don't exist yet.
$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;
}
}
// Update existing or create new Flow.
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();
// Export to the Sync Core
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();
// Conditionally redirect to the migration page.
if ($data['migrateNext']) {
// If this was set, our redirect would be ignored. It's automatically set by Drupal if you hit Edit on the list page.
\Drupal::request()->query
->remove('destination');
$form_state
->setRedirect('cms_content_sync.syndication', [], [
'query' => [
'flow' => $flow->id,
'startNew' => 'true',
],
]);
}
else {
// In this case it's fine if a ?destination query param overwrites our default to go to the Flow list page.
$form_state
->setRedirect('entity.cms_content_sync_flow.collection');
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
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. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
105 |
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. | |
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. | |
JsonForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
JsonForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
JsonForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
JsonForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
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. | 4 |
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. |