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/Plugin/Condition/Webform.php \Drupal\webform\Plugin\Condition\Webform

Provides a 'Webform' condition.

Plugin annotation


@Condition(
  id = "webform",
  label = @Translation("Webforms"),
  context_definitions = {
    "webform" = @ContextDefinition("entity:webform", label = @Translation("Webform"), required = FALSE),
    "webform_submission" = @ContextDefinition("entity:webform_submission", label = @Translation("Webform submission"), required = FALSE),
    "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = FALSE),
  }
)

Hierarchy

Expanded class hierarchy of Webform

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

File

src/Plugin/Condition/Webform.php, line 25

Namespace

Drupal\webform\Plugin\Condition
View source
class Webform extends ConditionPluginBase implements ContainerFactoryPluginInterface {
  use WebformEntityStorageTrait;

  /**
   * The webform entity reference manager.
   *
   * @var \Drupal\webform\WebformEntityReferenceManagerInterface
   */
  protected $webformEntityReferenceManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = new static($configuration, $plugin_id, $plugin_definition);
    $instance->entityTypeManager = $container
      ->get('entity_type.manager');
    $instance->webformEntityReferenceManager = $container
      ->get('webform.entity_reference_manager');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $options = [];
    $webforms = $this
      ->getWebformStorage()
      ->loadMultiple();
    foreach ($webforms as $webform) {
      $options[$webform
        ->id()] = $webform
        ->label();
    }
    $form['webforms'] = [
      '#title' => $this
        ->t('Webform'),
      '#description' => $this
        ->t('Select which webforms this block should be displayed on.'),
      '#type' => 'select',
      '#options' => $options,
      '#multiple' => $options,
      '#select2' => TRUE,
      '#default_value' => $this->configuration['webforms'],
    ];
    WebformElementHelper::process($form['webforms']);
    if (empty($this->configuration['context_mapping'])) {
      $form['message'] = [
        '#type' => 'webform_message',
        '#message_message' => $this
          ->t('Please make sure to select which entities should be used to determine the current webform.'),
        '#message_type' => 'warning',
      ];
    }
    $form = parent::buildConfigurationForm($form, $form_state);

    // Add helpful descriptions to context mapping.
    $form['context_mapping']['webform']['#description'] = $this
      ->t("Select 'Webform from URL' to display this block, when the current request's path contains the selected webform.");
    $form['context_mapping']['webform_submission']['#title'] = $this
      ->t('Select a @context value:', [
      '@context' => $this
        ->t('webform submission'),
    ]);
    $form['context_mapping']['webform_submission']['#description'] = $this
      ->t("Select 'Webform submission from URL' to display this block, when the current request's path contains a webform submission that was created from the selected webform.");
    $form['context_mapping']['node']['#description'] = $this
      ->t("Select 'Node from URL' to display this block, when the current request's path contains a node that references the selected webform using a dedicated webform field or node.");

