0

Is it possible to mark only a single property of a field as translateable?

E.g. I have a custom field type RoomRates with 2 properties uri and price_mode.

/**
 * Plugin implementation of the 'kognitiv_room_rate' field type.
 *
 * @FieldType(
 *   id = "kognitiv_room_rate",
 *   label = @Translation("Kognitiv room rate"),
 *   description = @Translation("Stores an URL to retrieve price and availability data for a room"),
 *   default_widget = "kognitiv_room_rate",
 *   default_formatter = "kognitiv_room_rate",
 *   cardinality = 1,
 *   column_groups = {
 *     "uri" = {
 *       "label" = @Translation("URI"),
 *       "translatable" = TRUE
 *     },
 *     "price_mode" = {
 *       "label" = @Translation("Show prices as")
 *     }
 *   }
 * )
 */
class RoomRate extends FieldItemBase implements LinkItemInterface {

  const PRICE_MODE_PERSON = 'PERSON';
  const PRICE_MODE_DAY = 'DAY';

  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['uri'] = DataDefinition::create('uri')
      ->setLabel(t('URI'))
      ->setRequired(TRUE);

    $properties['price_mode'] = DataDefinition::create('string')
      ->setLabel(t('Show prices as'))
      ->setRequired(TRUE);

    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      'columns' => [
        'uri' => [
          'description' => 'The URI of the JSON API endpoint.',
          'type' => 'varchar',
          'length' => 2048,
        ],
        'price_mode' => [
          'description' => 'Show prices as price per day or price per person in std occupancy',
          'type' => 'varchar',
          'length' => 10,
        ],
      ],
      'indexes' => [],
      'foreign keys' => [],
    ];
  }

The uri property should be translateable (if the site builder enables translation for this field), while the price_mode property should never be translateable/always be the same accross all languages (no matter if the site builder enabled field translation or not).

1 Answer 1

2

content_translation has a feature that synchronizes certain property between translations, see how the \Drupal\image\Plugin\Field\FieldType\ImageItem field type uses that:

 *   column_groups = {
 *     "file" = {
 *       "label" = @Translation("File"),
 *       "columns" = {
 *         "target_id", "width", "height"
 *       },
 *       "require_all_groups_for_translation" = TRUE
 *     },
 *     "alt" = {
 *       "label" = @Translation("Alt"),
 *       "translatable" = TRUE
 *     },
 *     "title" = {
 *       "label" = @Translation("Title"),
 *       "translatable" = TRUE
 *     },
 *   },

By not setting translatable = TRUE, the UI by default suggests to keep that value across translations, although it can be configured differently.

3
  • Is there anything else beyond the annotation to look for? I've added the column_groups to my field's annotation, but that doesn't seem to have any effect. When I add a translation, I always get my primary lang's value as preset (e.g. price_mode does not have "translatable" = TRUE, but I still get the value from the primary lang as default)
    – Hudri
    Feb 20, 2018 at 15:38
  • Did you configure it then explicitly on the content translation settings page (the one with the trillion checkboxes for each bundle and field). You need to do that to enable it, this is apprently only used as a default value for that form but not at runtime
    – Berdir
    Feb 20, 2018 at 18:38
  • Thanks, that was the missing point, I just checked the field's translateable checkbox under Structure / Manage Fields, this was not enough.
    – Hudri
    Feb 21, 2018 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.