You are here

public function FormManglerService::handleFormSubmit in Token Content Access 2.0.x

Same name and namespace in other branches
  1. 8 src/FormManglerService.php \Drupal\tca\FormManglerService::handleFormSubmit()

Handle general aspects of TCA form submission.

(Not specific to node etc.).

Parameters

array $form: The form.

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

File

src/FormManglerService.php, line 335

Class

FormManglerService
Class FormManglerService.

Namespace

Drupal\tca

Code

public function handleFormSubmit(array $form, $form_state) {
  $is_entity_bundle = $form_state
    ->getValue('tca_is_entity_bundle');
  $entity_type_id = $form_state
    ->getValue('tca_entity_type_id');
  $entity_id = $form_state
    ->getValue('tca_entity_id');
  $active = $form_state
    ->getValue('tca_active');
  $forced = !$active ? FALSE : (bool) $form_state
    ->getValue('tca_force');
  $settings = [
    'active' => $active,
    'token' => NULL,
    'public' => $form_state
      ->getValue('tca_public'),
    'force' => $forced,
  ];
  if (!$entity_id) {
    $entity_id = $form_state
      ->getformObject()
      ->getEntity()
      ->id();
    if (!$entity_id) {
      return;
    }
  }
  $triggered_element = $form_state
    ->getTriggeringElement();
  $regenerate = in_array('tca', $triggered_element['#array_parents']);
  $token = NULL;
  $is_new = FALSE;

  // Set token.
  if (!$is_entity_bundle && $active) {
    $tca_settings = $this->tcaSettingsManager
      ->loadSettingsAsConfig($entity_type_id, $entity_id);
    $token = $tca_settings
      ->get('token');
    $is_new = !$token;
    if ($regenerate || $is_new) {
      $token = $this->tcaSettingsManager
        ->generateToken($entity_type_id, $entity_id);
      $form_state
        ->setValue('tca_token', $token);
    }
    $settings['token'] = $token;
  }

  // Doesn't save if Regenearte button was clicked.
  if (!$regenerate) {
    $this->tcaSettingsManager
      ->saveSettings($settings, $entity_type_id, $entity_id);
  }

  // Set message.
  if ($is_new && $token) {
    $entity = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->load($entity_id);
    $entity_url = $entity
      ->toUrl('canonical', [
      'query' => [
        'tca' => $token,
      ],
      'absolute' => TRUE,
    ])
      ->toString();
    \Drupal::messenger()
      ->addMessage(t('URL to bypass Token Access Control for this entity: @url', [
      '@url' => $entity_url,
    ]));
  }
}