1 | <?php |
||||
2 | |||||
3 | namespace SilverStripe\CKANRegistry\Model\ResourceFilter; |
||||
4 | |||||
5 | use InvalidArgumentException; |
||||
6 | use SilverStripe\CKANRegistry\Forms\PresentedOptionsField; |
||||
7 | use SilverStripe\CKANRegistry\Model\ResourceFilter; |
||||
8 | use SilverStripe\Forms\DropdownField; |
||||
9 | use SilverStripe\Forms\FieldList; |
||||
10 | |||||
11 | /** |
||||
12 | * Provides a single select option for CKAN resources to be filtered by |
||||
13 | */ |
||||
14 | class Dropdown extends ResourceFilter |
||||
15 | { |
||||
16 | private static $db = [ |
||||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
17 | 'Options' => 'Text', // JSON blob |
||||
18 | ]; |
||||
19 | |||||
20 | private static $table_name = 'CKANFilter_Dropdown'; |
||||
0 ignored issues
–
show
|
|||||
21 | |||||
22 | private static $singular_name = 'Dropdown'; |
||||
0 ignored issues
–
show
|
|||||
23 | |||||
24 | protected $fieldType = DropdownField::class; |
||||
25 | |||||
26 | public function getCMSFields() |
||||
27 | { |
||||
28 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||||
29 | $fields->addFieldToTab('Root.Main', PresentedOptionsField::create( |
||||
30 | 'Options', |
||||
31 | $this->FilterFor, |
||||
0 ignored issues
–
show
The property
FilterFor does not exist on SilverStripe\CKANRegistr...ResourceFilter\Dropdown . Since you implemented __get , consider adding a @property annotation.
Loading history...
|
|||||
32 | _t(__CLASS__ . '.OPTIONS', 'Presented options') |
||||
33 | )); |
||||
34 | }); |
||||
35 | |||||
36 | return parent::getCMSFields(); |
||||
37 | } |
||||
38 | |||||
39 | public function getClientConfig() |
||||
40 | { |
||||
41 | $config = parent::getClientConfig(); |
||||
42 | |||||
43 | try { |
||||
44 | $config['options'] = $this->getConfiguredOptions(); |
||||
45 | } catch (InvalidArgumentException $e) { |
||||
46 | $config['options'] = []; |
||||
47 | } |
||||
48 | |||||
49 | return $config; |
||||
50 | } |
||||
51 | |||||
52 | /** |
||||
53 | * Get the options that have been configured for this dropdown by the CMS author. ie. parse the "Options" value |
||||
54 | * |
||||
55 | * @throws InvalidArgumentException When the configured "selectType" is unknown |
||||
56 | */ |
||||
57 | protected function getConfiguredOptions() |
||||
58 | { |
||||
59 | $spec = json_decode($this->Options, true) ?: []; |
||||
0 ignored issues
–
show
The property
Options does not exist on SilverStripe\CKANRegistr...ResourceFilter\Dropdown . Since you implemented __get , consider adding a @property annotation.
Loading history...
It seems like
$this->Options can also be of type null ; however, parameter $json of json_decode() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
60 | |||||
61 | if (!isset($spec['selectType'])) { |
||||
62 | return []; |
||||
63 | } |
||||
64 | |||||
65 | $selectType = (int) $spec['selectType']; |
||||
66 | |||||
67 | if ($selectType === PresentedOptionsField::SELECT_TYPE_ALL) { |
||||
68 | return $spec['selections']; |
||||
69 | } |
||||
70 | if ($selectType === PresentedOptionsField::SELECT_TYPE_CUSTOM) { |
||||
71 | return $spec['customOptions']; |
||||
72 | } |
||||
73 | |||||
74 | throw new InvalidArgumentException('Unknown "selectType" is configured for ' . static::class); |
||||
75 | } |
||||
76 | } |
||||
77 |