You are here

class Webform in Webform 6.x

Same name in this branch
  1. 6.x src/Element/Webform.php \Drupal\webform\Element\Webform
  2. 6.x src/Entity/Webform.php \Drupal\webform\Entity\Webform
  3. 6.x src/Plugin/Condition/Webform.php \Drupal\webform\Plugin\Condition\Webform
Same name and namespace in other branches
  1. 8.5 src/Element/Webform.php \Drupal\webform\Element\Webform

Provides a render element to display a webform.

Plugin annotation

@RenderElement("webform");

Hierarchy

Expanded class hierarchy of Webform

1 file declares its use of Webform
WebformEntityController.php in src/Controller/WebformEntityController.php
47 string references to 'Webform'
block_content.type.webform.yml in tests/modules/webform_test_block_custom/config/install/block_content.type.webform.yml
tests/modules/webform_test_block_custom/config/install/block_content.type.webform.yml
field.field.block_content.webform.webform.yml in tests/modules/webform_test_block_custom/config/install/field.field.block_content.webform.webform.yml
tests/modules/webform_test_block_custom/config/install/field.field.block_content.webform.webform.yml
field.field.node.webform.webform.yml in modules/webform_node/config/optional/field.field.node.webform.webform.yml
modules/webform_node/config/optional/field.field.node.webform.webform.yml
field.field.node.webform_demo_event.webform.yml in modules/webform_demo/webform_demo_event_registration/config/install/field.field.node.webform_demo_event.webform.yml
modules/webform_demo/webform_demo_event_registration/config/install/field.field.node.webform_demo_event.webform.yml
field.field.node.webform_demo_region.webform.yml in modules/webform_demo/webform_demo_region_contact/config/install/field.field.node.webform_demo_region.webform.yml
modules/webform_demo/webform_demo_region_contact/config/install/field.field.node.webform_demo_region.webform.yml

... See full list

4 #type uses of Webform
WebformBlock::build in src/Plugin/Block/WebformBlock.php
Builds and returns the renderable array for this block plugin.
WebformEntityReferenceEntityFormatter::viewElements in src/Plugin/Field/FieldFormatter/WebformEntityReferenceEntityFormatter.php
Builds a renderable array for a field value.
WebformShareController::page in modules/webform_share/src/Controller/WebformShareController.php
Returns a webform to be shared as the page of an iframe.
WebformTestElementController::index in tests/modules/webform_test_element/src/Controller/WebformTestElementController.php
Returns the webform test element page.

File

src/Element/Webform.php, line 16

Namespace

