You are here

class WebformShareController in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_share/src/Controller/WebformShareController.php \Drupal\webform_share\Controller\WebformShareController

Provides route responses for webform share page, script, and embed.

Hierarchy

Expanded class hierarchy of WebformShareController

File

modules/webform_share/src/Controller/WebformShareController.php, line 16

Namespace

Drupal\webform_share\Controller
View source
class WebformShareController extends ControllerBase {

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The webform message manager.
   *
   * @var \Drupal\webform\WebformMessageManagerInterface
   */
  protected $messageManager;

  /**
   * The webform request handler.
   *
   * @var \Drupal\webform\WebformRequestInterface
   */
  protected $requestHandler;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->renderer = $container
      ->get('renderer');
    $instance->messageManager = $container
      ->get('webform.message_manager');
    $instance->requestHandler = $container
      ->get('webform.request');
    return $instance;
  }

  /**
   * Returns a webform to be shared as the page of an iframe.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object.
   * @param string|null $library
   *   The iframe JavaScript library.
   * @param string|null $version
   *   The iframe JavaScript library version.
   *
   * @return array
   *   The webform rendered in a page template with only the content.
   *
   * @see \Drupal\webform_share\Theme\WebformShareThemeNegotiator
   * @see page--webform-share.html.twig
   * @see webform_share.libraries.yml
   */
  public function page(Request $request, $library = NULL, $version = NULL) {
    $webform = $this->requestHandler
      ->getCurrentWebform();
    $source_entity = $this->requestHandler
      ->getCurrentSourceEntity([
      'webform',
    ]);
    $build = [];

    // Webform.
    $build['webform'] = [
      '#type' => 'webform',
      '#webform' => $webform,
      '#source_entity' => $source_entity,
      '#prefix' => '<div class="webform-share-submission-form">',
      '#suffix' => '</div>',
    ];

    // Attachments.
    $build['#attached']['library'][] = 'webform_share/webform_share.page';
    if ($library && $version) {
      $build['#attached']['library'][] = "webform_share/libraries.{$library}.{$version}";
    }

    // Add setting notifying AjaxCommand that this page is shared via an
    // embeddded iframe.
    // @see Drupal.AjaxCommands.prototype.webformRefresh
    $build['#attached']['drupalSettings']['webform_share']['page'] = TRUE;
    return $build;
  }

  /**
   * Returns a webform to be shared using (java)script.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object.
   * @param string|null $library
   *   The iframe JavaScript library.
   * @param string|null $version
   *   The iframe JavaScript library version.
   *
   * @return array
   *   The webform rendered in a page template with only the content.
   *
   * @see \Drupal\webform_share\Theme\WebformShareThemeNegotiator
   * @see page--webform-share.html.twig
   * @see webform_share.libraries.yml
   */
  public function script(Request $request, $library = NULL, $version = NULL) {
    $webform = $this->requestHandler
      ->getCurrentWebform();
    $source_entity = $this->requestHandler
      ->getCurrentSourceEntity([
      'webform',
    ]);
    $build = [
      '#type' => 'webform_share_iframe',
      '#webform' => $webform,
      '#source_entity' => $source_entity,
      '#javascript' => TRUE,
      '#query' => $request->query
        ->all(),
    ];
    $iframe = $this->renderer
      ->renderPlain($build);
    $iframe_script = json_encode($iframe);
    $iframe_script = str_replace('src=\\"\\/\\/', 'src=\\"' . $request
      ->getScheme() . ':\\/\\/', $iframe_script);
    $content = 'document.write(' . $iframe_script . ');';
    $response = new CacheableResponse($content, 200, [
      'Content-Type' => 'text/javascript',
    ]);
    $response
      ->addCacheableDependency($webform);
    if ($source_entity) {
      $response
        ->addCacheableDependency($source_entity);
    }
    return $response;
  }

  /**
   * Returns a preview of a webform to be shared.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object.
   *
   * @return array
   *   A render array containing a review of the webform to be shared.
   */
  public function preview(Request $request) {
    $webform = $this->requestHandler
      ->getCurrentWebform();
    $source_entity = $this->requestHandler
      ->getCurrentSourceEntity([
      'webform',
    ]);
    $build = [];
    if ($this
      ->currentUser()
      ->isAuthenticated()) {
      $build['message'] = [
        '#type' => 'webform_message',
        '#message_message' => [
          'message' => [
            '#markup' => $this
              ->t('To test anonymous user access to the below embedded webform, please log out or open the below link in a new private or incognito window.'),
            '#suffix' => '<br/>',
          ],
          'link' => [
            '#type' => 'link',
            '#url' => Url::fromRoute('<current>'),
            '#title' => Url::fromRoute('<current>')
              ->setAbsolute()
              ->toString(),
          ],
        ],
        '#message_type' => 'info',
        '#message_close' => TRUE,
        '#message_storage' => WebformMessage::STORAGE_SESSION,
      ];
    }
    $build['preview'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'webform-share-iframe-container',
        ],
      ],
    ];
    $build['preview']['iframe'] = [
      '#type' => 'webform_share_iframe',
      '#webform' => $webform,
      '#source_entity' => $source_entity,
      '#javascript' => TRUE,
      '#options' => [
        'log' => TRUE,
      ],
      '#query' => $request->query
        ->all(),
    ];
    $build['#attached']['library'][] = 'webform_share/webform_share.admin';
    return $build;
  }

  /**
   * Returns a test of a webform to be shared.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request object.
   *
   * @return array
   *   A render array containing a review of the webform to be shared.
   */
  public function test(Request $request) {
    $webform = $this->requestHandler
      ->getCurrentWebform();
    $source_entity = $this->requestHandler
      ->getCurrentSourceEntity([
      'webform',
    ]);
    $build = [];
    $build['message'] = [
      '#type' => 'webform_message',
      '#message_message' => $this->messageManager
        ->get(WebformMessageManagerInterface::SUBMISSION_TEST),
      '#message_type' => 'warning',
      '#message_close' => TRUE,
      '#message_storage' => WebformMessage::STORAGE_SESSION,
    ];
    $build['test'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'webform-share-iframe-container',
        ],
      ],
    ];
    $build['test']['iframe'] = [
      '#type' => 'webform_share_iframe',
      '#webform' => $webform,
      '#source_entity' => $source_entity,
      '#test' => TRUE,
      '#javascript' => TRUE,
      '#options' => [
        'log' => TRUE,
      ],
      '#query' => $request->query
        ->all(),
    ];
    $build['#attached']['library'][] = 'webform_share/webform_share.admin';
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformShareController::$messageManager protected property The webform message manager.
WebformShareController::$renderer protected property The renderer.
WebformShareController::$requestHandler protected property The webform request handler.
WebformShareController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
WebformShareController::page public function Returns a webform to be shared as the page of an iframe.
WebformShareController::preview public function Returns a preview of a webform to be shared.
WebformShareController::script public function Returns a webform to be shared using (java)script.
WebformShareController::test public function Returns a test of a webform to be shared.