cms_content_sync-3.0.x-dev/modules/cms_content_sync_draggableviews/cms_content_sync_draggableviews.module
modules/cms_content_sync_draggableviews/cms_content_sync_draggableviews.module
<?php
/**
* @file
* Module file for cms_content_sync_draggableviews.
*/
use Drupal\cms_content_sync\Controller\PushEntities;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_alter().
*
* {@inheritdoc}
*/
function cms_content_sync_draggableviews_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Filter the right form.
if (FALSE === strpos($form_id, 'views_form_')) {
return;
}
if (empty($form['actions']['save_order'])) {
return;
}
// If there is no results remove the save-order button.
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';
}
/**
* Submit handler.
*
* @param mixed $form
* The form object.
*/
function cms_content_sync_draggableviews_views_submit(&$form, FormStateInterface $form_state) {
$input = $form_state->getUserInput();
/** @var \Drupal\views\ViewExecutable $view */
$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();
}
/**
* Show success or failed message after pushing the related entities.
*
* @param bool $success
* Indicate that the batch API tasks were all completed successfully.
* @param array $results
* An array of all the results that were updated.
* @param array $operations
* A list of all the operations that had not been completed by the
* batch API.
*/
function cms_content_sync_draggableviews_batch_pushed($success, array $results, array $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]));
}
}
/**
* Implements hook_module_implements_alter().
*
* {@inheritdoc}
*/
function cms_content_sync_draggableviews_module_implements_alter(&$implementations, $hook) {
if ('form_alter' == $hook) {
$module = 'cms_content_sync_draggableviews';
$group = $implementations[$module];
unset($implementations[$module]);
$implementations[$module] = $group;
}
}
