PlatformBase.php in Social Media Links Block and Field 8.2
File
src/PlatformBase.php
View source
<?php
namespace Drupal\social_media_links;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
class PlatformBase extends PluginBase implements PlatformInterface {
protected $value;
protected $description;
public function getValue() {
return Html::escape($this->value);
}
public function setValue($value) {
$this->value = $value;
}
public function getIconName() {
return !empty($this->pluginDefinition['iconName']) ? $this->pluginDefinition['iconName'] : $this->pluginDefinition['id'];
}
public function getName() {
return $this->pluginDefinition['name'];
}
public function getFieldDescription() {
return isset($this->pluginDefinition['fieldDescription']) ? $this->pluginDefinition['fieldDescription'] : '';
}
public function getUrlPrefix() {
return isset($this->pluginDefinition['urlPrefix']) ? $this->pluginDefinition['urlPrefix'] : '';
}
public function getUrlSuffix() {
return isset($this->pluginDefinition['urlSuffix']) ? $this->pluginDefinition['urlSuffix'] : '';
}
public function getUrl() {
return Url::fromUri($this
->getUrlPrefix() . $this
->getValue() . $this
->getUrlSuffix());
}
public function generateUrl(Url $url) {
return $url
->toString();
}
public function getDescription() {
return Html::escape($this->description);
}
public function setDescription($description) {
$this->description = $description;
}
public static function validateValue(array &$element, FormStateInterface $form_state, array $form) {
if (!empty($element['#value']) && !empty($element['#field_prefix'])) {
if (UrlHelper::isExternal($element['#value'])) {
$form_state
->setError($element, t("The entered value %value is a URL. You should enter only the relative part, the URL prefix is automatically prepended.", [
'%value' => $element['#value'],
]));
}
}
}
}