class PoolExport in CMS Content Sync 2.1.x
Same name and namespace in other branches
- 8 src/Controller/PoolExport.php \Drupal\cms_content_sync\Controller\PoolExport
- 2.0.x src/Controller/PoolExport.php \Drupal\cms_content_sync\Controller\PoolExport
Push changes controller.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\cms_content_sync\Controller\PoolExport
Expanded class hierarchy of PoolExport
2 files declare their use of PoolExport
- CliService.php in src/
Cli/ CliService.php - JsonForm.php in src/
Form/ JsonForm.php
File
- src/
Controller/ PoolExport.php, line 16
Namespace
Drupal\cms_content_sync\ControllerView source
class PoolExport extends ControllerBase {
/**
* Export pool.
*
* @param string $cms_content_sync_pool
* The id of the pool
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \EdgeBox\SyncCore\Exception\SyncCoreException
* @throws \Exception
*
* @return null|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function export(string $cms_content_sync_pool, Request $request) : RedirectResponse {
/**
* @var \Drupal\cms_content_sync\Entity\Pool $pool
*/
$pool = \Drupal::entityTypeManager()
->getStorage('cms_content_sync_pool')
->load($cms_content_sync_pool);
$messenger = \Drupal::messenger();
if (!PoolExport::validateBaseUrl($pool)) {
return new RedirectResponse(Url::fromRoute('entity.cms_content_sync_pool.collection')
->toString());
}
$exporter = new SyncCorePoolExport($pool);
$sites = $exporter
->verifySiteId();
if ('false' === $request->query
->get('force', 'false') && $sites && count($sites)) {
$url = Url::fromRoute('entity.cms_content_sync_pool.export', [
'cms_content_sync_pool' => $pool
->id(),
], [
'query' => [
'force' => 'true',
],
])
->toString();
$messenger
->addMessage($this
->t('This site id is not unique, site with id %id is already registered with base url %base_url. If you changed the site URL and want to force the export, <a href="@url">click here</a>.', [
'@url' => Markup::create($url),
'%id' => array_keys($sites)[0],
'%base_url' => array_values($sites)[0],
]), $messenger::TYPE_ERROR);
return new RedirectResponse(Url::fromRoute('entity.cms_content_sync_pool.collection')
->toString());
}
$batch = $exporter
->prepareBatch(false, !empty($_GET['force']) && 'true' === $_GET['force']);
$operations = [];
for ($i = 0; $i < $batch
->count(); ++$i) {
$operations[] = [
[
$batch
->get($i),
'execute',
],
[],
];
}
$batch = [
'title' => t('Export configuration'),
'operations' => $operations,
'finished' => '\\Drupal\\cms_content_sync\\Controller\\PoolExport::batchFinished',
];
batch_set($batch);
return batch_process(Url::fromRoute('entity.cms_content_sync_pool.collection'));
}
/**
* Ensure that the sites base url does not contain localhost or an IP.
*
* @param object $pool
* The pool object
*
* @throws \Exception
*
* @return bool
* Returns true or false
*/
public static function validateBaseUrl($pool) : bool {
$messenger = \Drupal::messenger();
$settings = ContentSyncSettings::getInstance();
$baseUrl = $settings
->getSiteBaseUrl(true);
if (false !== strpos($pool->backend_url, 'cms-content-sync.io')) {
if (false !== strpos($baseUrl, 'localhost') || !UrlHelper::isValid($baseUrl, true) || '/' === mb_substr($baseUrl, -1) || preg_match('@https?://[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+(/|$)@', $baseUrl)) {
$messenger
->addMessage(t('The site does not have a valid base url. The base url must not contain "localhost", does not contain a trailing slash and is not allowed to be an IP address. The base url of the site can be configured <a href="@url">here.</a>', [
'@url' => Url::fromRoute('cms_content_sync.settings_form')
->toString(),
]), $messenger::TYPE_ERROR);
return false;
}
}
return true;
}
/**
* Batch export finished callback.
*
* @param $success
* @param $results
* @param $operations
*/
public static function batchFinished($success, $results, $operations) {
if ($success) {
$message = t('Pool has been exported.');
}
else {
$message = t('Pool export failed.');
}
\Drupal::messenger()
->addMessage($message);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
46 |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage 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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PoolExport:: |
public static | function | Batch export finished callback. | |
PoolExport:: |
public | function | Export pool. | |
PoolExport:: |
public static | function | Ensure that the sites base url does not contain localhost or an IP. | |
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. |