-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
RelatedOptionsUtility.php
61 lines (50 loc) 路 1.67 KB
/
RelatedOptionsUtility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace Bolt\Utils;
use Bolt\Entity\Content;
use Bolt\Entity\Field;
use Bolt\Storage\Query;
/**
* Utility class to get the 'Related Records' as options to show as a pull-down in the Editor.
*
* Decorated by `Bolt\Cache\RelatedOptionsUtilityCacher`
*/
class RelatedOptionsUtility
{
/** @var Query */
private $query;
/** @var ContentHelper */
private $contentHelper;
public function __construct(Query $query, ContentHelper $contentHelper)
{
$this->query = $query;
$this->contentHelper = $contentHelper;
}
/**
* Decorated by `Bolt\Cache\RelatedOptionsUtilityCacher`
*/
public function fetchRelatedOptions(string $contentTypeSlug, string $order, string $format, bool $required, ?bool $allowEmpty, int $maxAmount): array
{
$pager = $this->query->getContent($contentTypeSlug, ['order' => $order])
->setMaxPerPage($maxAmount)
->setCurrentPage(1);
$records = iterator_to_array($pager->getCurrentPageResults());
$options = [];
// We need to add this as a 'dummy' option for when the user is allowed
// not to pick an option. This is needed, because otherwise the `select`
// would default to the first one.
if (Field::settingsAllowEmpty($allowEmpty, $required)) {
$options[] = [
'key' => '',
'value' => '',
];
}
/** @var Content $record */
foreach ($records as $record) {
$options[] = [
'key' => $record->getId(),
'value' => $this->contentHelper->get($record, $format),
];
}
return $options;
}
}