Commit b1a99756 authored by Ciprian Stavovei's avatar Ciprian Stavovei Committed by Petar Bašić
Browse files

Issue #3314800: Allow users to see latest changes in frontend after adding/updating content

parent 5aa4d82c
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@
 * General functions and hook implementations.
 */

use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_trusted_redirect_hosts_alter().
 */
@@ -13,3 +16,32 @@ function lupus_decoupled_ce_api_trusted_redirect_hosts_alter(array &$trusted_hos
    ->getFrontendBaseUrl();
  $trusted_hosts[] = parse_url($frontend_base_url, PHP_URL_HOST);
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function lupus_decoupled_ce_api_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Add new submit handler for adding ?auth=1 after editing a node.
  $form['actions']['submit']['#submit'][] = '_lupus_decoupled_ce_api_form_submit_add_auth';
}

/**
 * Add ?auth=1 after saving a node.
 *
 * @param array $form
 *   Form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   Form state object.
 */
function _lupus_decoupled_ce_api_form_submit_add_auth(array &$form, FormStateInterface $form_state) {
  $url = $form_state->getRedirect();
  if (!empty($url)) {
    $entity = $form_state->getFormObject()->getEntity();
    $url = $entity->toUrl();
    // Add an "auth=1" query-parameter so a static-generated frontend would
    // do an API-call to fetch the latest content. This ensures editors can
    // see their changes immediately when being redirected to the frontend.
    $url->setOption('query', ['auth' => 1]);
    $form_state->setRedirectUrl($url);
  }
}