marketo_suite-1.0.x-dev/src/Plugin/SubmissionBehavior/Redirect.php

src/Plugin/SubmissionBehavior/Redirect.php
<?php

namespace Drupal\e3_marketo\Plugin\SubmissionBehavior;

use Drupal\Core\Url;
use Drupal\e3_marketo\Entity\MarketoFormEntityInterface;
use Drupal\e3_marketo\Plugin\SubmissionBehaviorBase;

/**
 * Redirect submission behavior.
 *
 * @SubmissionBehavior(
 *   id = "msb_redirect",
 *   label = @Translation("Redirect to a page"),
 *   description = @Translation("Redirect user to specified page on submission."),
 *   weight = 2,
 * )
 */
class Redirect extends SubmissionBehaviorBase {

  /**
   * {@inheritdoc}
   */
  public function getSubmissionCallbacks(MarketoFormEntityInterface $marketo_form = NULL, array $extra_data = []) : array {
    // Check if redirect URL value has already been directly provided.
    if (!empty($extra_data['redirect_url'])) {
      $redirect_url = $extra_data['redirect_url'];

      if (str_starts_with($redirect_url, '/')) {
        $redirect_url = Url::fromUri("internal:{$redirect_url}")->setAbsolute()->toString();
      }
    }

    // Check for redirect URL on the parent entity.
    if (empty($redirect_url) && !empty($marketo_form->_referringItem)) {
      $parent_entity = $marketo_form->_referringItem->getEntity();

      if ($parent_entity && $parent_entity->hasField('field_redirect_url')) {
        $redirect_uri = $parent_entity->get('field_redirect_url')->getString();

        if ($redirect_uri) {
          $redirect_url = Url::fromUri($redirect_uri)->setAbsolute()->toString();
        }
      }
    }

    // If URL wasn't provided by the parent entity, look for one on form.
    if (empty($redirect_url) && $marketo_form->hasField('field_redirect_url')) {
      $redirect_uri = $marketo_form->get('field_redirect_url')->getString();

      if ($redirect_uri) {
        $redirect_url = Url::fromUri($redirect_uri)->setAbsolute()->toString();
      }
    }

    return [
      'redirectToPage' => $redirect_url ?? '/',
    ];
  }

}

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

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