Created
May 13, 2019 19:31
-
-
Save paboden/c795b729ab038f77bd1d3fd42b84a121 to your computer and use it in GitHub Desktop.
D8 language tweaks for better output between interface and content language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Some simple modifications that can help when dealing with an admin/interface language | |
* and a content language that have different directions (dir: ltr or rtl) | |
* | |
* Snippet to get current language of content. | |
* \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) | |
* | |
* Snippet to get current language of interface. This should be set to the users admin preference. | |
* \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE) | |
* | |
* Can use getDirection(), getID(), and getName() for specific info about the langauge. | |
*/ | |
/** | |
* Implements hook_editor_js_settings_alter | |
*/ | |
function MYMODULE_editor_js_settings_alter(array &$settings) { | |
// Gets the direction of the content langauge. | |
$direction = \Drupal::languageManager() | |
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) | |
->getDirection(); | |
// Set the language direction for ckeditor formatted text areas. | |
foreach ($settings['editor']['formats'] as $name => $value) { | |
$settings['editor']['formats'][$name]['editorSettings']['contentsLangDirection'] = $direction; | |
} | |
} | |
/** | |
* Implements hook_preprocess_input(). | |
*/ | |
function MYMODULE_preprocess_input(&$vars) { | |
if (isset($vars['attributes']['id'])) { | |
// Gets the direction of the content langauge. | |
$direction = \Drupal::languageManager() | |
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) | |
->getDirection(); | |
// sets the direction for menu description form field | |
if ($vars['attributes']['id'] == 'edit-menu-description') { | |
$vars['attributes']['dir'] = $direction; | |
} | |
// sets the direction for the menu title form field | |
if ($vars['attributes']['id'] == 'edit-menu-title') { | |
$vars['attributes']['dir'] = $direction; | |
} | |
} | |
} | |
/** | |
* Implements hook_preprocess_textarea(). | |
*/ | |
function MYMODULE_preprocess_textarea(&$vars) { | |
$direction = \Drupal::languageManager() | |
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) | |
->getDirection(); | |
// If dealing with an Attribute object, use the setAttribute method. | |
// Sometimes the attributes are already set as an array, sometimes | |
// they are an attribute object. Be ready my friends. | |
if ($vars['element']['#parents'][0] == 'body') { | |
$vars['attributes']->setAttribute('dir', $direction); | |
} | |
} | |
/** | |
* Implements hook_preprocess_html(). | |
*/ | |
function MYMODULE_preprocess_html(&$vars) { | |
$direction = \Drupal::languageManager() | |
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT) | |
->getDirection(); | |
// If you want the output to follow the direction of the content language, | |
// regardless of the interface langauge direction. Helpful in making RTL language | |
// pages look more appropriate, but still allow the favored admin language on | |
// the toolbar and tabs | |
if (!\Drupal::service('router.admin_context')->isAdminRoute()) { | |
$vars['html_attributes']['dir'] = $direction; | |
} | |
} | |
/** | |
* Implements hook_form_FORM_ID_alter(). | |
* | |
* Alters the node edit form. | |
*/ | |
function MYMODULE_form_node_page_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { | |
// This is good to modify those fields whose content entry would make more sense to set to the direction | |
// of the contents language. Like, on an arabic content page with an english interface, you probably | |
// don't need the user author field to be rtl, nor do you need the revision comments field. | |
// Set the individual fields you think will best work with the content language direction. | |
$content_language = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT); | |
$direction = $content_language->getDirection(); | |
// This will alter the language direction of the title textfield | |
foreach ($form['title']['widget'] as $key => $value) { | |
if (is_numeric($key)) { | |
$form['title']['widget'][$key]['value']['#attributes']['dir'] = $direction; | |
} | |
} | |
// Textarea field language direction alteration example. | |
// This will modify the dir of a body field, without a ckeditor | |
foreach ($form['body']['widget'] as $key => $value) { | |
if (is_numeric($key)) { | |
$form['body']['widget'][$key]['#attributes']['dir'] = $direction; | |
} | |
} | |
// Example for modifying direction of a link field. | |
foreach ($form['LINK_FIELD_NAME']['widget'] as $key => $value) { | |
if (is_numeric($key)) { | |
$form['field_related_links']['widget'][$key]['uri']['#attributes']['dir'] = $direction; | |
$form['field_related_links']['widget'][$key]['title']['#attributes']['dir'] = $direction; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment