View source
<?php
namespace Drupal\gdpr\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use function explode;
use function in_array;
use function is_array;
use function parse_url;
use function strlen;
use function strpos;
use function substr;
class ContentLinksForm extends ConfigFormBase {
const GDPR_CONTENT_CONF_KEY = 'gdpr.content_mapping';
protected $languageManager;
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('language_manager'));
}
public function __construct(ConfigFactoryInterface $configFactory, LanguageManagerInterface $languageManager) {
parent::__construct($configFactory);
$this->languageManager = $languageManager;
}
protected function getEditableConfigNames() {
return [
static::GDPR_CONTENT_CONF_KEY,
];
}
public function getFormId() {
return 'gdpr_content_links_form';
}
public static function requiredContentList() {
return [
'privacy_policy' => t('Privacy policy'),
'terms_of_use' => t('Terms of use'),
'about_us' => t('About us'),
'impressum' => t('Impressum'),
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#tree'] = TRUE;
$form['description'] = [
'#markup' => $this
->t('Enter internal paths (e.g. <em>@internal_path</em>) or full URLs (e.g. <em>@full_url</em>) per each language installed on the site.', [
'@internal_path' => '/privacy-policy',
'@full_url' => 'https://www.example.com/terms-of-service.pdf',
]),
];
$form['links'] = [
'#type' => 'container',
];
$urls = $this
->loadUrls();
foreach ($this->languageManager
->getLanguages() as $langCode => $language) {
$form['links'][$langCode] = [
'#type' => 'details',
'#title' => $language
->getName(),
'#open' => $language
->isDefault(),
];
foreach (static::requiredContentList() as $key => $label) {
$form['links'][$langCode][$key] = [
'#type' => 'textfield',
'#title' => $label,
'#process_default_value' => FALSE,
'#element_validate' => [
[
static::class,
'validateUriElement',
],
],
'#default_value' => isset($urls[$langCode][$key]) ? $urls[$langCode][$key] : NULL,
];
}
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->hasValue('links')) {
$links = $form_state
->getValue('links', []);
$config = $this->configFactory
->getEditable(static::GDPR_CONTENT_CONF_KEY);
$config
->set('links', $links)
->save();
}
parent::submitForm($form, $form_state);
}
protected function loadUrls() {
$config = $this
->config(static::GDPR_CONTENT_CONF_KEY)
->get('links');
if (NULL === $config || !is_array($config)) {
$config = [];
}
foreach ($config as $langCode => $links) {
foreach ($links as $key => $link) {
$config[$langCode][$key] = static::getUriAsDisplayableString($link);
}
}
return $config;
}
protected static function getUriAsDisplayableString($uri) {
$scheme = parse_url($uri, PHP_URL_SCHEME);
$displayableString = $uri;
if ($scheme === 'internal') {
$uriReference = explode(':', $uri, 2)[1];
$path = parse_url($uri, PHP_URL_PATH);
if ($path === '/') {
$uriReference = '<front>' . substr($uriReference, 1);
}
$displayableString = $uriReference;
}
return $displayableString;
}
public static function validateUriElement($element, FormStateInterface $form_state, $form) {
$uri = static::getUserEnteredStringAsUri($element['#value']);
$form_state
->setValueForElement($element, $uri);
if (parse_url($uri, PHP_URL_SCHEME) === 'internal' && 0 !== strpos($element['#value'], '<front>') && !in_array($element['#value'][0], [
'/',
'?',
'#',
], TRUE)) {
$form_state
->setError($element, t('Manually entered paths should start with /, ? or #.'));
return;
}
}
protected static function getUserEnteredStringAsUri($string) {
$uri = $string;
if (!empty($string) && parse_url($string, PHP_URL_SCHEME) === NULL) {
if (strpos($string, '<front>') === 0) {
$string = '/' . substr($string, strlen('<front>'));
}
$uri = 'internal:' . $string;
}
return $uri;
}
}