You are here

entity_share_async.module in Entity Share 8.2

Same filename and directory in other branches
  1. 8.3 modules/entity_share_async/entity_share_async.module

File

modules/entity_share_async/entity_share_async.module
View source
<?php

/**
 * @file
 * Hook implementations for the entity share async module.
 */
declare (strict_types=1);
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function entity_share_async_form_entity_share_client_pull_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $async_button = [
    '#type' => 'submit',
    '#value' => t('Import asynchronously'),
    '#button_type' => 'primary',
    '#submit' => [
      '_entity_share_async_form_submit',
    ],
  ];

  // Add buttons.
  if (isset($form['channel_wrapper']['entities_wrapper']['actions_top'])) {
    $form['channel_wrapper']['entities_wrapper']['actions_top']['async'] = $async_button;
  }
  if (isset($form['channel_wrapper']['entities_wrapper']['actions_bottom'])) {
    $form['channel_wrapper']['entities_wrapper']['actions_bottom']['async'] = $async_button;
  }

  // Alter the entities table select.
  if (isset($form['channel_wrapper']['entities_wrapper']['entities'])) {
    $state_storage = \Drupal::state();
    $request = \Drupal::request();
    $async_states = $state_storage
      ->get('entity_share_async.states', []);
    $remote_id = $form_state
      ->getValue('remote', $request
      ->get('remote'));
    $channel_id = $form_state
      ->getValue('channel', $request
      ->get('channel'));
    foreach ($form['channel_wrapper']['entities_wrapper']['entities']['#options'] as $uuid => $values) {
      if (isset($async_states[$remote_id][$channel_id][$uuid])) {
        $form['channel_wrapper']['entities_wrapper']['entities']['#options'][$uuid]['#attributes']['class'] = [
          'entity-share-waiting-sync',
        ];
        $form['channel_wrapper']['entities_wrapper']['entities']['#options'][$uuid]['status'] = t('Waiting for synchronization');
      }
    }
  }
}

/**
 * Submit handler for asynchronous import.
 *
 * @param array $form
 *   The form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The form state.
 */
function _entity_share_async_form_submit(array &$form, FormStateInterface $form_state) {
  $remote_id = $form_state
    ->getValue('remote');
  $channel_id = $form_state
    ->getValue('channel');
  if (!empty($remote_id) && !empty($channel_id)) {
    $searched_text = $form_state
      ->getValue('search', '');
    $selected_entities = $form_state
      ->getValue('entities');
    $selected_entities = array_filter($selected_entities);
    $query = \Drupal::request()->query;
    $redirect_parameters = [
      'query' => [
        'remote' => $remote_id,
        'channel' => $channel_id,
        'search' => $searched_text,
      ],
    ];
    $get_offset = $query
      ->get('offset');
    if (!is_null($get_offset) && is_numeric($get_offset)) {
      $redirect_parameters['query']['offset'] = $get_offset;
    }
    $get_page = $query
      ->get('page');
    if (!is_null($get_page) && is_numeric($get_page)) {
      $redirect_parameters['query']['page'] = $get_page;
    }
    $get_sort = $query
      ->get('sort', '');
    if (!empty($get_sort)) {
      $redirect_parameters['query']['sort'] = $get_sort;
    }
    $get_order = $query
      ->get('order', '');
    if (!empty($get_order)) {
      $redirect_parameters['query']['order'] = $get_order;
    }

    // Redirect back to prefiltered pull form.
    $form_state
      ->setRedirect('entity_share_client.admin_content_pull_form', [], $redirect_parameters);
    if (!empty($selected_entities)) {

      /** @var \Drupal\entity_share_async\Service\QueueHelperInterface $queue_helper */
      $queue_helper = \Drupal::service('entity_share_async.queue_helper');
      $queue_helper
        ->enqueue($remote_id, $channel_id, $selected_entities);
    }
  }
}

Functions