You are here

public function UrlValue::getUrlValue in Twig Extender 8.2

Same name and namespace in other branches
  1. 8.4 modules/twig_extender_extras/src/Plugin/TwigPlugin/UrlValue.php \Drupal\twig_extender_extras\Plugin\TwigPlugin\UrlValue::getUrlValue()
  2. 8.3 modules/twig_extender_extras/src/Plugin/TwigPlugin/UrlValue.php \Drupal\twig_extender_extras\Plugin\TwigPlugin\UrlValue::getUrlValue()
  3. 4.x modules/twig_extender_extras/src/Plugin/TwigPlugin/UrlValue.php \Drupal\twig_extender_extras\Plugin\TwigPlugin\UrlValue::getUrlValue()

Get a url value from a link field.

Parameters

\Drupal\Core\Field\Plugin\DataType\FieldItem $field: Field item from type link.

Return value

\Drupal\Core\Url Url object.

Throws

\Exception

File

modules/twig_extender_extras/src/Plugin/TwigPlugin/UrlValue.php, line 32

Class

UrlValue
The plugin for render a url string of a link field object.

Namespace

Drupal\twig_extender_extras\Plugin\TwigPlugin

Code

public function getUrlValue(FieldItem $field) {
  try {
    $field_type = $field
      ->getFieldDefinition()
      ->getType();
    if ($field_type == 'link') {
      $uri = $field
        ->first()
        ->getUrl();
      return $uri;
    }
  } catch (\Exception $e) {
    \Drupal::logger('twig_extender_extras')
      ->error($e
      ->getMessage());
    throw $e;
  }
}