Unverified Commit 9c054814 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2571371 by shubham.prakash, Berdir, ytsurk, Wim Leers, tstoeckler,...

Issue #2571371 by shubham.prakash, Berdir, ytsurk, Wim Leers, tstoeckler, Gábor Hojtsy: Editor settings cannot be translated

(cherry picked from commit f68812c3)
parent 921bc085
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -48,5 +48,5 @@ ckeditor.plugin.stylescombo:
  label: 'Styles dropdown'
  mapping:
    styles:
      type: label
      type: text
      label: 'List of styles'
+85 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\ckeditor\Functional;

use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;

/**
 * Tests administration of the CKEditor StylesCombo plugin.
 *
 * @group ckeditor
 */
class CKEditorStylesComboTranslationTest extends BrowserTestBase {

  /**
   * {inheritdoc}
   */
  public static $modules = ['ckeditor', 'config_translation'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * A user with the 'administer filters' permission.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * A randomly generated format machine name.
   *
   * @var string
   */
  protected $format;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->format = strtolower($this->randomMachineName());
    $filter_format = FilterFormat::create([
      'format' => $this->format,
      'name' => $this->randomString(),
      'filters' => [],
    ]);
    $filter_format->save();
    $editor = Editor::create([
      'format' => $this->format,
      'editor' => 'ckeditor',
    ]);
    $editor->save();

    $this->adminUser = $this->drupalCreateUser(['administer filters', 'translate configuration']);

    ConfigurableLanguage::createFromLangcode('de')->save();
  }

  /**
   * Tests translations of CKEditor styles configuration.
   */
  public function testExistingFormat() {
    $this->drupalLogin($this->adminUser);
    $edit = [
      'editor[settings][plugins][stylescombo][styles]' => 'h1.title|Title',
    ];
    $this->drupalPostForm('admin/config/content/formats/manage/' . $this->format, $edit, 'Save configuration');

    $this->drupalGet('admin/config/content/formats/manage/' . $this->format . '/translate/de/add');
    $this->assertEquals('textarea', $this->assertSession()->fieldExists('List of styles')->getTagName());
    $this->assertSession()->fieldValueEquals('List of styles', 'h1.title|Title');

    $page = $this->getSession()->getPage();
    $page->fillField('List of styles', 'h1.title|Titel');
    $page->pressButton('Save translation');
    $this->assertSession()->pageTextContains('Successfully saved German translation.');
  }

}
+5 −0
Original line number Diff line number Diff line
@@ -5,3 +5,8 @@ services:
  element.editor:
    class: Drupal\editor\Element
    arguments: ['@plugin.manager.editor']
  editor.config_translation_mapper_subscriber:
    class: Drupal\editor\EventSubscriber\EditorConfigTranslationSubscriber
    arguments: ['@config.factory']
    tags:
      - {name: event_subscriber}
+62 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\editor\EventSubscriber;

use Drupal\config_translation\ConfigEntityMapper;
use Drupal\config_translation\Event\ConfigMapperPopulateEvent;
use Drupal\config_translation\Event\ConfigTranslationEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Config\ConfigFactoryInterface;

/**
 * Adds configuration names to configuration mapper on POPULATE_MAPPER event.
 */
class EditorConfigTranslationSubscriber implements EventSubscriberInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * EditorConfigTranslationSubscriber constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];
    if (class_exists('Drupal\config_translation\Event\ConfigTranslationEvents')) {
      $events[ConfigTranslationEvents::POPULATE_MAPPER][] = ['addConfigNames'];
    }
    return $events;
  }

  /**
   * Reacts to the populating of a configuration mapper.
   *
   * @param \Drupal\config_translation\Event\ConfigMapperPopulateEvent $event
   *   The configuration mapper event.
   */
  public function addConfigNames(ConfigMapperPopulateEvent $event) {
    $mapper = $event->getMapper();
    if ($mapper instanceof ConfigEntityMapper && $mapper->getType() == 'filter_format') {
      $editor_config_name = 'editor.editor.' . $mapper->getEntity()->id();
      // Only add the text editor config if it exists, otherwise we assume no
      // editor has been set for this text format.
      if (!$this->configFactory->get($editor_config_name)->isNew()) {
        $mapper->addConfigName($editor_config_name);
      }
    }
  }

}