PushChangesConfirm.php in CMS Content Sync 8
File
src/Form/PushChangesConfirm.php
View source
<?php
namespace Drupal\cms_content_sync\Form;
use Drupal\cms_content_sync\PushIntent;
use Drupal\cms_content_sync\SyncIntent;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class PushChangesConfirm extends ConfirmFormBase {
protected $tempStoreFactory;
protected $storage;
protected $nodes;
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManager $manager) {
$this->tempStoreFactory = $temp_store_factory;
$this->storage = $manager
->getStorage('node');
$this->nodes = $this->tempStoreFactory
->get('node_cms_content_sync_push_changes_confirm')
->get('nodes');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'node_cms_content_sync_push_changes_confirm';
}
public function getQuestion() {
return 'Are you sure you want to push this content? Depending on the amount and complexity of it, this action may take a while.';
}
public function getCancelUrl() {
return new Url('system.admin_content');
}
public function getConfirmText() {
return t('Push');
}
public function buildForm(array $form, FormStateInterface $form_state) {
if (empty($this->nodes)) {
return new RedirectResponse($this
->getCancelUrl()
->setAbsolute()
->toString());
}
$items = [];
foreach ($this->nodes as $node) {
$items[$node
->id()] = $node
->label();
}
$form['nodes'] = [
'#theme' => 'item_list',
'#items' => $items,
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('confirm')) {
$ignored = 0;
foreach ($this->nodes as $node) {
if (!PushIntent::pushEntityFromUi($node, PushIntent::PUSH_MANUALLY, SyncIntent::ACTION_UPDATE)) {
++$ignored;
}
}
\Drupal::messenger()
->addMessage(t('Pushed @count content items.', [
'@count' => count($this->nodes) - $ignored,
]));
if ($ignored) {
\Drupal::messenger()
->addWarning(t('@count content items have been ignored as they\'re not configured to be pushed.', [
'@count' => $ignored,
]));
}
$this
->logger('cms_content_sync')
->notice('Pushed @count content, ignored @ignored.', [
'@count' => count($this->nodes) - $ignored,
'@ignored' => $ignored,
]);
$this->tempStoreFactory
->get('node_cms_content_sync_push_changes_confirm')
->delete('nodes');
}
$form_state
->setRedirect('system.admin_content');
}
}