Skip to content
Snippets Groups Projects

Issue #3488426: Support Custom Field module

1 file
+ 82
0
Compare changes
  • Side-by-side
  • Inline
<?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