You are here

function entity_share_async_form_entity_share_client_pull_form_alter in Entity Share 8.3

Same name and namespace in other branches
  1. 8.2 modules/entity_share_async/entity_share_async.module \entity_share_async_form_entity_share_client_pull_form_alter()

Implements hook_form_FORM_ID_alter().

File

modules/entity_share_async/entity_share_async.module, line 15

Code

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',
    '#validate' => [
      '::validateSelectedEntities',
    ],
    '#submit' => [
      '::submitForm',
      '_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'));
    $import_configs = \Drupal::entityTypeManager()
      ->getStorage('import_config')
      ->loadMultiple();
    foreach (array_keys($form['channel_wrapper']['entities_wrapper']['entities']['#options']) as $uuid) {
      if (isset($async_states[$remote_id][$channel_id][$uuid])) {
        $import_config_label = t('Undefined');
        $import_config_id = $async_states[$remote_id][$channel_id][$uuid];
        if (isset($import_configs[$import_config_id])) {
          $import_config_label = $import_configs[$import_config_id]
            ->label();
        }
        $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 (import config %import_config)', [
          '%import_config' => $import_config_label,
        ]);
      }
    }
  }
}