Skip to content

Commit 1fd9a2b

Browse files
authored
Merge pull request #11 from charlotte17/urlObject
Adds url object
2 parents c621f26 + 212d5d9 commit 1fd9a2b

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"aleksip\\DataTransformPlugin\\": "src/",
2020
"Drupal\\Component\\Render\\": "src/",
2121
"Drupal\\Component\\Utility\\": "src/",
22+
"Drupal\\Core\\": "src/",
2223
"Drupal\\Core\\Template\\": "src/"
2324
}
2425
},

src/Drupal/Core/Url.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
namespace Drupal\Core;
4+
5+
/**
6+
* Defines an object that holds information about a URL.
7+
*/
8+
class Url {
9+
10+
/**
11+
* The URL options.
12+
*
13+
* See \Drupal\Core\Url::fromUri() for details on the options.
14+
*
15+
* @var array
16+
*/
17+
protected $options = array();
18+
19+
/**
20+
* The URI.
21+
*
22+
* @var string
23+
*/
24+
protected $uri;
25+
26+
/**
27+
* Constructs a new Url object.
28+
*
29+
* In most cases, use Url::fromRoute() rather than
30+
* constructing Url objects directly in order to avoid ambiguity and make your
31+
* code more self-documenting.
32+
*
33+
* @param string $uri
34+
* The uri
35+
* @param array $options
36+
* See \Drupal\Core\Url::fromUri() for details.
37+
*
38+
* @see static::fromRoute()
39+
* @see static::fromUri()
40+
*
41+
* @todo Update this documentation for non-routed URIs in
42+
* https://www.drupal.org/node/2346787
43+
*/
44+
public function __construct($uri, $options = array()) {
45+
$this->uri = $uri;
46+
$this->options = $options;
47+
}
48+
49+
/**
50+
* Creates a new Url object from a URI.
51+
*
52+
* @param string $uri
53+
* The URI of the resource including the scheme.
54+
* @param array $options
55+
* (optional) An associative array of additional URL options, with the
56+
* following elements:
57+
* - 'attributes': An associative array of HTML attributes that will be
58+
* added to the anchor tag if you use the \Drupal\Core\Link class to make
59+
* the link.
60+
*
61+
* @return \Drupal\Core\Url
62+
* A new Url object.
63+
*/
64+
public static function fromUri($uri, $options = []) {
65+
$url = new static($uri, $options);
66+
67+
return $url;
68+
}
69+
70+
/**
71+
* Returns the URL options.
72+
*
73+
* @return array
74+
* The array of options. See \Drupal\Core\Url::fromUri() for details on what
75+
* it contains.
76+
*/
77+
public function getOptions() {
78+
return $this->options;
79+
}
80+
81+
/**
82+
* Gets a specific option.
83+
*
84+
* See \Drupal\Core\Url::fromUri() for details on the options.
85+
*
86+
* @param string $name
87+
* The name of the option.
88+
*
89+
* @return mixed
90+
* The value for a specific option, or NULL if it does not exist.
91+
*/
92+
public function getOption($name) {
93+
if (!isset($this->options[$name])) {
94+
return NULL;
95+
}
96+
97+
return $this->options[$name];
98+
}
99+
100+
/**
101+
* Sets the URL options.
102+
*
103+
* @param array $options
104+
* The array of options. See \Drupal\Core\Url::fromUri() for details on what
105+
* it contains.
106+
*
107+
* @return $this
108+
*/
109+
public function setOptions($options) {
110+
$this->options = $options;
111+
return $this;
112+
}
113+
114+
/**
115+
* Sets a specific option.
116+
*
117+
* See \Drupal\Core\Url::fromUri() for details on the options.
118+
*
119+
* @param string $name
120+
* The name of the option.
121+
* @param mixed $value
122+
* The option value.
123+
*
124+
* @return $this
125+
*/
126+
public function setOption($name, $value) {
127+
$this->options[$name] = $value;
128+
return $this;
129+
}
130+
131+
/**
132+
* Returns the URI value for this Url object.
133+
*
134+
* @return string
135+
* A URI.
136+
*/
137+
public function getUri() {
138+
return $this->uri;
139+
}
140+
141+
/**
142+
* Generates the string URL representation for this Url object.
143+
*
144+
* @return string
145+
* A string URL.
146+
*/
147+
public function __tostring() {
148+
return $this->uri;
149+
}
150+
151+
}

src/aleksip/DataTransformPlugin/DataTransformer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace aleksip\DataTransformPlugin;
44

55
use Drupal\Core\Template\Attribute;
6+
use Drupal\Core\Url;
67
use PatternLab\Data;
78
use PatternLab\PatternData;
89
use PatternLab\PatternEngine;
@@ -91,6 +92,10 @@ protected function processKey($data, $key)
9192
if (isset($value['Attribute()']) && is_array($value['Attribute()'])) {
9293
$data[$key] = new Attribute($value['Attribute()']);
9394
}
95+
elseif (isset($value['Url()']['url'])) {
96+
$options = isset($value['Url()']['options']) && is_array($value['Url()']['options']) ? $value['Url()']['options'] : [];
97+
$data[$key] = Url::fromUri($value['Url()']['url'], $options);
98+
}
9499
elseif (isset($value['include()']) && is_array($value['include()']) && isset($value['include()']['pattern'])) {
95100
$pattern = $value['include()']['pattern'];
96101
if (is_string($pattern) && isset($this->patternDataStore[$pattern])) {

0 commit comments

Comments
 (0)