cms_content_sync_draggableviews.module in CMS Content Sync 2.1.x
File
modules/cms_content_sync_draggableviews/cms_content_sync_draggableviews.module
View source
<?php
use Drupal\cms_content_sync\Controller\PushEntities;
use Drupal\Core\Form\FormStateInterface;
function cms_content_sync_draggableviews_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (strpos($form_id, 'views_form_') === FALSE) {
return;
}
if (empty($form['actions']['save_order'])) {
return;
}
if (!isset($form['draggableviews'][0])) {
return;
}
$form['actions']['save_order_and_push'] = $form['actions']['save_order'];
$form['actions']['save_order_and_push']['#value'] = Drupal::translation()
->translate('Save order and push');
$form['actions']['save_order_and_push']['#submit'][] = 'cms_content_sync_draggableviews_views_submit';
}
function cms_content_sync_draggableviews_views_submit(&$form, FormStateInterface $form_state) {
$input = $form_state
->getUserInput();
$view = $form_state
->getBuildInfo()['args'][0];
$entity_type = $view
->getBaseEntityType();
if (!$entity_type) {
return;
}
$storage = Drupal::entityTypeManager()
->getStorage($entity_type
->id());
$operations = PushEntities::create(Drupal::getContainer());
$operations
->skipUnpushed();
foreach ($input['draggableviews'] as $item) {
$entity = $storage
->load($item['id']);
$operations
->addEntity($entity);
}
$operations
->showSkipped();
if ($operations
->isEmpty()) {
return;
}
$operations
->setCallback('cms_content_sync_draggableviews_batch_pushed');
$operations
->start();
}
function cms_content_sync_draggableviews_batch_pushed($success, $results, $operations) {
if (!$success) {
$message = t('Failed to push items.');
Drupal::messenger()
->addMessage($message);
}
$succeeded = count(array_filter($results));
$failed = count($results) - $succeeded;
Drupal::messenger()
->addMessage(t('%synchronized items have been pushed.', [
'%synchronized' => $succeeded,
]));
if ($failed) {
Drupal::messenger()
->addMessage(t('%synchronized items have not been pushed.', [
'%synchronized' => $failed,
]));
}
}
function cms_content_sync_draggableviews_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter') {
$module = 'cms_content_sync_draggableviews';
$group = $implementations[$module];
unset($implementations[$module]);
$implementations[$module] = $group;
}
}