WebformLink.php in Webform 6.x
File
src/Plugin/WebformElement/WebformLink.php
View source
<?php
namespace Drupal\webform\Plugin\WebformElement;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformLink 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 defineDefaultProperties() {
$properties = parent::defineDefaultProperties();
unset($properties['select2'], $properties['chosed']);
return $properties;
}
protected function formatHtmlItemValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
return [
'link' => [
'#type' => 'link',
'#title' => $value['title'],
'#url' => $this->pathValidator
->getUrlIfValid($value['url']),
],
];
}
protected function formatTextItemValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
return [
'link' => new FormattableMarkup('@title (@url)', [
'@title' => $value['title'],
'@url' => $value['url'],
]),
];
}
}