Skip to content
Snippets Groups Projects
Commit 97ceb189 authored by Al Munnings's avatar Al Munnings
Browse files

Re-do inflector singularize with media exclusion, and language select ui

parents 6885fd8a 7917e18b
Branches
Tags
No related merge requests found
Pipeline #36077 passed
......@@ -10,3 +10,4 @@ settings:
site_front: true
site_name: false
site_slogan: false
inflector_langcode: 'en'
......@@ -49,6 +49,9 @@ graphql_compose.settings:
svg_filesize:
type: integer
label: 'Expose SVG filesize'
inflector_langcode:
type: string
label: 'Inflector language code'
custom:
type: sequence
label: 'Custom settings'
......
......@@ -12,7 +12,7 @@ services:
class: Drupal\graphql_compose\LanguageInflector
arguments:
- '@module_handler'
- '@language_manager'
- '@config.factory'
# Cache bin for graphql_compose plugin definitions.
cache.graphql_compose.definitions:
......@@ -20,7 +20,8 @@ services:
tags:
- { name: cache.bin }
factory: cache_factory:get
arguments: [graphql_compose_definitions]
arguments:
- graphql_compose_definitions
# Plugin manager for schema
graphql_compose.schema_type_manager:
......
......@@ -326,6 +326,23 @@ class SettingsForm extends ConfigFormBase {
];
}
$form['advanced']['inflector_langcode'] = [
'#type' => 'select',
'#title' => $this->t('Inflector language'),
'#description' => $this->t('The language to use for inflection. Inflection will change the singularization and pluralization of schema types. Eg Tags -> Tag, Quizzes -> Quiz.'),
'#options' => [
'en' => $this->t('English'),
'fr' => $this->t('French'),
'nb' => $this->t('Norwegian Bokmal'),
'pt-pt' => $this->t('Portuguese'),
'pt-br' => $this->t('Portuguese (Brazil)'),
'es' => $this->t('Spanish'),
'tr' => $this->t('Turkish'),
],
'#default_value' => $this->getConfig()->get('settings.inflector_langcode') ?: 'en',
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
......@@ -504,6 +521,7 @@ class SettingsForm extends ConfigFormBase {
->set('settings.site_slogan', $form_state->getValue('site_slogan'))
->set('settings.svg_image', $form_state->getValue('svg_image', FALSE))
->set('settings.svg_filesize', $form_state->getValue('svg_filesize', 100))
->set('settings.inflector_langcode', $form_state->getValue('inflector_langcode'))
->set('settings.custom', $custom_settings)
->save();
......
......@@ -8,8 +8,14 @@ use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Doctrine\Inflector\Language;
use Doctrine\Inflector\LanguageInflectorFactory;
use Doctrine\Inflector\Rules\Patterns;
use Doctrine\Inflector\Rules\Ruleset;
use Doctrine\Inflector\Rules\Substitution;
use Doctrine\Inflector\Rules\Substitutions;
use Doctrine\Inflector\Rules\Transformations;
use Doctrine\Inflector\Rules\Word;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
......@@ -31,12 +37,12 @@ class LanguageInflector {
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* Module handler service.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* Language manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory service.
*/
public function __construct(
protected ModuleHandlerInterface $moduleHandler,
protected LanguageManagerInterface $languageManager
protected ConfigFactoryInterface $configFactory,
) {
$this->inflector = $this->getInflectorFactory()->build();
}
......@@ -53,7 +59,22 @@ class LanguageInflector {
* @see https://www.doctrine-project.org/projects/doctrine-inflector/en/2.0/index.html
*/
protected function getInflectorFactory(): LanguageInflectorFactory {
return InflectorFactory::createForLanguage($this->getInflectorLanguage());
$factory = InflectorFactory::createForLanguage($this->getInflectorLanguage());
$factory->withSingularRules(
new Ruleset(
new Transformations(),
new Patterns(),
new Substitutions(
// Drupal'isms point to 'media' being used in a singular form.
// Eg getMediaType() not getMediumType().
// Adding an underscore to the singular form bypasses the inflector.
new Substitution(new Word('media'), new Word('_media'))
)
),
);
return $factory;
}
/**
......@@ -65,7 +86,10 @@ class LanguageInflector {
* The language name that works with doctrine inflector.
*/
protected function getInflectorLanguage(): string {
switch ($this->languageManager->getDefaultLanguage()->getId()) {
$langcode = $this->configFactory->get('graphql_compose.settings')->get('settings.inflector_langcode');
switch ($langcode) {
case 'fr':
return Language::FRENCH;
......@@ -91,7 +115,7 @@ class LanguageInflector {
* Returns the singular form of a string.
*
* @param string $original
* The bundle string to be singularized.
* The string to be singularized.
*
* @return string
* Singular form of a string.
......@@ -101,6 +125,9 @@ class LanguageInflector {
public function singularize(string $original): string {
$singular = $this->inflector->singularize($original);
// Remove any leading slash added by inflector rule bypasses.
$singular = ltrim($singular, '_');
$this->moduleHandler->invokeAll('graphql_compose_singularize_alter', [
$original,
&$singular,
......@@ -112,20 +139,23 @@ class LanguageInflector {
/**
* Returns the plural forms of a string.
*
* If the method can't determine the form with certainty,
* several possible plurals are returned.
*
* @param string $singular
* Singular form of a string.
*
* @return string
* Plural form(s) of a string.
* Plural form of a string.
*
* @see hook_graphql_compose_pluralize_alter()
*/
public function pluralize(string $singular): string {
$plural = $this->inflector->pluralize($singular);
// If the plural is the same as the original,
// Failsafe pluralize. news... news_items.
if ($plural === $singular) {
$plural .= '_' . $this->t('items');
}
$this->moduleHandler->invokeAll('graphql_compose_pluralize_alter', [
$singular,
&$plural,
......
......@@ -129,10 +129,6 @@ class EntityTypeWrapper {
$singular = $this->inflector->singularize($this->entity->id());
$plural = $this->inflector->pluralize($singular);
if ($plural === $singular) {
$plural .= $this->t('Items');
}
return u($plural)
->title()
->prepend($this->entityTypePlugin->getPrefix())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment