View source
<?php
namespace Drupal\webform\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\webform\EntityStorage\WebformEntityStorageTrait;
use Drupal\webform\Utility\WebformElementHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Webform extends ConditionPluginBase implements ContainerFactoryPluginInterface {
use WebformEntityStorageTrait;
protected $webformEntityReferenceManager;
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;
}
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);
$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.");
$form['#attached']['library'][] = 'webform/webform.block';
return $form;
}
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.'));
}
}
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);
}
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,
]);
}
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;
}
}
public function defaultConfiguration() {
return [
'webforms' => [],
] + parent::defaultConfiguration();
}
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;
}
}