WebformContact.php in Webform 6.x
File
src/Plugin/WebformElement/WebformContact.php
View source
<?php
namespace Drupal\webform\Plugin\WebformElement;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformContact extends WebformCompositeBase {
protected $pathValidator;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->pathValidator = $container
->get('path.validator');
return $instance;
}
protected function formatHtmlItemValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$lines = $this
->formatTextItemValue($element, $webform_submission, $options);
if (!empty($lines['email'])) {
$lines['email'] = [
'#type' => 'link',
'#title' => $lines['email'],
'#url' => $this->pathValidator
->getUrlIfValid('mailto:' . $lines['email']),
];
}
return $lines;
}
protected function formatTextItemValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
$location = '';
if (!empty($value['city'])) {
$location .= $value['city'];
}
if (!empty($value['state_province'])) {
$location .= $location ? ', ' : '';
$location .= $value['state_province'];
}
if (!empty($value['postal_code'])) {
$location .= $location ? '. ' : '';
$location .= $value['postal_code'];
}
$lines = [];
if (!empty($value['name'])) {
$lines['name'] = $value['name'];
}
if (!empty($value['company'])) {
$lines['company'] = $value['company'];
}
if (!empty($value['address'])) {
$lines['address'] = $value['address'];
}
if (!empty($value['address_2'])) {
$lines['address_2'] = $value['address_2'];
}
if ($location) {
$lines['location'] = $location;
}
if (!empty($value['country'])) {
$lines['country'] = $value['country'];
}
if (!empty($value['email'])) {
$lines['email'] = $value['email'];
}
if (!empty($value['phone'])) {
$lines['phone'] = $value['phone'];
}
return $lines;
}
}