Commit 6085b349 authored by webchick's avatar webchick

Issue #1842718 by alippai, andypost, alexpott, nod_: Use new Transliteration...

Issue #1842718 by alippai, andypost, alexpott, nod_: Use new Transliteration functionality in core for machine names.
parent 567203a0
......@@ -3759,6 +3759,9 @@ function form_validate_table($element, &$form_state) {
* name must not be changed after initial creation.
*/
function form_process_machine_name($element, &$form_state) {
// We need to pass the langcode to the client.
$language = language(LANGUAGE_TYPE_INTERFACE);
// Apply default form element properties.
$element += array(
'#title' => t('Machine-readable name'),
......@@ -3825,6 +3828,7 @@ function form_process_machine_name($element, &$form_state) {
'machineName' => array(
'#' . $source['#id'] => $element['#machine_name'],
),
'langcode' => $language->langcode,
),
);
$element['#attached']['library'][] = array('system', 'drupal.machine-name');
......
(function ($) {
(function ($, Drupal, drupalSettings) {
"use strict";
......@@ -42,19 +42,19 @@ Drupal.behaviors.machineName = {
function machineNameHandler(e) {
var data = e.data;
machine = self.transliterate($(e.target).val(), data.options);
// Set the machine name to the transliterated value.
if (machine !== '') {
if (machine !== data.options.replace) {
data.$target.val(machine);
data.$preview.html(data.options.field_prefix + Drupal.checkPlain(machine) + data.options.field_suffix);
}
data.$suffix.show();
var settings = data.options;
var baseValue = $(e.target).val();
var rx = new RegExp(settings.replace_pattern, 'g');
var expected = baseValue.toLowerCase().replace(rx, settings.replace).substr(0, settings.maxlength);
if(baseValue.toLowerCase() !== expected) {
self.transliterate(baseValue, settings).done(function (machine) {
self.showMachineName(machine.substr(0, settings.maxlength), data);
});
}
else {
data.$suffix.hide();
data.$target.val(machine);
data.$preview.empty();
self.showMachineName(expected, data);
}
}
......@@ -124,6 +124,23 @@ Drupal.behaviors.machineName = {
}
},
showMachineName: function (machine, data) {
var settings = data.options;
// Set the machine name to the transliterated value.
if (machine !== '') {
if (machine !== settings.replace) {
data.$target.val(machine);
data.$preview.html(settings.field_prefix + Drupal.checkPlain(machine) + settings.field_suffix);
}
data.$suffix.show();
}
else {
data.$suffix.hide();
data.$target.val(machine);
data.$preview.empty();
}
},
/**
* Transliterate a human-readable name to a machine name.
*
......@@ -141,9 +158,14 @@ Drupal.behaviors.machineName = {
* The transliterated source string.
*/
transliterate: function (source, settings) {
var rx = new RegExp(settings.replace_pattern, 'g');
return source.toLowerCase().replace(rx, settings.replace).substr(0, settings.maxlength);
return $.get(drupalSettings.basePath + 'machine_name/transliterate', {
text: source,
langcode: drupalSettings.langcode,
replace_pattern: settings.replace_pattern,
replace: settings.replace,
lowercase: true
});
}
};
})(jQuery);
})(jQuery, Drupal, drupalSettings);
<?php
/**
* @file
* Contains \Drupal\system\MachineNameController.
*/
namespace Drupal\system;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\ControllerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Controller routines for machine name transliteration routes.
*/
class MachineNameController implements ControllerInterface {
/**
* The transliteration helper.
*
* @var \Drupal\Component\Transliteration\TransliterationInterface
*/
protected $transliteration;
/**
* Constructs a MachineNameController object.
*
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* The transliteration helper.
*/
public function __construct(TransliterationInterface $transliteration) {
$this->transliteration = $transliteration;
}
/**
* Implements \Drupal\Core\ControllerInterface::create().
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('transliteration')
);
}
/**
* Transliterates a string in given language. Various postprocessing possible.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The input string and language for the transliteration.
* Optionally may contain the replace_pattern, replace, lowercase parameters.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The transliterated string.
*/
public function transliterate(Request $request) {
$text = $request->query->get('text');
$langcode = $request->query->get('langcode');
$replace_pattern = $request->query->get('replace_pattern');
$replace = $request->query->get('replace');
$lowercase = $request->query->get('lowercase');
$transliterated = $this->transliteration->transliterate($text, $langcode, '_');
if($lowercase) {
$transliterated = drupal_strtolower($transliterated);
}
if(isset($replace_pattern) && isset($replace)) {
$transliterated = preg_replace('@' . $replace_pattern . '@', $replace, $transliterated);
}
return new JsonResponse($transliterated);
}
}
<?php
/**
* @file
* Contains \Drupal\system\Tests\Transliteration\MachineNameControllerTest.
*/
namespace Drupal\system\Tests\Transliteration;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Component\Transliteration\PHPTransliteration;
use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\system\MachineNameController;
use Symfony\Component\HttpFoundation\Request;
/**
* Tests machine name controller's transliteration functionality.
*/
class MachineNameControllerTest extends DrupalUnitTestBase {
/**
* The machine name controller.
*
* @var \Drupal\system\MachineNameController
*/
protected $machineNameController;
public static function getInfo() {
return array(
'name' => 'Machine name controller tests',
'description' => 'Tests that the machine name controller can transliterate strings as expected.',
'group' => 'Transliteration',
);
}
public function setUp() {
parent::setUp();
// Create the machine name controller.
$this->machineNameController = new MachineNameController(new PHPTransliteration());
}
/**
* Tests machine name controller's transliteration functionality.
*
* @see \Drupal\system\MachineNameController::transliterate()
*/
public function testMachineNameController() {
$request = Request::create('', 'GET', array('text' => 'Bob', 'langcode' => 'en'));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"Bob"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Bob', 'langcode' => 'en', 'lowercase' => TRUE));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"bob"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Bob', 'langcode' => 'en', 'replace' => 'Alice', 'replace_pattern' => 'Bob'));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"Alice"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Bob', 'langcode' => 'en', 'replace' => 'Alice', 'replace_pattern' => 'Tom'));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"Bob"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Äwesome', 'langcode' => 'en', 'lowercase' => TRUE));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"awesome"', $json->getContent());
$request = Request::create('', 'GET', array('text' => 'Äwesome', 'langcode' => 'de', 'lowercase' => TRUE));
$json = $this->machineNameController->transliterate($request);
$this->assertEqual('"aewesome"', $json->getContent());
}
}
......@@ -4,3 +4,9 @@ system.cron:
_controller: '\Drupal\system\CronController::run'
requirements:
_access_system_cron: 'TRUE'
system.machine_name_transliterate:
pattern: '/machine_name/transliterate'
defaults:
_controller: '\Drupal\system\MachineNameController::transliterate'
requirements:
_permission: 'access content'
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