You are here

public function PullForm::synchronizeSelectedEntities in Entity Share 8.2

Same name and namespace in other branches
  1. 8.3 modules/entity_share_client/src/Form/PullForm.php \Drupal\entity_share_client\Form\PullForm::synchronizeSelectedEntities()
  2. 8 modules/entity_share_client/src/Form/PullForm.php \Drupal\entity_share_client\Form\PullForm::synchronizeSelectedEntities()

Form submission handler for the 'synchronize' action.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

File

modules/entity_share_client/src/Form/PullForm.php, line 275

Class

PullForm
Form controller to pull entities.

Namespace

Drupal\entity_share_client\Form

Code

public function synchronizeSelectedEntities(array &$form, FormStateInterface $form_state) {
  $selected_entities = $form_state
    ->getValue('entities');
  $selected_entities = array_filter($selected_entities);
  $selected_remote = $form_state
    ->getValue('remote');
  $selected_channel = $form_state
    ->getValue('channel');
  $searched_text = $form_state
    ->getValue('search', '');
  $redirect_parameters = [
    'query' => [
      'remote' => $selected_remote,
      'channel' => $selected_channel,
      'search' => $searched_text,
    ],
  ];
  $get_offset = $this->query
    ->get('offset');
  if (!is_null($get_offset) && is_numeric($get_offset)) {
    $redirect_parameters['query']['offset'] = $get_offset;
  }
  $get_page = $this->query
    ->get('page');
  if (!is_null($get_page) && is_numeric($get_page)) {
    $redirect_parameters['query']['page'] = $get_page;
  }
  $get_sort = $this->query
    ->get('sort', '');
  if (!empty($get_sort)) {
    $redirect_parameters['query']['sort'] = $get_sort;
  }
  $get_order = $this->query
    ->get('order', '');
  if (!empty($get_order)) {
    $redirect_parameters['query']['order'] = $get_order;
  }
  $form_state
    ->setRedirect('entity_share_client.admin_content_pull_form', [], $redirect_parameters);

  // Add the selected UUIDs to the URL.
  // We do not handle offset or limit as we provide a maximum of 50 UUIDs.
  $url = $this->channelsInfos[$selected_channel]['url'];
  $parsed_url = UrlHelper::parse($url);
  $query = $parsed_url['query'];
  $query['filter']['uuid-filter'] = [
    'condition' => [
      'path' => 'id',
      'operator' => 'IN',
      'value' => array_values($selected_entities),
    ],
  ];
  $query = UrlHelper::buildQuery($query);
  $prepared_url = $parsed_url['path'] . '?' . $query;
  $selected_remote = $this->remoteWebsites[$selected_remote];
  $http_client = $this->remoteManager
    ->prepareJsonApiClient($selected_remote);
  $response = $this->requestService
    ->request($http_client, 'GET', $prepared_url);
  $json = Json::decode((string) $response
    ->getBody());
  if (!isset($json['errors'])) {
    $batch = [
      'title' => $this
        ->t('Synchronize entities'),
      'operations' => [
        [
          '\\Drupal\\entity_share_client\\JsonapiBatchHelper::importEntityListBatch',
          [
            $selected_remote,
            EntityShareUtility::prepareData($json['data']),
          ],
        ],
      ],
      'finished' => '\\Drupal\\entity_share_client\\JsonapiBatchHelper::importEntityListBatchBatchFinished',
    ];
    batch_set($batch);
  }
}