lupus_decoupled-1.x-dev/modules/lupus_decoupled_ce_api/lupus_decoupled_ce_api.module

modules/lupus_decoupled_ce_api/lupus_decoupled_ce_api.module
<?php

/**
 * @file
 * General functions and hook implementations.
 */

use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Implements hook_trusted_redirect_hosts_alter().
 */
function lupus_decoupled_ce_api_trusted_redirect_hosts_alter(array &$trusted_hosts) {
  $frontend_base_urls = \Drupal::service('lupus_decoupled_ce_api.base_url_provider')
    ->getAllFrontendBaseUrls();
  foreach ($frontend_base_urls as $frontend_base_url) {
    if ($frontend_base_url && $trusted_host = parse_url($frontend_base_url, PHP_URL_HOST)) {
      $trusted_hosts[] = $trusted_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';
  // Replace preview submit handler for redirecting to the proper front end.
  // Adding ?auth=1 will be done by LupusPreviewPathProcessor.
  if (isset($form['actions']['preview']['#submit']) && is_array($form['actions']['preview']['#submit'])) {
    $form['actions']['preview']['#submit'][] = '_lupus_decoupled_ce_api_node_preview_submit';
  }
}

/**
 * Prepares preview on the front end to work properly.
 *
 * We need to set the entity context/option on the redirect URL in order
 * to allow our path-processing to take of processing the URL correctly.
 *
 * @param array $form
 *   Form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   Form state object.
 */
function _lupus_decoupled_ce_api_node_preview_submit(array &$form, FormStateInterface $form_state) {
  // Get redirect URL, even if disabled.
  $disabled = $form_state->isRedirectDisabled();
  if ($disabled) {
    $form_state->disableRedirect(FALSE);
  }
  $preview_url = $form_state->getRedirect();
  // Restore disabled flag.
  if ($disabled) {
    $form_state->disableRedirect();
  }

  if ($preview_url && $form_state->getFormObject() instanceof EntityFormInterface) {
    assert($preview_url instanceof Url);
    \Drupal::messenger()->addMessage('setting entity');
    $preview_url->setOption('entity', $form_state->getFormObject()->getEntity());
  }
}

/**
 * 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)) {
    $form_object = $form_state->getFormObject();
    if ($form_object instanceof EntityFormInterface) {
      $url = $form_object->getEntity()->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);
    }
  }
}

/**
 * Implements hook_entity_operation().
 */
function lupus_decoupled_ce_api_entity_operation(EntityInterface $entity) {
  $operations = [];
  $entityType = $entity->getEntityType();
  // Only for entity node.
  if ($entityType->id() === 'node' && \Drupal::currentUser()->hasPermission('use api operation link')) {
    // Build the url.
    $apiBaseUrl = \Drupal::service('lupus_decoupled_ce_api.base_url_provider')
      ->getApiBaseUrl();
    $entityApiUrl = $entity->toUrl()->setOptions([
      'base_url' => $apiBaseUrl,
      'language' => $entity->language(),
    ]);

    $operations['view-api-output'] = [
      'title' => t('View API Output'),
      'weight' => 50,
      'url' => $entityApiUrl,
    ];
  }
  return $operations;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc