Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
jsonapi_reference
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
jsonapi_reference
Commits
fc35820b
Commit
fc35820b
authored
6 months ago
by
arousseau
Browse files
Options
Downloads
Patches
Plain Diff
Added OptionsProviderInterface implementation
#3474619
parent
1074c4e4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!23
Added OptionsProviderInterface implementation #3474619
Pipeline
#284255
passed
6 months ago
Stage: build
Stage: validate
Stage: test
Changes
1
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Field/FieldType/TypedResourceObjectItem.php
+64
-4
64 additions, 4 deletions
src/Plugin/Field/FieldType/TypedResourceObjectItem.php
with
64 additions
and
4 deletions
src/Plugin/Field/FieldType/TypedResourceObjectItem.php
+
64
−
4
View file @
fc35820b
...
...
@@ -6,8 +6,11 @@ use Drupal\Core\Field\FieldDefinitionInterface;
use
Drupal\Core\Field\FieldItemBase
;
use
Drupal\Core\Field\FieldStorageDefinitionInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Session\AccountInterface
;
use
Drupal\Core\StringTranslation\TranslatableMarkup
;
use
Drupal\Core\TypedData\DataDefinition
;
use
Drupal\Core\TypedData\OptionsProviderInterface
;
use
Drupal\jsonapi_reference
\JsonApiClientInterface
;
/**
* Plugin implementation of the 'typed_resource_object' field type.
...
...
@@ -21,7 +24,14 @@ use Drupal\Core\TypedData\DataDefinition;
* default_widget = "typed_resource_object_autocomplete"
* )
*/
class
TypedResourceObjectItem
extends
FieldItemBase
{
class
TypedResourceObjectItem
extends
FieldItemBase
implements
OptionsProviderInterface
{
/**
* The JSON:API client service.
*
* @var \Drupal\jsonapi_reference\JsonApiClientInterface
*/
protected
$client
;
/**
* {@inheritdoc}
...
...
@@ -89,9 +99,7 @@ class TypedResourceObjectItem extends FieldItemBase {
public
function
storageSettingsForm
(
array
&
$form
,
FormStateInterface
$form_state
,
$has_data
):
array
{
$elements
=
[];
/** @var \Drupal\jsonapi_reference\JsonApiClientInterface $client */
$client
=
\Drupal
::
service
(
'jsonapi_reference.jsonapi_client'
);
$types
=
$client
->
listResourceObjectTypes
();
$types
=
$this
->
client
()
->
listResourceObjectTypes
();
$elements
[
'resource_object_type'
]
=
[
'#type'
=>
'select'
,
...
...
@@ -124,6 +132,19 @@ class TypedResourceObjectItem extends FieldItemBase {
return
$value
===
NULL
||
$value
===
''
;
}
/**
* Gets the JSON:API client service.
*
* @return \Drupal\jsonapi_reference\JsonApiClientInterface
*/
protected
function
client
():
JsonApiClientInterface
{
if
(
!
isset
(
$this
->
client
))
{
$this
->
client
=
\Drupal
::
service
(
'jsonapi_reference.jsonapi_client'
);
}
return
$this
->
client
;
}
/**
* Automatically cast field to a string.
*
...
...
@@ -138,4 +159,43 @@ class TypedResourceObjectItem extends FieldItemBase {
return
$this
->
get
(
'value'
)
->
getValue
();
}
/**
* {@inheritdoc}
*/
public
function
getPossibleValues
(
?AccountInterface
$account
=
NULL
):
array
{
return
$this
->
getSettableValues
(
$account
);
}
/**
* {@inheritdoc}
*/
public
function
getPossibleOptions
(
?AccountInterface
$account
=
NULL
):
array
{
return
$this
->
getSettableOptions
(
$account
);
}
/**
* {@inheritdoc}
*/
public
function
getSettableValues
(
?AccountInterface
$account
=
NULL
):
array
{
return
array_keys
(
$this
->
getSettableOptions
(
$account
));
}
/**
* {@inheritdoc}
*/
public
function
getSettableOptions
(
?AccountInterface
$account
=
NULL
):
array
{
$options
=
$this
->
client
()
->
search
(
$this
->
getSetting
(
'resource_object_type'
),
$this
->
getSetting
(
'title_attribute'
),
NULL
);
$return
=
[];
foreach
(
$options
as
$option
)
{
$return
[
$option
[
1
]]
=
$option
[
0
];
}
return
$return
;
}
}
This diff is collapsed.
Click to expand it.
Elliot Ward
@Eli-T
mentioned in commit
c43f4fd8
·
6 months ago
mentioned in commit
c43f4fd8
mentioned in commit c43f4fd83bd62dce17842fa14c82012f81ceb866
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment