You are here

UrlSettingType.php in UI Patterns Settings 8

Same filename and directory in other branches
  1. 8.2 src/Plugin/UiPatterns/SettingType/UrlSettingType.php

File

src/Plugin/UiPatterns/SettingType/UrlSettingType.php
View source
<?php

namespace Drupal\ui_patterns_settings\Plugin\UIPatterns\SettingType;

use Drupal\Core\Url;
use Drupal\ui_patterns_settings\Definition\PatternDefinitionSetting;
use Drupal\ui_patterns_settings\Plugin\TokenSettingTypeBase;

/**
 * Url setting type.
 *
 * @UiPatternsSettingType(
 *   id = "url",
 *   label = @Translation("Url")
 * )
 */
class UrlSettingType extends TokenSettingTypeBase {

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Classes

Namesort descending Description
UrlSettingType Url setting type.