You are here

class WebformShareEmbedForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_share/src/Form/WebformShareEmbedForm.php \Drupal\webform_share\Form\WebformShareEmbedForm

Webform share embed form.

Hierarchy

Expanded class hierarchy of WebformShareEmbedForm

1 string reference to 'WebformShareEmbedForm'
webform_share.routing.yml in modules/webform_share/webform_share.routing.yml
modules/webform_share/webform_share.routing.yml

File

modules/webform_share/src/Form/WebformShareEmbedForm.php, line 15

Namespace

Drupal\webform_share\Form
View source
class WebformShareEmbedForm extends FormBase {

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

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

  /**
   * WebformEntityReferenceLinkFormatter constructor.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param \Drupal\webform\WebformRequestInterface $request_handler
   *   The webform request handler.
   */
  public function __construct(RendererInterface $renderer, WebformRequestInterface $request_handler) {
    $this->renderer = $renderer;
    $this->requestHandler = $request_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('renderer'), $container
      ->get('webform.request'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'webform_share_embed_form';
  }

  /**
   * Form constructor.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The form structure.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $webform = $this->requestHandler
      ->getCurrentWebform();
    $source_entity = $this->requestHandler
      ->getCurrentSourceEntity([
      'webform',
    ]);
    $form['info'] = [
      '#type' => 'webform_message',
      '#message_message' => $this
        ->t('Choose how you want to embed the webform and then copy-n-paste the below code snippet directly into the HTML source of any webpage.'),
      '#message_type' => 'info',
      '#message_close' => TRUE,
      '#message_storage' => WebformMessage::STORAGE_SESSION,
    ];
    $form['type'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Embed type'),
      '#title_display' => 'invisible',
      '#options' => [
        'script' => $this
          ->t('JavaScript -- The embedded webform will be inserted using JavaScript.'),
        'resizing' => $this
          ->t('Resizing iframe -- The embedded webform will be responsive and adjusted to fit within the page using an iframe and JavaScript.'),
        'fixed' => $this
          ->t('Fixed iframe -- The embedded webform will be a fixed size on the page using an iframe with a scrollbar.'),
      ],
      '#options_display' => 'buttons',
      '#options_description_display' => 'help',
      '#default_value' => 'script',
    ];
    $types = [
      'script' => [
        'type' => 'script',
        'title' => $this
          ->t('JavaScript code'),
      ],
      'resizing' => [
        'type' => 'iframe',
        'title' => $this
          ->t('Resizing iframe code'),
        'javascript' => TRUE,
      ],
      'fixed' => [
        'type' => 'iframe',
        'title' => $this
          ->t('Fixed iframe code'),
      ],
    ];
    foreach ($types as $type => $item) {
      $build = [
        '#type' => 'webform_share_' . $item['type'],
        '#webform' => $webform,
        '#source_entity' => $source_entity,
        '#query' => $this
          ->getRequest()->query
          ->all(),
      ];
      if (!empty($item['javascript'])) {
        $build['#javascript'] = $item['javascript'];
      }
      $code = trim((string) $this->renderer
        ->renderPlain($build));
      $form[$type] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'js-webform-share-admin-copy',
          ],
        ],
        '#states' => [
          'visible' => [
            ':input[name="type"]' => [
              'value' => $type,
            ],
          ],
        ],
      ];
      $form[$type]['code'] = [
        '#type' => 'webform_codemirror',
        '#title' => $item['title'],
        '#mode' => 'html',
        '#default_value' => $code,
      ];
      $form[$type]['copy'] = [
        '#type' => 'button',
        '#value' => $this
          ->t('Copy code'),
      ];
      $form[$type]['message'] = [
        '#markup' => $this
          ->t('Code copied to clipboard…'),
        '#prefix' => '<strong class="webform-share-admin-copy-message">',
        '#suffix' => '</strong>',
      ];
    }
    $form['#attached']['library'][] = 'webform_share/webform_share.admin';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Do nothing.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator 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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
WebformShareEmbedForm::$renderer protected property The renderer.
WebformShareEmbedForm::$requestHandler protected property Webform request handler.
WebformShareEmbedForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
WebformShareEmbedForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
WebformShareEmbedForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
WebformShareEmbedForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
WebformShareEmbedForm::__construct public function WebformEntityReferenceLinkFormatter constructor.