    // Attached library to summarize configuration settings.
    $form['#attached']['library'][] = 'webform/webform.block';
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    if (!empty($values['webforms']) && empty(array_filter($values['context_mapping']))) {
      $form_state
        ->setErrorByName('webforms', $this
        ->t('Please select which entity should be used to determine the current webform.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValue('webforms') ?: [];
    $values = array_filter($values);
    $this->configuration['webforms'] = array_combine($values, $values);
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function summary() {
    if (count($this->configuration['webforms']) > 1) {
      $webforms = $this->configuration['webforms'];
      $last = array_pop($webforms);
      $webforms = implode(', ', $webforms);
      return $this
        ->t('The webform is @webforms or @last', [
        '@webforms' => $webforms,
        '@last' => $last,
      ]);
    }
    $webform = reset($this->configuration['webforms']);
    return $this
      ->t('The webform is @webform', [
      '@webform' => $webform,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate() {
    if (empty($this->configuration['webforms']) && !$this
      ->isNegated()) {
      return TRUE;
    }
    elseif ($webform = $this
      ->getContextWebform()) {
      return !empty($this->configuration['webforms'][$webform
        ->id()]);
    }
    else {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'webforms' => [],
    ] + parent::defaultConfiguration();
  }

  /**
   * Gets the webform for a defined context.
   *
   * @return null|\Drupal\webform\WebformInterface
   *   The current context's webform.
   */
  protected function getContextWebform() {
    if ($webform_submission = $this
      ->getContextValue('webform_submission')) {
      return $webform_submission
        ->getWebform();
    }
    if ($webform = $this
      ->getContextValue('webform')) {
      return $webform;
    }
    if ($node = $this
      ->getContextValue('node')) {
      if ($webform_target = $this->webformEntityReferenceManager
        ->getWebform($node)) {
        return $webform_target;
      }
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionPluginBase::$executableManager protected property The condition manager to proxy execute calls through.
ConditionPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ConditionPluginBase::execute public function Executes the plugin. Overrides ExecutableInterface::execute
ConditionPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ConditionPluginBase::isNegated public function Determines whether condition result will be negated. Overrides ConditionInterface::isNegated
ConditionPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ConditionPluginBase::setExecutableManager public function Sets the executable manager class. Overrides ConditionInterface::setExecutableManager
ConditionPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 5
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginAssignmentTrait::t abstract protected function Ensures the t() method is available.
ContextAwarePluginTrait::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginTrait::$initializedContextConfig protected property Tracks whether the context has been initialized from configuration.
ContextAwarePluginTrait::getCacheContexts public function 9
ContextAwarePluginTrait::getCacheMaxAge public function 7
ContextAwarePluginTrait::getCacheTags public function 4
ContextAwarePluginTrait::getContext public function
ContextAwarePluginTrait::getContextDefinition public function
ContextAwarePluginTrait::getContextDefinitions public function
ContextAwarePluginTrait::getContextMapping public function
ContextAwarePluginTrait::getContexts public function
ContextAwarePluginTrait::getContextValue public function
ContextAwarePluginTrait::getContextValues public function
ContextAwarePluginTrait::getPluginDefinition abstract protected function 1
ContextAwarePluginTrait::setContext public function 1
ContextAwarePluginTrait::setContextMapping public function
ContextAwarePluginTrait::setContextValue public function
ContextAwarePluginTrait::validateContexts public function
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
ExecutablePluginBase::getConfig public function Gets all configuration values.
ExecutablePluginBase::getConfigDefinition public function Gets the definition of a configuration option.
ExecutablePluginBase::getConfigDefinitions public function Gets an array of definitions of available configuration options.
ExecutablePluginBase::setConfig public function Sets the value of a particular configuration option.
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::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
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
Webform::$webformEntityReferenceManager protected property The webform entity reference manager.
Webform::buildConfigurationForm public function Form constructor. Overrides ConditionPluginBase::buildConfigurationForm
Webform::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Webform::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConditionPluginBase::defaultConfiguration
Webform::evaluate public function Evaluates the condition and returns TRUE or FALSE accordingly. Overrides ConditionInterface::evaluate
Webform::getContextWebform protected function Gets the webform for a defined context.
Webform::submitConfigurationForm public function Form submission handler. Overrides ConditionPluginBase::submitConfigurationForm
Webform::summary public function Provides a human readable summary of the condition's configuration. Overrides ConditionInterface::summary
Webform::validateConfigurationForm public function Form validation handler. Overrides ConditionPluginBase::validateConfigurationForm
WebformEntityStorageTrait::$entityStorageToTypeMappings protected property An associate array of entity type storage aliases.
WebformEntityStorageTrait::$entityTypeManager protected property The entity type manager. 5
WebformEntityStorageTrait::getEntityStorage protected function Retrieves the entity storage.
WebformEntityStorageTrait::getSubmissionStorage protected function Retrieves the webform submission storage.
WebformEntityStorageTrait::getWebformStorage protected function Retrieves the webform storage.
WebformEntityStorageTrait::__get public function Implements the magic __get() method.