You are here

class Webform in Webform 8.5

Same name in this branch
  1. 8.5 src/Element/Webform.php \Drupal\webform\Element\Webform
  2. 8.5 src/Entity/Webform.php \Drupal\webform\Entity\Webform
  3. 8.5 src/Plugin/Condition/Webform.php \Drupal\webform\Plugin\Condition\Webform
Same name and namespace in other branches
  1. 6.x 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

48 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 26

Namespace

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

  /**
   * The entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $entityStorage;

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

  /**
   * Creates a new Webform instance.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
   *   The entity storage.
   * @param \Drupal\webform\WebformEntityReferenceManagerInterface $webform_entity_reference_manager
   *   The webform entity reference manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $entity_storage, WebformEntityReferenceManagerInterface $webform_entity_reference_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityStorage = $entity_storage;
    $this->webformEntityReferenceManager = $webform_entity_reference_manager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $options = [];
    $webforms = $this->entityStorage
      ->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
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.
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::createContextFromConfiguration protected function Overrides ContextAwarePluginBase::createContextFromConfiguration
ContextAwarePluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 9
ContextAwarePluginBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 7
ContextAwarePluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 4
ContextAwarePluginBase::getContext public function This code is identical to the Component in order to pick up a different Context class. Overrides ContextAwarePluginBase::getContext
ContextAwarePluginBase::getContextDefinition public function Overrides ContextAwarePluginBase::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Overrides ContextAwarePluginBase::getContextDefinitions
ContextAwarePluginBase::getContextMapping public function Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::getContextMapping
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginBase::setContext
ContextAwarePluginBase::setContextMapping public function Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::setContextMapping
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__get public function Implements magic __get() method.
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
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.
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 3
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. 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
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2
Webform::$entityStorage protected property The entity storage.
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
Webform::__construct public function Creates a new Webform instance. Overrides ConditionPluginBase::__construct