Drupal\webform\Element
View source
class Webform extends RenderElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#pre_render' => [
        [
          $class,
          'preRenderWebformElement',
        ],
      ],
      '#webform' => NULL,
      '#default_data' => [],
      '#action' => NULL,
      '#sid' => NULL,
      '#information' => NULL,
    ];
  }

  /**
   * Webform element pre render callback.
   */
  public static function preRenderWebformElement($element) {
    $webform = $element['#webform'] instanceof WebformInterface ? $element['#webform'] : WebformEntity::load($element['#webform']);
    if (!$webform) {
      return $element;
    }
    if (!empty($element['#sid'])) {
      $webform_submission = WebformSubmission::load($element['#sid']);
      if ($webform_submission && $webform_submission
        ->access('update') && $webform_submission
        ->getWebform()
        ->id() === $webform
        ->id()) {
        $element['webform_build'] = \Drupal::service('entity.form_builder')
          ->getForm($webform_submission, 'edit');
      }
      elseif ($webform
        ->getSetting('form_access_denied') !== WebformInterface::ACCESS_DENIED_DEFAULT) {

        // Set access denied message.
        $element['webform_access_denied'] = static::buildAccessDenied($webform);
      }
      else {
        static::addCacheableDependency($element, $webform);
      }
    }
    else {
      if ($webform
        ->access('submission_create')) {
        $values = [];

        // Set data.
        $values['data'] = $element['#default_data'];

        // Set source entity type and id.
        if (!empty($element['#entity']) && $element['#entity'] instanceof EntityInterface) {
          $values['entity_type'] = $element['#entity']
            ->getEntityTypeId();
          $values['entity_id'] = $element['#entity']
            ->id();
        }
        elseif (!empty($element['#entity_type']) && !empty($element['#entity_id'])) {
          $values['entity_type'] = $element['#entity_type'];
          $values['entity_id'] = $element['#entity_id'];
        }

        // Build the webform.
        $element['webform_build'] = $webform
          ->getSubmissionForm($values);
      }
      elseif ($webform
        ->getSetting('form_access_denied') !== WebformInterface::ACCESS_DENIED_DEFAULT) {

        // Set access denied message.
        $element['webform_access_denied'] = static::buildAccessDenied($webform);
      }
      else {
        static::addCacheableDependency($element, $webform);
      }
    }
    if (isset($element['webform_build'])) {

      // Set custom form submit action.
      if (!empty($element['#action'])) {
        $element['webform_build']['#action'] = $element['#action'];
      }

      // Hide submission information.
      if ($element['#information'] === FALSE && isset($element['webform_build']['information'])) {
        $element['webform_build']['information']['#access'] = FALSE;
      }
    }

    // Allow anonymous drafts to be restored.
    // @see \Drupal\webform\WebformSubmissionForm::buildForm
    if (\Drupal::currentUser()
      ->isAnonymous() && $webform
      ->getSetting('draft') === WebformInterface::DRAFT_ALL) {
      $element['#cache']['max-age'] = 0;

      // @todo Remove once bubbling of element's max-age to page cache is fixed.
      // @see https://www.drupal.org/project/webform/issues/3015760
      // @see https://www.drupal.org/project/drupal/issues/2352009
      if (\Drupal::moduleHandler()
        ->moduleExists('page_cache')) {
        \Drupal::service('page_cache_kill_switch')
          ->trigger();
      }
    }
    return $element;
  }

  /**
   * Build access denied message for a webform.
   *
   * @param \Drupal\webform\WebformInterface $webform
   *   A webform.
   *
   * @return array
   *   A renderable array containing thea access denied message for a webform.
   */
  public static function buildAccessDenied(WebformInterface $webform) {

    /** @var \Drupal\webform\WebformTokenManagerInterface $webform_token_manager */
    $webform_token_manager = \Drupal::service('webform.token_manager');

    // Message.
    $config = \Drupal::configFactory()
      ->get('webform.settings');
    $message = $webform
      ->getSetting('form_access_denied_message') ?: $config
      ->get('settings.default_form_access_denied_message');
    $message = $webform_token_manager
      ->replace($message, $webform);

    // Attributes.
    $attributes = $webform
      ->getSetting('form_access_denied_attributes');
    $attributes['class'][] = 'webform-access-denied';
    $build = [
      '#type' => 'container',
      '#attributes' => $attributes,
      'message' => WebformHtmlEditor::checkMarkup($message),
    ];
    return static::addCacheableDependency($build, $webform);
  }

  /**
   * Adds webform.settings and webform as cache dependencies to a render array.
   *
   * @param array &$elements
   *   The render array to update.
   * @param \Drupal\webform\WebformInterface $webform
   *   A webform.
   *
   * @return array
   *   A render array with webform.settings and webform as cache dependencies.
   */
  public static function addCacheableDependency(array &$elements, WebformInterface $webform) {

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    // Track if webform.settings is updated.
    $config = \Drupal::configFactory()
      ->get('webform.settings');
    $renderer
      ->addCacheableDependency($elements, $config);

    // Track if the webform is updated.
    $renderer
      ->addCacheableDependency($elements, $webform);
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
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.
Webform::addCacheableDependency public static function Adds webform.settings and webform as cache dependencies to a render array.
Webform::buildAccessDenied public static function Build access denied message for a webform.
Webform::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
Webform::preRenderWebformElement public static function Webform element pre render callback.