You are here

public function PullForm::synchronizeSelectedEntities in Entity Share 8

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.2 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 205

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');
  $form_state
    ->setRedirect('entity_share_client.admin_content_pull_form', [], [
    'query' => [
      'remote' => $selected_remote,
      'channel' => $selected_channel,
    ],
  ]);

  // 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' => 'uuid',
      '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);
  $json_response = $http_client
    ->get($prepared_url)
    ->getBody()
    ->getContents();
  $json = Json::decode($json_response);
  if (!isset($json['errors'])) {
    $batch = [
      'title' => $this
        ->t('Synchronize entities'),
      'operations' => [
        [
          '\\Drupal\\entity_share_client\\JsonapiBatchHelper::importEntityListBatch',
          [
            $selected_remote,
            $this->jsonapiHelper
              ->prepareData($json['data']),
          ],
        ],
      ],
      'finished' => '\\Drupal\\entity_share_client\\JsonapiBatchHelper::importEntityListBatchBatchFinished',
    ];
    batch_set($batch);
  }
}