class FieldSyncWidget

Field Sync Widget for content_translation.

Hierarchy

Expanded class hierarchy of FieldSyncWidget

3 files declare their use of FieldSyncWidget
ContentTranslationFormHooks.php in core/modules/content_translation/src/Hook/ContentTranslationFormHooks.php
ContentTranslationFormLanguageHooks.php in core/modules/content_translation/src/Hook/ContentTranslationFormLanguageHooks.php
content_translation.module in core/modules/content_translation/content_translation.module

File

core/modules/content_translation/src/FieldSyncWidget.php, line 15

Namespace

Drupal\content_translation
View source
class FieldSyncWidget {
  use StringTranslationTrait;
  public function __construct(protected readonly FieldTypePluginManagerInterface $fieldTypePluginManager) {
  }
  
  /**
   * Returns a form element to configure field synchronization.
   *
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field
   *   A field definition object.
   * @param string $element_name
   *   (optional) The element name, which is added to drupalSettings so that
   *   javascript can manipulate the form element.
   *
   * @return array
   *   A form element to configure field synchronization.
   */
  public function widget(FieldDefinitionInterface $field, $element_name = 'third_party_settings[content_translation][translation_sync]') : array {
    // No way to store field sync information on this field.
    if (!$field instanceof ThirdPartySettingsInterface) {
      return [];
    }
    $element = [];
    $definition = $this->fieldTypePluginManager
      ->getDefinition($field->getType());
    $column_groups = $definition['column_groups'];
    if (!empty($column_groups) && count($column_groups) > 1) {
      $options = [];
      $default = [];
      $require_all_groups_for_translation = [];
      foreach ($column_groups as $group => $info) {
        $options[$group] = $info['label'];
        $default[$group] = !empty($info['translatable']) ? $group : FALSE;
        if (!empty($info['require_all_groups_for_translation'])) {
          $require_all_groups_for_translation[] = $group;
        }
      }
      $default = $field->getThirdPartySetting('content_translation', 'translation_sync', $default);
      $element = [
        '#type' => 'checkboxes',
        '#title' => $this->t('Translatable elements'),
        '#options' => $options,
        '#default_value' => $default,
      ];
      if ($require_all_groups_for_translation) {
        // The actual checkboxes are sometimes rendered separately and the
        // parent element is ignored. Attach to the first option to ensure that
        // this does not get lost.
        $element[key($options)]['#attached']['drupalSettings']['contentTranslationDependentOptions'] = [
          'dependent_selectors' => [
            $element_name => $require_all_groups_for_translation,
          ],
        ];
        $element[key($options)]['#attached']['library'][] = 'content_translation/drupal.content_translation.admin';
      }
    }
    return $element;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
FieldSyncWidget::widget public function Returns a form element to configure field synchronization.
FieldSyncWidget::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.