You are here

public static function FormManglerService::staticHandleFormAjax in Token Content Access 8

Same name and namespace in other branches
  1. 2.0.x src/FormManglerService.php \Drupal\tca\FormManglerService::staticHandleFormAjax()

Handle general aspects of AJAX form behavior.

(Not specific to node etc.).

Parameters

array $form: The form.

string|int|object $form_state: The form state.

File

src/FormManglerService.php, line 404

Class

FormManglerService
Class FormManglerService.

Namespace

Drupal\tca

Code

public static function staticHandleFormAjax(array $form, $form_state) {
  $token = $form_state
    ->getValue('tca_token');
  $entity_type_id = $form_state
    ->getValue('tca_entity_type_id');
  $entity_id = $form_state
    ->getValue('tca_entity_id');
  if ($entity_id) {
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type_id)
      ->load($entity_id);
    $entity_url = $entity
      ->toUrl('canonical', [
      'query' => [
        'tca' => $token,
      ],
      'absolute' => TRUE,
    ])
      ->toString();
  }
  else {
    $entity_url = t('N/A');
  }
  $response = new AjaxResponse();
  $response
    ->addCommand(new InvokeCommand('[name="tca_token"]', 'val', [
    $token,
  ]));
  $response
    ->addCommand(new InvokeCommand('.form-item-tca-url-copy span', 'text', [
    $entity_url,
  ]));
  $response
    ->addCommand(new InvokeCommand('.js-form-item-tca-clipboardjs .clipboardjs', 'val', [
    $entity_url,
  ]));
  return $response;
}