You are here

public function UrlSettingType::settingsPreprocess in UI Patterns Settings 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/UiPatterns/SettingType/UrlSettingType.php \Drupal\ui_patterns_settings\Plugin\UIPatterns\SettingType\UrlSettingType::settingsPreprocess()

Preprocess setting variable.

Parameters

string $value: The stored value.

array $context: Context informations. Keys:

  • entity.

\Drupal\ui_patterns_settings\Definition\PatternDefinitionSetting $def: The pattern definition.

Return value

string The processed value.

Overrides TokenSettingTypeBase::settingsPreprocess

File

src/Plugin/UiPatterns/SettingType/UrlSettingType.php, line 22

Class

UrlSettingType
Url setting type.

Namespace

Drupal\ui_patterns_settings\Plugin\UIPatterns\SettingType

Code

public function settingsPreprocess($value, array $context, PatternDefinitionSetting $def) {
  $value = parent::settingsPreprocess($value, $context, $def);
  if (empty($value)) {
    return "";
  }
  try {
    $url = Url::fromUri($value)
      ->toString();
  } catch (\Exception $e) {

    // Not a valid uri. Try user input:
    try {
      $url = Url::fromUserInput($value)
        ->toString();
    } catch (\Exception $e) {

      // Not a valid url.
      return '#';
    }
  }
  return $url;
}