Commit 4a06454b authored by alexpott's avatar alexpott

Issue #2224779 by swentel: Move 'column_groups' out of settings in a dedicated annotation.

parent d026a1c3
......@@ -23,7 +23,8 @@
function content_translation_field_sync_widget(FieldDefinitionInterface $field) {
$element = array();
$column_groups = $field->getSetting('column_groups');
$definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field->getType());
$column_groups = $definition['column_groups'];
if (!empty($column_groups) && count($column_groups) > 1) {
$options = array();
$default = array();
......
......@@ -166,6 +166,27 @@ function content_translation_entity_base_field_info_alter(&$fields, EntityTypeIn
}
}
/**
* Implements hook_field_info_alter().
*
* Content translation extends the @FieldType annotation with following key:
* - column_groups: contains information about the field type properties
* which columns should be synchronized across different translations and
* which are translatable. This is useful for instance to translate the
* "alt" and "title" textual elements of an image field, while keeping the
* same image on every translation.
*
* @see Drupal\image\Plugin\Field\FieldType\imageItem.
*/
function content_translation_field_info_alter(&$info) {
foreach ($info as $key => $settings) {
// Supply the column_groups key if it's not there.
if (empty($settings['column_groups'])) {
$info[$key]['column_groups'] = array();
}
}
}
/**
* Implements hook_entity_operation_alter().
*/
......
......@@ -38,6 +38,7 @@ public function __construct(EntityManagerInterface $entityManager) {
*/
public function synchronizeFields(ContentEntityInterface $entity, $sync_langcode, $original_langcode = NULL) {
$translations = $entity->getTranslationLanguages();
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
// If we have no information about what to sync to, if we are creating a new
// entity, if we have no translations for the current entity and we are not
......@@ -55,6 +56,8 @@ public function synchronizeFields(ContentEntityInterface $entity, $sync_langcode
foreach ($entity as $field_name => $items) {
$field_definition = $items->getFieldDefinition();
$field_type_definition = $field_type_manager->getDefinition($field_definition->getType());
$column_groups = $field_type_definition['column_groups'];
// Sync if the field is translatable, not empty, and the synchronization
// setting is enabled.
......@@ -65,7 +68,6 @@ public function synchronizeFields(ContentEntityInterface $entity, $sync_langcode
if (!empty($groups)) {
$columns = array();
foreach ($groups as $group) {
$column_groups = $field_definition->getSetting('column_groups');
$info = $column_groups[$group];
// A missing 'columns' key indicates we have a single-column group.
$columns = array_merge($columns, isset($info['columns']) ? $info['columns'] : array($group));
......
......@@ -90,43 +90,6 @@ field.image.settings:
default_image:
type: field_default_image
label: 'Default value'
column_groups:
type: mapping
label: 'Column groups'
mapping:
file:
type: mapping
label: 'File settings'
mapping:
label:
type: label
label: 'Label'
columns:
type: sequence
label: 'Columns'
sequence:
- type: string
label: 'Column'
alt:
type: mapping
label: 'Alternative text'
mapping:
label:
type: label
label: 'Label'
translatable:
type: boolean
label: 'Translatable'
title:
type: mapping
label: 'Title text'
mapping:
label:
type: label
label: 'Label'
translatable:
type: boolean
label: 'Translatable'
field.image.instance_settings:
type: mapping
......
......@@ -20,6 +20,22 @@
* description = @Translation("This field stores the ID of an image file as an integer value."),
* default_widget = "image_image",
* default_formatter = "image",
* column_groups = {
* "file" = {
* "label" = @Translation("File"),
* "columns" = {
* "target_id", "width", "height"
* }
* },
* "alt" = {
* "label" = @Translation("Alt"),
* "translatable" = TRUE
* },
* "title" = {
* "label" = @Translation("Title"),
* "translatable" = TRUE
* },
* },
* list_class = "\Drupal\file\Plugin\Field\FieldType\FileFieldItemList"
* )
*/
......@@ -37,20 +53,6 @@ public static function defaultSettings() {
'width' => NULL,
'height' => NULL,
),
'column_groups' => array(
'file' => array(
'label' => t('File'),
'columns' => array('target_id', 'width', 'height'),
),
'alt' => array(
'label' => t('Alt'),
'translatable' => TRUE,
),
'title' => array(
'label' => t('Title'),
'translatable' => TRUE,
),
),
) + parent::defaultSettings();
}
......
......@@ -11,19 +11,6 @@ settings:
title: ''
width: null
height: null
column_groups:
file:
label: File
columns:
- target_id
- width
- height
alt:
label: Alt
translatable: true
title:
label: Title
translatable: true
locked: false
cardinality: 1
translatable: false
......
......@@ -12,19 +12,6 @@ settings:
title: ''
width: null
height: null
column_groups:
file:
label: File
columns:
- target_id
- width
- height
alt:
label: Alt
translatable: true
title:
label: Title
translatable: true
module: image
locked: false
cardinality: 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment