Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lingotek
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
lingotek
Merge requests
!25
Issue
#3488426
: Support Custom Field module
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3488426
: Support Custom Field module
issue/lingotek-3488426:3488426-support-custom-field
into
4.1.x
Overview
0
Commits
1
Pipelines
1
Changes
1
Open
Jacob Rockowitz
requested to merge
issue/lingotek-3488426:3488426-support-custom-field
into
4.1.x
6 months ago
Overview
0
Commits
1
Pipelines
1
Changes
1
Expand
Closes
#3488426
0
0
Merge request reports
Compare
4.1.x
4.1.x (HEAD)
and
latest version
latest version
b4f4f613
1 commit,
6 months ago
1 file
+
82
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/Plugin/LingotekFieldProcessor/LingotekCustomFieldProcessor.php
0 → 100644
+
82
−
0
Options
<?php
namespace
Drupal\lingotek\Plugin\LingotekFieldProcessor
;
use
Drupal\Core\Entity\ContentEntityInterface
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Plugin\PluginBase
;
use
Drupal\lingotek\FieldProcessor\LingotekFieldProcessorInterface
;
use
Drupal\lingotek\LingotekContentTranslationEntityRevisionResolver
;
/**
* @LingotekFieldProcessor(
* id = "custom_field",
* weight = 5,
* )
*/
class
LingotekCustomFieldProcessor
extends
PluginBase
implements
LingotekFieldProcessorInterface
{
/**
* Translatable field type.
*
* @see \Drupal\custom_field\Plugin\CustomField\FieldType.
*/
protected
array
$translatableFieldTypes
=
[
'string'
,
'string_long'
];
/**
* Translatable field width.
*
* @see \Drupal\custom_field\Plugin\CustomField\FieldWidget
*/
protected
array
$translatableFieldWidgets
=
[
'text'
,
'textarea'
];
/**
* {@inheritdoc}
*/
public
function
appliesToField
(
FieldDefinitionInterface
$field_definition
,
ContentEntityInterface
&
$entity
)
{
return
(
'custom'
===
$field_definition
->
getType
());
}
/**
* {@inheritdoc}
*/
public
function
extract
(
ContentEntityInterface
&
$entity
,
string
$field_name
,
FieldDefinitionInterface
$field_definition
,
array
&
$data
,
array
&
$visited
=
[],
string
$revision_mode
=
LingotekContentTranslationEntityRevisionResolver
::
RESOLVE_LATEST_TRANSLATION_AFFECTED
)
{
// Get columns and field settings.
$settings
=
$field_definition
->
getSettings
();
$columns
=
$settings
[
'columns'
];
$field_settings
=
$settings
[
'field_settings'
];
// Get translatable columns.
$translatable_columns
=
[];
foreach
(
$columns
as
$column_name
=>
$column
)
{
$field_type
=
$column
[
'type'
];
$widget_type
=
$field_settings
[
$column_name
][
'type'
]
??
NULL
;
if
(
in_array
(
$field_type
,
$this
->
translatableFieldTypes
)
&&
in_array
(
$widget_type
,
$this
->
translatableFieldWidgets
)
)
{
$translatable_columns
[
$column_name
]
=
$column_name
;
}
}
// Extract translatable data.
$data
[
$field_name
]
=
[];
foreach
(
$entity
->
get
(
$field_name
)
as
$delta
=>
$field_item
)
{
$data
[
$field_name
][
$delta
]
=
array_intersect_key
(
$field_item
->
toArray
(),
$translatable_columns
);
}
}
/**
* {@inheritdoc}
*/
public
function
store
(
ContentEntityInterface
&
$translation
,
string
$langcode
,
ContentEntityInterface
&
$revision
,
string
$field_name
,
FieldDefinitionInterface
$field_definition
,
array
&
$field_data
)
{
foreach
(
$field_data
as
$delta
=>
$field_value
)
{
$existing_value
=
$translation
->
get
(
$field_name
)
->
get
(
$delta
)
->
getValue
();
$value
=
$field_value
+
$existing_value
;
$translation
->
get
(
$field_name
)
->
set
(
$delta
,
$value
);
}
}
}
Loading