You are here

class ExampleWebformVariant in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_example_variant/src/Plugin/WebformVariant/ExampleWebformVariant.php \Drupal\webform_example_variant\Plugin\WebformVariant\ExampleWebformVariant

Webform example variant.

Plugin annotation


@WebformVariant(
  id = "example",
  label = @Translation("Example"),
  category = @Translation("Example"),
  description = @Translation("Example of a webform variant."),
)

Hierarchy

Expanded class hierarchy of ExampleWebformVariant

File

modules/webform_example_variant/src/Plugin/WebformVariant/ExampleWebformVariant.php, line 19

Namespace

Drupal\webform_example_variant\Plugin\WebformVariant
View source
class ExampleWebformVariant extends WebformVariantBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'debug' => FALSE,
      'description__markup' => '',
      'notes__type' => 'textfield',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function isApplicable(WebformInterface $webform) {

    // Only allow variant to be applicable to webform_example_variant_ webforms.
    return strpos($webform
      ->id(), 'webform_example_variant_') === 0;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['example'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Example settings'),
    ];
    $form['example']['description__markup'] = [
      '#type' => 'webform_html_editor',
      '#title' => $this
        ->t('Description'),
      '#parents' => [
        'settings',
        'description__markup',
      ],
      '#default_value' => $this->configuration['description__markup'],
    ];
    $form['example']['notes__type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Notes element type'),
      '#options' => [
        'textarea' => $this
          ->t('Text area'),
        'textfield' => $this
          ->t('Text field'),
      ],
      '#empty_value' => '',
      '#default_value' => $this->configuration['notes__type'],
      '#parents' => [
        'settings',
        'notes__type',
      ],
    ];

    // Development.
    $form['development'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Development settings'),
    ];
    $form['development']['debug'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable debugging'),
      '#description' => $this
        ->t('If checked, variant information will be displayed onscreen to all users.'),
      '#return_value' => TRUE,
      '#default_value' => $this->configuration['debug'],
      '#parents' => [
        'settings',
        'debug',
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration = $form_state
      ->getValues();
    $this->configuration['debug'] = (bool) $this->configuration['debug'];
  }

  /**
   * {@inheritdoc}
   */
  public function applyVariant() {
    $webform = $this
      ->getWebform();
    if (!$this
      ->isApplicable($webform)) {
      return FALSE;
    }

    // Set description markup.
    $description_markup = $this->configuration['description__markup'];
    $description_element = $webform
      ->getElementDecoded('description');
    if ($description_element && $description_markup) {
      $description_element['#markup'] = $description_markup;
      $webform
        ->setElementProperties('description', $description_element);
    }

    // Set notes type.
    $notes_type = $this->configuration['notes__type'];
    $notes_element = $webform
      ->getElementDecoded('notes');
    if ($notes_element && $notes_type) {
      $notes_element['#type'] = $notes_type;
      $webform
        ->setElementProperties('notes', $notes_element);
    }
    return TRUE;
  }

}

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
ExampleWebformVariant::applyVariant public function Apply variant to the webform. Overrides WebformVariantBase::applyVariant
ExampleWebformVariant::buildConfigurationForm public function Form constructor. Overrides WebformVariantBase::buildConfigurationForm
ExampleWebformVariant::defaultConfiguration public function Gets default configuration for this plugin. Overrides WebformVariantBase::defaultConfiguration
ExampleWebformVariant::isApplicable public function Determine if this variant is applicable to the webform. Overrides WebformVariantBase::isApplicable
ExampleWebformVariant::submitConfigurationForm public function Form submission handler. Overrides WebformVariantBase::submitConfigurationForm
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
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.
WebformEntityInjectionTrait::$webform protected property The webform. 1
WebformEntityInjectionTrait::$webformSubmission protected property The webform submission. 1
WebformEntityInjectionTrait::getWebform public function Get the webform that this handler is attached to.
WebformEntityInjectionTrait::getWebformSubmission public function Set webform and webform submission entity.
WebformEntityInjectionTrait::resetEntities public function Reset webform and webform submission entity.
WebformEntityInjectionTrait::setEntities public function
WebformEntityInjectionTrait::setWebform public function Set the webform that this is handler is attached to.
WebformEntityInjectionTrait::setWebformSubmission public function Get the webform submission that this handler is handling.
WebformPluginSettingsTrait::getSetting public function
WebformPluginSettingsTrait::getSettings public function
WebformPluginSettingsTrait::setSetting public function
WebformPluginSettingsTrait::setSettings public function
WebformVariantBase::$configFactory protected property The configuration factory.
WebformVariantBase::$element_key protected property The element key of the webform variant.
WebformVariantBase::$label protected property The webform variant label.
WebformVariantBase::$notes protected property The webform variant notes.
WebformVariantBase::$status protected property The webform variant status.
WebformVariantBase::$variant_id protected property The webform variant ID.
WebformVariantBase::$weight protected property The weight of the webform variant.
WebformVariantBase::calculateDependencies public function
WebformVariantBase::create public static function IMPORTANT: Webform handlers are initialized and serialized when they are attached to a webform. Make sure not include any services as a dependency injection that directly connect to the database. This will prevent "LogicException: The database… Overrides ContainerFactoryPluginInterface::create 1
WebformVariantBase::description public function Returns the webform variant description. Overrides WebformVariantInterface::description
WebformVariantBase::disable public function Disables the webform variant. Overrides WebformVariantInterface::disable
WebformVariantBase::enable public function Enables the webform variant. Overrides WebformVariantInterface::enable
WebformVariantBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
WebformVariantBase::getElementKey public function Returns the element key of the webform variant. Overrides WebformVariantInterface::getElementKey
WebformVariantBase::getLabel public function Returns the label of the webform variant. Overrides WebformVariantInterface::getLabel
WebformVariantBase::getMachineNameReplace public function Returns the webform variant machine name replacement character. Overrides WebformVariantInterface::getMachineNameReplace
WebformVariantBase::getMachineNameReplacePattern public function Returns the webform variant machine name replacement pattern. Overrides WebformVariantInterface::getMachineNameReplacePattern
WebformVariantBase::getNotes public function Returns notes of the webform variant. Overrides WebformVariantInterface::getNotes
WebformVariantBase::getOffCanvasWidth public function Get configuration form's off-canvas width. Overrides WebformVariantInterface::getOffCanvasWidth 1
WebformVariantBase::getStatus public function Returns the status of the webform variant. Overrides WebformVariantInterface::getStatus
WebformVariantBase::getSummary public function Returns a render array summarizing the configuration of the webform variant. Overrides WebformVariantInterface::getSummary 1
WebformVariantBase::getVariantId public function Returns the unique ID representing the webform variant. Overrides WebformVariantInterface::getVariantId
WebformVariantBase::getWeight public function Returns the weight of the webform variant. Overrides WebformVariantInterface::getWeight
WebformVariantBase::isDisabled public function Returns the webform variant disabled indicator. Overrides WebformVariantInterface::isDisabled
WebformVariantBase::isEnabled public function Returns the webform variant enabled indicator. Overrides WebformVariantInterface::isEnabled 1
WebformVariantBase::isExcluded public function Checks if the variant is excluded via webform.settings. Overrides WebformVariantInterface::isExcluded
WebformVariantBase::label public function Returns the webform variant label. Overrides WebformVariantInterface::label
WebformVariantBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
WebformVariantBase::setElementKey public function Sets the element key of this webform variant. Overrides WebformVariantInterface::setElementKey
WebformVariantBase::setLabel public function Sets the label for this webform variant. Overrides WebformVariantInterface::setLabel
WebformVariantBase::setNotes public function Set notes for this webform variant. Overrides WebformVariantInterface::setNotes
WebformVariantBase::setStatus public function Sets the status for this webform variant. Overrides WebformVariantInterface::setStatus
WebformVariantBase::setVariantId public function Sets the id for this webform variant. Overrides WebformVariantInterface::setVariantId
WebformVariantBase::setWeight public function Sets the weight for this webform variant. Overrides WebformVariantInterface::setWeight
WebformVariantBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1