Skip to content

Commit 688a81e

Browse files
committed
Allows to have url with or without base url
1 parent 5f1535d commit 688a81e

File tree

4 files changed

+88
-25
lines changed

4 files changed

+88
-25
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ properties are renderable by objects. Let's take the example below:
6969
"footer_sitemap": {
7070
"l10n": true,
7171
"check_active_routes": true,
72+
"relative_urls": false,
7273
"objects": {
7374
"boilerplate/object/section": {
7475
"label": "{{title}}",
@@ -119,6 +120,26 @@ Given the settings above:
119120
$builder = $container['charcoal/sitemap/builder'];
120121
$sitemap = $builder->build('footer_sitemap'); // footer_sitemap is the ident of the settings you want.
121122
```
123+
You can also use the `SitemapBuilderAwareTrait`, which includes the setter and getter for the sitemap builder, in order
124+
to use it with minimal code in every necessary class.
125+
126+
127+
### Options
128+
129+
| Key | Values | Default | Description
130+
|:--- |:---: |:---: |---
131+
|`l10n` | true/false | false | Defines if the sitemap includes all the languages defined in the configurations
132+
| `check_active_routes` | true/false | false | Checks on every object if the route is active (using isActiveRoute)
133+
| `relative_urls` | true/false | false | Returns either the absolute URL or the relative URL
134+
| `locale` | string | Current locale | You can choose the locale for the sitemap. (Unless l10n)
135+
| `objects` | object class string | n/a | The objects and their configurations
136+
| `objects` |- |- |-
137+
| `filters` | Array | n/a | Filters for the list
138+
| `orders` | Array | n/a | Orders for the list
139+
| `children` | Array | n/a | Uses the same options as `objects`
140+
| `url` | string | {{url}} | Renderable URL, in case the method is not `url`
141+
| `label` | string | {{title}} | Renderable label, in case it's not `title`
142+
| `data` | Array | n/a | Any order data you want in. This is recursively rendered on the object
122143

123144
### Sitemap.xml
124145
This contrib provides a route for `sitemap.xml` that dynamically loads the `xml` config and outputs it

src/Charcoal/Sitemap/ExampleInterface.php

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Charcoal\Sitemap\Mixin;
4+
5+
use Charcoal\Sitemap\Service\Builder;
6+
7+
trait SitemapBuilderAwareTrait
8+
{
9+
/**
10+
* @var Builder
11+
*/
12+
protected $sitemapBuilder;
13+
14+
/**
15+
* @return Builder
16+
*/
17+
public function sitemapBuilder()
18+
{
19+
return $this->sitemapBuilder;
20+
}
21+
22+
/**
23+
* @param Builder $sitemapBuilder
24+
* @return SitemapBuilderAwareTrait
25+
*/
26+
public function setSitemapBuilder($sitemapBuilder)
27+
{
28+
$this->sitemapBuilder = $sitemapBuilder;
29+
return $this;
30+
}
31+
}

src/Charcoal/Sitemap/Service/Builder.php

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
namespace Charcoal\Sitemap\Service;
44

5+
use Charcoal\Factory\FactoryInterface;
6+
use Charcoal\Loader\CollectionLoader;
7+
use Charcoal\Object\CategoryInterface;
8+
use Charcoal\Object\HierarchicalInterface;
9+
use Charcoal\Object\RoutableInterface;
10+
use Charcoal\Translator\TranslatorAwareTrait;
11+
use Charcoal\View\ViewableInterface;
512
use InvalidArgumentException;
613
use RuntimeException;
714

815
// From 'charcoal-factory'
9-
use Charcoal\Factory\FactoryInterface;
1016

1117
// From 'charcoal-core'
12-
use Charcoal\Loader\CollectionLoader;
1318

1419
// From 'charcoal-object'
15-
use Charcoal\Object\CategoryInterface;
16-
use Charcoal\Object\HierarchicalInterface;
17-
use Charcoal\Object\RoutableInterface;
1820

1921
// From 'charcoal-translator'
20-
use Charcoal\Translator\TranslatorAwareTrait;
2122

2223
// From 'charcoal-view'
23-
use Charcoal\View\ViewableInterface;
2424

2525
/**
2626
* Sitemap builder from object hierarchy
@@ -117,6 +117,7 @@ protected function defaultOptions()
117117
'locale' => $this->translator()->getLocale(),
118118
'l10n' => true,
119119
'check_active_routes' => true,
120+
'relative_urls' => true,
120121
'objects' => [
121122
'label' => '{{title}}',
122123
'url' => '{{url}}',
@@ -175,6 +176,9 @@ public function build($ident = 'default')
175176
if (!isset($options['check_active_routes'])) {
176177
$options['check_active_routes'] = $defaults['check_active_routes'];
177178
}
179+
if (!isset($options['relative_urls'])) {
180+
$options['relative_urls'] = $defaults['relative_urls'];
181+
}
178182
$out[] = $this->buildObject($class, $options);
179183
}
180184

@@ -198,11 +202,13 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
198202
}
199203
}
200204

205+
// Loadin the actual objects from the predefined settings
201206
$factory = $this->modelFactory();
202207
$obj = $factory->create($class);
203208

204209
$loader = $this->collectionLoader()->setModel($obj);
205210

211+
// From the filters
206212
if (isset($options['filters'])) {
207213
$filters = $options['filters'];
208214
if ($parent) {
@@ -211,6 +217,7 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
211217
$loader->addFilters($filters);
212218
}
213219

220+
// From the orders
214221
if (isset($options['orders'])) {
215222
$orders = $options['orders'];
216223
if ($parent) {
@@ -219,6 +226,7 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
219226
$loader->addOrders($orders);
220227
}
221228

229+
// From the category / hierarchichal property
222230
$category = ($obj instanceof CategoryInterface);
223231
$hierarchical = ($obj instanceof HierarchicalInterface);
224232
if ($hierarchical || $category) {
@@ -229,21 +237,26 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
229237
}
230238
}
231239

240+
// Loading
232241
$list = $loader->load();
233242

243+
// Processing the objects and rendering data
234244
$out = [];
235245
$children = isset($options['children']) ? $options['children'] : [];
236246
$level++;
237247

248+
// Options
238249
$l10n = $options['l10n'];
239-
$locale = $options['locale'];
250+
$defaultLocale = $options['locale'];
240251
$checkActiveRoutes = $options['check_active_routes'];
252+
$relativeUrls = $options['relative_urls'];
241253

242-
$availableLocales = $l10n ? $this->translator()->availableLocales() : [$locale];
254+
// Locales
255+
$availableLocales = $l10n ? $this->translator()->availableLocales() : [$defaultLocale];
243256

244257
foreach ($availableLocales as $locale) {
245258

246-
$currentLocale = $locale;
259+
// Get opposite languages locales
247260
$oppositeLang = [];
248261
foreach ($availableLocales as $l) {
249262
if ($l == $locale) {
@@ -252,12 +265,15 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
252265
$oppositeLang[] = $l;
253266
}
254267

268+
// Set the local to the current locale before looping the list.
255269
$this->translator()->setLocale($locale);
256270
foreach ($list as $object) {
271+
// When checking active routes, do not display routes that are not active
257272
if ($checkActiveRoutes && $object instanceof RoutableInterface && !$object->isActiveRoute()) {
258273
continue;
259274
}
260275

276+
// Hierarchical (children, when defined)
261277
$cs = [];
262278
if (!empty($children)) {
263279
foreach ($children as $cname => $opts) {
@@ -266,27 +282,34 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
266282
}
267283
}
268284

285+
// Url template, relative or absolute?
286+
$urlTemplate = $relativeUrls ? $options['url'] : '{{#withBaseUrl}}' . $options['url'] . '{{/withBaseUrl}}';
287+
269288
$tmp = [
270289
'label' => trim($this->renderData($object, $options['label'])),
271-
'url' => trim($this->renderData($object, '{{#withBaseUrl}}' . $options['url'] . '{{/withBaseUrl}}')),
290+
'url' => trim($this->renderData($object, $urlTemplate)),
272291
'children' => $cs,
273292
'data' => $this->renderData($object, $options['data']),
274293
'level' => $level,
275294
'lang' => $locale
276295
];
277296

297+
// If you need a priority, fix your own rules
278298
$priority = '';
279299
if (isset($options['priority']) && $options['priority']) {
280300
$priority = $this->renderData($object, (string)$options['priority']);
281301
}
282302
$tmp['priority'] = $priority;
283303

304+
// If you need a date of last modification, fix your own rules
284305
$last = '';
285306
if (isset($options['last_modified']) && $options['last_modified']) {
286307
$last = $this->renderData($object, $options['last_modified']);
287308
}
288309
$tmp['last_modified'] = $last;
289310

311+
// Opposite Languages
312+
// Meant to be alternate, thus the lack of data rendering
290313
$alternates = [];
291314
foreach ($oppositeLang as $ol) {
292315
$this->translator()->setLocale($ol);
@@ -302,15 +325,15 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
302325

303326
$tmp['alternates'] = $alternates;
304327

305-
$this->translator()->setLocale($currentLocale);
328+
$this->translator()->setLocale($locale);
306329
$out[] = $tmp;
307330
}
308331
}
309332

310333

311334
return $out;
312335
}
313-
336+
314337
/**
315338
* Recursive data renderer
316339
*

0 commit comments

Comments
 (0)